作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将Elasticsearch spring boot项目从1.5.7升级到2.2.1。我在项目中仅有的依赖项(与 Elasticsearch 有关)是spring-data-Elasticsearch。但是,像SuggestRequestBuilder
这样的缺少/过时的类很少,我的构建失败了。我在google中也找到了合适的替代方法。谁能帮助我找到使用这些suggestRequestBuilder
,suggestionResponse
的替代方法。
最佳答案
通过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());
}
}
}
并打印出正确的结果:“锤子”。当然,还有其他类型的建议(
termSuggestion
,
completionSuggestion
,
phraseSuggestion
),您可以灵活地调整查询的参数。
关于java - 缺少SuggestRequestBuilder.java/org.elasticsearch.action.suggest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62910631/
我是一名优秀的程序员,十分优秀!