官方文檔地址:https://www.elastic.co/guide/en/elasticsearch/reference/index.html
權(quán)威指南:https://www.elastic.co/guide/cn/elasticsearch/guide/current/structured-search.html
1. 數(shù)據(jù)準(zhǔn)備
官方測(cè)試數(shù)據(jù)下載地址:https://download.elastic.co/demos/kibana/gettingstarted/accounts.zip ,數(shù)據(jù)量很大,我們自己構(gòu)造數(shù)據(jù)吧。
PUT /user/_doc/1
{
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests": [ "sports"]
}
PUT /user/_doc/2
{
"first_name" : "zhangsan",
"last_name" : "Smith",
"age" : 19,
"about" : "我是一個(gè)安靜的人",
"interests": [ "read" ]
}
PUT /user/_doc/3
{
"first_name" : "lisi",
"last_name" : "Alice",
"age" : 29,
"about" : "我喜歡規(guī)劃自己的生活",
"interests": [ "sports", "read","music" ]
}
2. match 匹配查詢
在匹配之前分析提供的文本,返回與提供的文本、數(shù)字、日期或布爾值匹配的文檔。 該match
查詢是執(zhí)行全文搜索的標(biāo)準(zhǔn)查詢,包括模糊匹配選項(xiàng)。
1. 全文檢索
① 底層分詞器會(huì)對(duì)查詢文本分詞,匹配about字段中含有g(shù)o或者含有rock的文檔:
GET /user/_search
{
"query": {
"match" : {
"about" : {
"query" : "go rock"
}
}
}
}
② 對(duì)應(yīng)的Java調(diào)用:
@Slf4j
@Service
public class ElasticSearchImpl {
@Autowired
private RestHighLevelClient restHighLevelClient;
public void searchUser() throws IOException {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("about","go rock");
searchSourceBuilder.query(matchQueryBuilder);
SearchRequest searchRequest = new SearchRequest(new String[]{"user"},searchSourceBuilder);
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(searchResponse);
}
}
③ 查詢的結(jié)果:
{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 7,
"successful" : 7,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.464436,
"hits" : [
{
"_index" : "user",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.464436,
"_source" : {
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests" : [
"sports"
]
}
}
]
}
}
2. 簡(jiǎn)化查詢DSL語(yǔ)句
GET /user/_search
{
"query": {
"match" : {
"about" : {
"query" : "我是一個(gè)"
}
}
}
}
可以簡(jiǎn)化查詢?yōu)椋?/p>
GET /user/_search
{
"query": {
"match" : {
"about" : "我是一個(gè)"
}
}
}
{
"took" : 25,
"timed_out" : false,
"_shards" : {
"total" : 7,
"successful" : 7,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 2.8478274,
"hits" : [
{
"_index" : "user",
"_type" : "_doc",
"_id" : "2",
"_score" : 2.8478274,
"_source" : {
"first_name" : "zhangsan",
"last_name" : "Smith",
"age" : 19,
"about" : "我是一個(gè)安靜的人",
"interests" : [
"read"
]
}
},
{
"_index" : "user",
"_type" : "_doc",
"_id" : "3",
"_score" : 0.6810339,
"_source" : {
"first_name" : "lisi",
"last_name" : "Alice",
"age" : 29,
"about" : "我喜歡規(guī)劃自己的生活",
"interests" : [
"sports",
"read",
"music"
]
}
}
]
}
}
3. match 匹配查詢?cè)?/h4>
查詢match
的類型為boolean
。這意味著對(duì)提供的文本進(jìn)行了分析,分析過(guò)程根據(jù)提供的文本構(gòu)造了一個(gè)布爾查詢。該operator
參數(shù)可以設(shè)置為or
或and
來(lái)控制布爾子句(默認(rèn)為or
),如果為and代表所有字段都要匹配,如果為or代表匹配分詞后的任何一個(gè)詞。
① 帶有operator參數(shù)and搜索條件,匹配的文檔需要包含文本的所有分詞,少一個(gè)都不行:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-403747.html
GET /user/_search
{
"query": {
"match" : {
"about" : {
"query" : "是一安靜個(gè)我",
"operator" : "and"
}
}
}
}
@Slf4j
@Service
public class ElasticSearchImpl {
@Autowired
private RestHighLevelClient restHighLevelClient;
public void searchUser() throws IOException {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("about","是一安靜個(gè)我");
// 設(shè)置Operator參數(shù)
matchQueryBuilder.operator(Operator.AND);
searchSourceBuilder.query(matchQueryBuilder);
SearchRequest searchRequest = new SearchRequest(new String[]{"user"},searchSourceBuilder);
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(searchResponse);
}
}
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 2.8478274,
"hits" : [
{
"_index" : "user",
"_type" : "_doc",
"_id" : "2",
"_score" : 2.8478274,
"_source" : {
"first_name" : "zhangsan",
"last_name" : "Smith",
"age" : 19,
"about" : "我是一個(gè)安靜的人",
"interests" : [
"read"
]
}
}
]
}
}
② 帶有operator參數(shù)or搜索條件,匹配的文檔需要包含文本的任意一個(gè)分詞:文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-403747.html
GET /user/_search
{
"query": {
"match" : {
"about" : "是一個(gè)我"
}
}
}
@Slf4j
@Service
public class ElasticSearchImpl {
@Autowired
private RestHighLevelClient restHighLevelClient;
public void searchUser() throws IOException {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("about","是一個(gè)我");
// 設(shè)置Operator參數(shù)
matchQueryBuilder.operator(Operator.OR);
searchSourceBuilder.query(matchQueryBuilder);
SearchRequest searchRequest = new SearchRequest(new String[]{"user"},searchSourceBuilder);
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(searchResponse);
}
}
{
"took" : 29,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 2.8478274,
"hits" : [
{
"_index" : "user",
"_type" : "_doc",
"_id" : "2",
"_score" : 2.8478274,
"_source" : {
"first_name" : "zhangsan",
"last_name" : "Smith",
"age" : 19,
"about" : "我是一個(gè)安靜的人",
"interests" : [
"read"
]
}
},
{
"_index" : "user",
"_type" : "_doc",
"_id" : "3",
"_score" : 0.6810339,
"_source" : {
"first_name" : "lisi",
"last_name" : "Alice",
"age" : 29,
"about" : "我喜歡規(guī)劃自己的生活",
"interests" : [
"sports",
"read",
"music"
]
}
}
]
}
}
到了這里,關(guān)于ElasticSearch系列 - SpringBoot整合ES之全文搜索匹配查詢 match的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!