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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20931053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:23:22  来源:igfitidea点击:

The Best way to use ElasticSearch in Spring java framework

javaspringframeworkselasticsearchspring-data-elasticsearch

提问by Hosang Jeon

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.

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

Is it a good choice to use Spring-data-elasticsearch(https://github.com/spring-projects/spring-data-elasticsearch)?

使用Spring-data-elasticsearch( https://github.com/spring-projects/spring-data-elasticsearch)是一个不错的选择吗?

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

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

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

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

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

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

采纳答案by Mohsin Husen

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

Spring data elasticsearch 支持 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有两个概念:内部对象和嵌套对象。

Detailed explanation can be found at managing relationship in elasticsearch

详细解释可以在elasticsearch中的管理关系中找到

Nested document Example

嵌套文档示例

Person Entity:

个人实体:

@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
}

Car Entity:

汽车实体:

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

Setting up data:

设置数据:

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

Indexing:

索引:

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

Searching:

搜索:

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

You can find more test cases about Nested and Inner Object at Nested Object Tests

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

回答by kwntn

You can use IndexQuery for save AND update:

您可以使用 IndexQuery 进行保存和更新:

public Serializable saveOrUpdate(Car car) {
    return template.index(new IndexQueryBuilder().withObject(car).build());
}

回答by Emma

Spring data elasticsearch uses transport client, and transport client is not supported by AWS elasticsearch. AWS elasticsearch supports only HTTP clients. So i think the best java client for elasticsearch is JEST. It also provides support for AWS authentication using IAM.

Spring data elasticsearch 使用传输客户端,AWS elasticsearch 不支持传输客户端。AWS elasticsearch 仅支持 HTTP 客户端。所以我认为elasticsearch最好的java客户端是JEST。它还支持使用 IAM 的 AWS 身份验证。