在 Spring java 框架中使用 ElasticSearch 的最佳方式

我正在开发一个系统,计划将弹性搜索用作数据存储库.我正在尝试选择开发我的应用程序的最佳方式,该应用程序可以索引和查询来自 elasticsearch 的数据.我的系统是建立在 Spring 框架之上的.

I'm developing a system which is planning to use elasticsearch as an data repository. I'm trying to choose the best way to develop my application that can index and query data from elasticsearch. The system I have is built on top of Spring framework.

使用Spring-data-elasticsearch(https:///github.com/spring-projects/spring-data-elasticsearch)?

或者使用elasticsearch核心库本身是一个不错的选择?

Or is it a good choice to use elasticsearch core libraries itself?

我需要处理嵌套数据(内部对象),但 Spring-data-elasticsearch 最近似乎没有任何操作.

I need to handle nested data (inner object) but Spring-data-elasticsearch seems to have no operations for that recently.

我希望我能找到问题的解决方案.提前致谢.

I hope I can find a solution for the question. Thanks in advance.

推荐答案

Spring data elasticsearch支持elasticsearch的大部分常用功能集,包括Nested、Inner Objects和Parent Child(最近).

Spring data elasticsearch supports most of the common feature set of elasticsearch including Nested, Inner Objects and Parent Child (recently).

当您说要使用嵌套数据(内部对象)时,请清楚elasticsearch有两个概念:内部对象和嵌套对象.

When you said that want to use nested data (inner object), please be clear as elasticsearch has two concepts: Inner Object and Nested Object.

详细解释可以在在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);

您可以在 嵌套对象测试

相关文章