1. 什么是 ElasticSearch 的 multi_match 查詢?
有時(shí)用戶需要在多個(gè)字段中查詢關(guān)鍵詞,除了使用布爾查詢封裝多個(gè)match查詢之外,可替代的方案是使用multi_match??梢栽趍ulti_match的query子句中組織數(shù)據(jù)匹配規(guī)則,并在fields子句中指定需要搜索的字段列表。
以下是一個(gè)示例multi-match查詢的語(yǔ)法:
{
"query": {
"multi_match": {
"query": "搜索文本",
"fields": ["字段1", "字段2", "字段3"]
}
}
}
在上面的查詢中,我們指定了要搜索的文本和要搜索的字段列表。ElasticSearch將搜索所有指定的字段,并將它們合并為一個(gè)結(jié)果集。
2. 如何在 multi_match 查詢中指定查詢字段?
在 Elasticsearch 的 multi_match 查詢中,可以使用 “fields” 參數(shù)來(lái)指定要查詢的字段。該參數(shù)接受一個(gè)數(shù)組,其中包含要查詢的字段名稱。
① 構(gòu)造數(shù)據(jù):
PUT /my_index
{
"mappings": {
"properties": {
"title":{
"type": "text"
},
"content":{
"type": "text"
}
}
}
}
PUT /my_index/_doc/1
{
"title": "Jindu Fashion Couple Romantic Theme Hotel",
"content": "Qingdao City"
}
PUT /my_index/_doc/2
{
"title": "Jindu Jiayi Holiday Inn",
"content": "Beijing City"
}
PUT /my_index/_doc/3
{
"title": "Jindu Xinxin 24 hour Hotel",
"content": "Huaibei City"
}
② 以下查詢將在 “title” 和 “content” 字段中搜索包含 “Beijing” 和 “Xinxin” 的文檔:
{
"query": {
"multi_match": {
"query": "Beijing Xinxin",
"fields": ["title", "content"]
}
}
}
如果未指定 “fields” 參數(shù),則默認(rèn)情況下將在所有字段中搜索。
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.9808292,
"hits" : [
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "2",
"_score" : 0.9808292,
"_source" : {
"title" : "Jindu Jiayi Holiday Inn",
"content" : "Beijing City"
}
},
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "3",
"_score" : 0.9808292,
"_source" : {
"title" : "Jindu Xinxin 24 hour Hotel",
"content" : "Huaibei City"
}
}
]
}
}
3. 如何在 multi_match 查詢中指定查詢權(quán)重?
在 Elasticsearch 的 multi_match 查詢中,可以使用 “fields” 參數(shù)來(lái)指定要搜索的字段,并使用 “^” 符號(hào)來(lái)指定每個(gè)字段的權(quán)重。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-420555.html
例如:“title” 的權(quán)重為 5,“content” 的權(quán)重為 2。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-420555.html
GET /my_index/_search
{
"query": {
"multi_match": {
"query": "Beijing Xinxin",
"fields": ["title^5","content^2"]
}
}
}
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 4.904146,
"hits" : [
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "3",
"_score" : 4.904146,
"_source" : {
"title" : "Jindu Xinxin 24 hour Hotel",
"content" : "Huaibei City"
}
},
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.9616584,
"_source" : {
"title" : "Jindu Jiayi Holiday Inn",
"content" : "Beijing City"
}
}
]
}
}
4. SpringBoot整合ES實(shí)現(xiàn) multi_match 查詢
@Slf4j
@Service
public class ElasticSearchImpl {
@Autowired
private RestHighLevelClient restHighLevelClient;
public void searchUser() throws IOException {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery("Beijing Xinxin", "title", "content");
searchSourceBuilder.query(multiMatchQueryBuilder);
SearchRequest searchRequest = new SearchRequest(new String[]{"hotel"},searchSourceBuilder);
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(searchResponse);
}
}
到了這里,關(guān)于ElasticSearch系列 - SpringBoot整合ES:多字段查詢 multi_match的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!