gpt4 book ai didi

java - 缺少SuggestRequestBuilder.java/org.elasticsearch.action.suggest

转载 作者:行者123 更新时间:2023-12-03 02:19:10 26 4
gpt4 key购买 nike

我正在尝试将Elasticsearch spring boot项目从1.5.7升级到2.2.1。我在项目中仅有的依赖项(与 Elasticsearch 有关)是spring-data-Elasticsearch。但是,像SuggestRequestBuilder这样的缺少/过时的类很少,我的构建失败了。我在google中也找到了合适的替代方法。谁能帮助我找到使用这些suggestRequestBuildersuggestionResponse的替代方法。

最佳答案

通过spring-boot 2.3.1和spring-boot-starter-data-elasticsearch,我使用了Suggest*包中的org.elasticsearch.search.suggest.*类。
示例:
ES索引中添加了一些产品:

{
"_index": "products",
"_id": "WtYOV3MBurwvUvNLpjxM",
"_source": {
"productName": "Hammer",
"available": true
}
},
{
"_index": "products",
"_id": "XtYdV3MBurwvUvNLXjzO",
"_source": {
"productName": "Hammer with handle",
"available": true
}
}
现在,我不想根据 productName字段建议使用单词“hamer”。这是找到正确建议的完整代码:
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.suggest.Suggest;
import org.elasticsearch.search.suggest.SuggestBuilder;
import org.elasticsearch.search.suggest.SuggestBuilders;
import org.elasticsearch.search.suggest.SuggestionBuilder;
import org.elasticsearch.search.suggest.term.TermSuggestion;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.stereotype.Component;
import ...;

@Component
public class ProductService {

private static final Logger logger = getLogger(ProductService.class);

public void suggestProducts() {

ClientConfiguration clientConfiguration =
ClientConfiguration.builder().connectedTo("localhost:9200").build();
RestHighLevelClient client = RestClients.create(clientConfiguration).rest();

SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
SuggestionBuilder termSuggestionBuilder =
SuggestBuilders.termSuggestion("productName").text("Hamer");
SuggestBuilder suggestBuilder = new SuggestBuilder();
suggestBuilder.addSuggestion("suggest_product", termSuggestionBuilder);
searchSourceBuilder.suggest(suggestBuilder);

SearchRequest searchRequest = new SearchRequest();
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse;
try {
searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
Suggest suggest = searchResponse.getSuggest();
TermSuggestion termSuggestion = suggest.getSuggestion("suggest_product");
for (TermSuggestion.Entry entry : termSuggestion.getEntries()) {
for (TermSuggestion.Entry.Option option : entry) {
String suggestText = option.getText().string();
logger.info(suggestText);
}
}
} catch (IOException e) {
logger.error(e.getMessage());
}
}
}
并打印出正确的结果:“锤子”。当然,还有其他类型的建议( termSuggestioncompletionSuggestionphraseSuggestion),您可以灵活地调整查询的参数。

关于java - 缺少SuggestRequestBuilder.java/org.elasticsearch.action.suggest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62910631/

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