带有嵌套字段和映射的 Spring Data 弹性搜索 [英] Spring Data Elastic Search with Nested Fields and mapping
问题描述
我同时使用 spring-data-elasticsearch 和 elasticsearch 来查询文档.我想对嵌套文档进行嵌套查询.
I am using spring-data-elasticsearch and elasticsearch together to query documents. I'd like to do nested queries on nested documents.
我在java中有这个:
I have this in 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
}
还有
class B { // some normal pojo }
当我让 spring-data 做映射时,我得到:
When I let spring-data do the mapping, I get :
"a": {
"properties": {
"bs": {
"properties": {
"someBProperty": {
"type": "string"
},
"BId": {
"type": "string"
}
}
},
"id": { ... },
...
}
当我尝试查询文档时,我遇到了经典的内部与嵌套文档问题,并且它无法识别嵌套元素.
When I am trying to query the document, I get the classical inner vs nested documents problems and it doesn't recognize the nested element.
当我尝试更新映射以使用嵌套文档时,我得到无法从非嵌套更改为嵌套".
When I try to update mapping to use nested document I get "can't be changed from non-nested to nested".
我应该以某种方式告诉 spring-data-es @NestedField => type: "nested" into mapping 吗?有没有办法在创建索引时向 spring-data 添加自定义映射 &映射?
Should I tell spring-data-es somehow that @NestedField => type: "nested" into mapping ? Is there a way to add custom mapping to spring-data when it create index & mapping ?
此外,我正在通过以下方式初始化索引:
Also, I am initializing indexes via :
elasticsearchTemplate.deleteIndex(A.class);
elasticsearchTemplate.createIndex(A.class);
elasticsearchTemplate.putMapping(A.class);
elasticsearchTemplate.refresh(A.class,true);
然后使用 spring-data 存储库查询:
And then querying using spring-data repository :
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);
这里的 res 在 Iterable 中有 0 个元素,但通过 REST 我得到了不支持嵌套查询的错误(因为我在映射中没有它).
Here the res has 0 element in the Iterable but via REST I get the error on the nested query not supported (as I don't have it in mapping).
最后,
Spring-Data-ElasticSearch 是否支持通过 ES QueryBuilders API 进行嵌套映射?我应该什么时候进行映射?
Does Spring-Data-ElasticSearch supports nested mappings via ES QueryBuilders API ? When should I make that mapping happens ?
推荐答案
Spring data elasticsearch 现在支持 Elasticsearch 的大部分常用功能集,包括 Nested、Inner Objects 和 Parent Child(最近)
Spring data elasticsearch now supports most of the common feature set of elasticsearch including Nested, Inner Objects and Parent Child(recently)
详细解释可以在在elasticsearch中管理关系
嵌套文档示例
个人实体
@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
}
汽车实体
public class Car {
private String name;
private String model;
//setters and getters
}
设置数据
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);
索引
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);
搜索
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);
你可以在 嵌套对象测试
这篇关于带有嵌套字段和映射的 Spring Data 弹性搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!