学习笔记 基于SpringBoot整合Elasticsearch实现搜索补全功能
学习笔记 基于SpringBoot整合Elasticsearch实现搜索补全功能
一、概述
搜索智能提示,像百度、淘宝、京东等在搜索内容会自动补全字段
主要是 怕面试被问到es相关 了解了一下
二、前提
这是我的版本贴一下:
- SpringBoot:2.5.12
- Elasticsearch: 7.7.0
三、实现过程
DOC实体类
@Data
@Document(indexName = "es_test")
@NoArgsConstructor
@AllArgsConstructor
public class EsTest{
@Id
@Field(type = FieldType.Keyword)
private String id;
@Field(analyzer = "standard", type = FieldType.Text)
private String name;
@Field(type = FieldType.Integer)
private Integer age;
@CompletionField(analyzer="ik_smart",searchAnalyzer="ik_smart", maxInputLength = 100)
private Completion suggest;
public EsTest(String id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
}
集成ElasticsearchRepository调用ES api
/**
* @author 谢宗佑
*/
public interface EsTestRepository extends ElasticsearchRepository<EsTest, String> {
}
然后咧创建一个测试类进行测试
@SpringBootTest
class EsTestApplicationTests {
@Autowired
private EsTestRepository esTestRepository;
@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;
@Autowired
private ElasticsearchOperations elasticsearchOperations;
@Test
void test01() {
List<EsTest> esTests = new ArrayList<>();
EsTest esTest01 = new EsTest("1", "谢宗佑1号我是谢宗佑", 18);
List<String> suggestList = new ArrayList<>();
suggestList.add("谢宗佑1号我是谢宗佑");
esTest01.setSuggest(new Completion(suggestList.toArray(new String[suggestList.size()])));
esTests.add(esTest01);
EsTest esTest02 = new EsTest("2", "谢宗佑我不是谢宗佑", 19);
List<String> suggestList01 = new ArrayList<>();
suggestList01.add("谢宗佑我不是谢宗佑");
esTest02.setSuggest(new Completion(suggestList01.toArray(new String[suggestList01.size()])));
esTests.add(esTest02);
EsTest esTest03 = new EsTest("3", "谢宗佑yyds", 20);
List<String> suggestList02 = new ArrayList<>();
suggestList02.add("谢宗佑yyds");
esTest03.setSuggest(new Completion(suggestList02.toArray(new String[suggestList02.size()])));
esTests.add(esTest03);
EsTest esTest04 = new EsTest("4", "谢宗佑佑佑佑佑", 21);
List<String> suggestList03 = new ArrayList<>();
suggestList03.add("谢宗佑佑佑佑佑");
esTest04.setSuggest(new Completion(suggestList03.toArray(new String[suggestList03.size()])));
esTests.add(esTest04);
esTestRepository.saveAll(esTests);
}
@Test
void test02() {
// 使用suggest进行标题联想
CompletionSuggestionBuilder suggest = SuggestBuilders.completionSuggestion("suggest")
// 关键字(参数传此)
.prefix("谢宗佑")
// 重复过滤
.skipDuplicates(true)
// 匹配数量
.size(10);
SuggestBuilder suggestBuilder = new SuggestBuilder();
suggestBuilder.addSuggestion("my-suggest", suggest);
IndexCoordinates indexCoordinates = elasticsearchOperations.getIndexCoordinatesFor(EsTest.class);
// 查询
SearchResponse goodsNameSuggestResp = elasticsearchRestTemplate.suggest(suggestBuilder, indexCoordinates);
Suggest.Suggestion<? extends Suggest.Suggestion.Entry<? extends Suggest.Suggestion.Entry.Option>> goodsNameSuggest = goodsNameSuggestResp
.getSuggest().getSuggestion("my-suggest");
// 处理返回
List<String> suggests = goodsNameSuggest.getEntries().stream().map(x -> x.getOptions().stream().map(y -> y.getText().toString()).collect(Collectors.toList())).findFirst().get();
// 输出内容
for (String s : suggests) {
System.out.println("suggest = " + s);
}
}
}
test01方法呢,就是将数据插入至ES中,来方便后面实现搜索补全功能
然后呢就是对搜索进行字段补全
test02方法就是对查询进行补全 打印的字段为补全的字段