Spring data elasticsearch支持 elasticsearch 的大多数常见功能集,包括 Nested、Inner Objects 和 Parent Child(最近)。
当你说想要使用嵌套数据(内部对象)时,请清楚,因为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<Car> cars = new ArrayList<Car>();
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<Person> persons = elasticsearchTemplate.queryForList(searchQuery, Person.class);
您可以在嵌套对象测试中找到有关嵌套和内部对象的更多测试用例