具有嵌套字段和映射的Spring Data弹性搜索

Ale*_*xis 11 elasticsearch spring-data spring-data-elasticsearch

我正在使用spring-data-elasticsearch和elasticsearch来查询文档.我想对嵌套文档进行嵌套查询.

我在java中有这个:

@Document(indexName = "as", type = "a", indexStoreType = "memory", shards = 1, replicas = 0, refreshInterval = "-1")
class A {

     @Id
     private String Id;

     @Field(type = String, index = analyzed, store = true)
     private String field1;

     // ... Many more Fields.

     @NestedField(type = FieldType.Object, index = analyzed, store = true, dotSuffix = "accounts")
     private List<B> bs;

     // ... getters and setters
}
Run Code Online (Sandbox Code Playgroud)

class B { // some normal pojo }
Run Code Online (Sandbox Code Playgroud)

当我让spring-data进行映射时,我得到:

"a": {
    "properties": {
        "bs": {
            "properties": {
                "someBProperty": {
                    "type": "string"
                },
                "BId": {
                    "type": "string"
                }
            }
        },
        "id": { ... },
        ...
}
Run Code Online (Sandbox Code Playgroud)

当我尝试查询文档时,我得到了经典的内部与嵌套文档问题,并且它无法识别嵌套元素.

当我尝试更新映射以使用嵌套文档时,我得到"无法从非嵌套更改为嵌套".

我应该以某种方式告诉spring-data-es @NestedField => type:"嵌套"到映射中吗?有没有办法在创建索引和映射时为spring-data添加自定义映射?

另外,我通过以下方式初始化索引:

elasticsearchTemplate.deleteIndex(A.class);
elasticsearchTemplate.createIndex(A.class);
elasticsearchTemplate.putMapping(A.class);
elasticsearchTemplate.refresh(A.class,true);
Run Code Online (Sandbox Code Playgroud)

然后使用spring-data存储库查询:

QueryBuilder builder = QueryBuilders.nestedQuery( "bs", QueryBuilders.boolQuery().must(QueryBuilders.matchQuery("as.field1", "A1")).must(QueryBuilders.matchQuery("as.field2", "B1")));

Iterable<DenormalizedRelationshipDocument> res = aRepository.search(builder);
Run Code Online (Sandbox Code Playgroud)

这里res在Iterable中有0个元素,但是通过REST我得到了不支持嵌套查询的错误(因为我在映射中没有它).

最后,

Spring-Data-ElasticSearch是否支持通过ES QueryBuilders API进行嵌套映射?我什么时候应该进行映射?

Moh*_*sen 14

Spring数据弹性搜索现在支持弹性搜索的大多数常见功能集,包括嵌套,内部对象和父子(最近)

在弹性搜索管理关系可以找到详细的解释

嵌套文档示例

人物实体

   @Document( indexName = "person" , type = "user")

    public class Person {

        @Id
        private String id;

        private String name;

        @Field( type = FieldType.Nested)
        private List<Car> car;

        // setters-getters

    }
Run Code Online (Sandbox Code Playgroud)

汽车实体



    public class Car {
    private String name;
    private String model;
    //setters and getters 
    }

Run Code Online (Sandbox Code Playgroud)

设置数据



    Person foo = new Person();
    foo.setName("Foo");
    foo.setId("1");

    List cars = new ArrayList();
    Car subaru = new Car();
    subaru.setName("Subaru");
    subaru.setModel("Imprezza");
    cars.add(subaru);
    foo.setCar(cars);

Run Code Online (Sandbox Code Playgroud)

索引



        IndexQuery indexQuery = new IndexQuery();
        indexQuery.setId(foo.getId());
        indexQuery.setObject(foo);

       //creating mapping
       elasticsearchTemplate.putMapping(Person.class);
       //indexing document
       elasticsearchTemplate.index(indexQuery);
       //refresh
       elasticsearchTemplate.refresh(Person.class, true);

Run Code Online (Sandbox Code Playgroud)

搜索

 

    QueryBuilder builder = nestedQuery("car", boolQuery().must(termQuery("car.name",      "subaru")).must(termQuery("car.model", "imprezza")));

    SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).build();
    List persons = elasticsearchTemplate.queryForList(searchQuery, Person.class);

Run Code Online (Sandbox Code Playgroud)

您可以在嵌套对象测试中找到有关嵌套和内部对象的更多测试用例