本文介绍了在Spring Java框架中使用ElasticSearch的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个系统,该系统计划将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数据Elasticsearch支持大多数Elasticsearch的常见功能集,包括嵌套,内部对象和父级子代.

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);

您可以在嵌套对象测试

这篇关于在Spring Java框架中使用ElasticSearch的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 13:17