gpt4 book ai didi

java - 在 Java REST 客户端 [6.5] API 上使用 ES 6.5 中的映射创建索引

转载 作者:行者123 更新时间:2023-12-01 17:46:44 26 4
gpt4 key购买 nike

我是 Elasticsearch 新手,并尝试按照文章 https://www.elastic.co/blog/you-complete-me 为应用程序集成自动完成功能。 .

我按照下面的方法做了同样的事情。

事件类

       public class Event {

private Long eventId;
private Long catalogId;
private Long orgId;
private String orgName;
private String catalogName;
private String name;
private String eventStatus;
.....
}

对象映射器用于将事件对象转换为 json 字符串。这是插入文档的代码

public String createEventDocument(Event document) throws Exception {
IndexRequest indexRequest = new IndexRequest(INDEX, TYPE, document.idAsString())
.source(convertEventDocumentToMap(document));
//create mapping with a complete field
IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
return indexResponse.getResult().name();
}

转换代码

private Map<String, Object> convertEventDocumentToMap(Event evt) {
return objectMapper.convertValue(evt, Map.class);
}

我想创建一个索引,并为 name_suggest 字段设置完成建议器。我怎样才能达到同样的效果?

感谢任何帮助

最佳答案

这是执行相同操作的解决方案。首先使用映射器创建索引并插入数据

 public String createEventDocument(Event document) throws Exception {
GetIndexRequest request = new GetIndexRequest();
request.indices(INDEX);
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
if(!exists){
createIndexWithMapping();
}
IndexRequest indexRequest = new IndexRequest(INDEX, TYPE, document.idAsString())
.source(convertEventDocumentToMap(document));
//create mapping with a complete field
IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
return indexResponse.getResult().name();
}

private boolean createIndexWithMapping() throws IOException {
CreateIndexRequest createIndexRequest = new CreateIndexRequest(INDEX);
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
{
builder.startObject( "properties" );
{
builder.startObject( "name_suggest" );
{
builder.field( "type", "completion" );
}
builder.endObject();
}
builder.endObject();
}
builder.endObject();
createIndexRequest.mapping(TYPE,builder);
createIndexRequest.timeout(TimeValue.timeValueMinutes(2));
CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
return createIndexResponse.isAcknowledged();

}

关于java - 在 Java REST 客户端 [6.5] API 上使用 ES 6.5 中的映射创建索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54360057/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com