本篇文章是从本人为知笔记上誊抄过来的,文章内容最初是当作笔记进行整理的,没有组织语言结构,各位看官自行查阅取舍
1、引入依赖
<properties>
<elasticsearch.version>7.6.2</elasticsearch.version>
</properties>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
测试代码:
@Test
public void test() throws IOException {
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));
SearchRequest request = new SearchRequest("sales");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
// 1、去某个字段匹配关键字
sourceBuilder.query(new MatchQueryBuilder("name", "Katherine"));
// 2、添加排序,排序包含:Field-, Score-, GeoDistance- and ScriptSortBuilder四类,这里是按score排序,结果不详叙
sourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.ASC));
// fetchSource,意思即想获取哪些参数,第一个参数为结果(source)中想获取的参数,第二个为排除的参数, 参数也可以是false,证明返回的结果中不包含source
// sourceBuilder.fetchSource(false); // 即获取结果的时候不返回source值
sourceBuilder.fetchSource(new String[]{"name"}, Strings.EMPTY_ARRAY);
// 3、高亮搜索关键字
HighlightBuilder highlightBuilder = new HighlightBuilder();
HighlightBuilder.Field highLighterName = new HighlightBuilder.Field("name");
highlightBuilder.field(highLighterName);
sourceBuilder.highlighter(highlightBuilder);
// 4、搜索建议,比如搜索的名字应该是zhangsan,但是打成了zhnagsan,,类似于百度搜索时提示:你是不是找的是zhangsan?
TermSuggestionBuilder termSuggestionBuilder = SuggestBuilders.termSuggestion("name").text("Katheirne");
SuggestBuilder suggestBuilder = new SuggestBuilder();
suggestBuilder.addSuggestion("suggest_name", termSuggestionBuilder);
sourceBuilder.suggest(suggestBuilder);
// 高亮搜索获取高亮标识的结果
for (SearchHit hit : resp.getHits().getHits()) {
Map<String, HighlightField> highlightFields = hit.getHighlightFields();
HighlightField highlightField = highlightFields.get("name");
System.out.println(highlightField.fragments()[0].toString());
}
request.source(sourceBuilder);
SearchResponse resp = client.search(request, RequestOptions.DEFAULT);
System.out.println(Arrays.toString(resp.getHits().getHits()));
System.out.println(resp.getSuggest());
}
对应代码中注释的执行结果如下:
- 查询关键字的结果:
[{
"_index" : "sales",
"_type" : "_doc",
"_id" : "rWip1XIBm1i_zZ64qBcU",
"_score" : 5.2027473,
"_source" : {
"transaction_date" : "1/27/09 15:28",
"account_created" : "1/17/09 17:52",
"name" : "Katherine",
"state" : "NM",
"@timestamp" : "2020-06-21T06:56:03.650Z",
"@version" : "1",
"country" : "United States",
"payment_type" : "Visa",
"city" : "Corrales ",
"price" : "1200",
"product" : "Product1",
"location" : {
"lon" : "-106.60611",
"lat" : "35.23778"
},
"last_login" : "1/27/09 15:17"
}
}, {
"_index" : "sales",
"_type" : "_doc",
"_id" : "oWip1XIBm1i_zZ64qBcT",
"_score" : 5.2027473,
"_source" : {
"transaction_date" : "1/11/09 10:51",
"account_created" : "12/6/08 1:34",
"name" : "Katherine",
"state" : "Hessen",
"@timestamp" : "2020-06-21T06:56:03.630Z",
"@version" : "1",
"country" : "Germany",
"payment_type" : "Mastercard",
"city" : "Hofheim am Taunus",
"price" : "1200",
"product" : "Product1",
"location" : {
"lon" : "8.45",
"lat" : "50.0833333"
},
"last_login" : "1/18/09 7:17"
}
}
.......
]
- 搜索建议的结果:
resp.getSuggest()
{
"suggest" : {
"suggest_name" : [
{
"text" : "katheirne",
"offset" : 0,
"length" : 9,
"options" : [
{
"text" : "katherine",
"score" : 0.8888889,
"freq" : 5
}
]
}
]
}
}
- 高亮搜索关键字:会多一个
highlight
项
{
"_index" : "sales",
"_type" : "_doc",
"_id" : "rWip1XIBm1i_zZ64qBcU",
"_score" : 5.2027473,
"_source" : {
"transaction_date" : "1/27/09 15:28",
"account_created" : "1/17/09 17:52",
"name" : "Katherine",
"state" : "NM",
"@timestamp" : "2020-06-21T06:56:03.650Z",
"@version" : "1",
"country" : "United States",
"payment_type" : "Visa",
"city" : "Corrales ",
"price" : "1200",
"product" : "Product1",
"location" : {
"lon" : "-106.60611",
"lat" : "35.23778"
},
"last_login" : "1/27/09 15:17"
},
"highlight" : {
"name" : [
"<em>Katherine</em>"
]
}
}
高亮搜索的结果:用<em></em>进行了标识
<em>Katherine</em>
<em>Katherine</em>
<em>Katherine</em>
<em>Katherine</em>
philippa <em>katherine</em>