Spring Data Elasticsearch - 如何将 Criteria 与嵌套字段一起使用

Spring Data Elasticsearch - How use Criteria with nested fields

我正在使用 spring-data-elasticsearch (4.0.1) 和 elasticsearch 一起查询文档。我想使用条件对嵌套文档进行嵌套查询,我注释了实体 类 但不能使用引用嵌套字段的条件进行查询。

{
  "user" : {
    "mappings" : {
      "properties" : {
        "contacts" : {
          "type" : "nested",
          "properties" : {
            "description" : {
              "type" : "text"
            },
            "id" : {
              "type" : "long"
            }
          }
        },
        "id" : {
          "type" : "long"
        },
        "name" : {
          "type" : "text"
        }
      }
    }
  }
}
{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "user",
        "_type" : "_doc",
        "_id" : "MgHFrXUBYAZ",
        "_score" : 1.0,
        "_source" : {
          "id" : 1,
          "name" : "Peter",
          "contacts" : [
            {
              "id" : 1,
              "description" : "foo"
            },
            {
              "id" : 2,
              "description" : "bar"
            }
          ]
        }
      }
    ]
  }
}
@Document(indexName = "user", createIndex = false)
public class User {

  @Id
  private Long id;

  @Field(type = FieldType.Nested)
  private List<Contacts> contactos;
}
public class Contacts {

  @Field(type = FieldType.Long)
  private Long id;

  @Field(type = FieldType.Text)
  private String description;
}
Criteria criteria = new Criteria("contacts.id").is(1);
CriteriaQuery query = new CriteriaQuery(criteria).setPageable(pageable);
SearchHits<User> searchHits = this.elasticsearchRestTemplate.search(query, User.class);

我没有得到任何结果,我做错了什么?

您可能希望使用来自 Spring 数据的 NativeSearchQueryBuilder 来实现此嵌套查询。这是一个关于它的样子的片段:

QueryBuilder builder = nestedQuery("contactos", boolQuery().must(termQuery("contacts.id", 1)));

SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).build();
List<User> users = elasticsearchTemplate.queryForList(searchQuery, User.class);