全文搜索
- match
- operator 提高精度
- minimum_should_match 控制精度
- bool 组合查询
- must 必须匹配
- must_not 必须不匹配
- should 如果有 must 则表示没有必须匹配但有会更匹配,如果没有 must 则表示至少需要有一个匹配
- minimum_should_match 控制多少个 should 需要匹配
- boost 控制权重
示例数据
curl -X DELETE -H 'Content-Type: application/json' -k -u elastic:KJNQ2rMeC481nFwSsqyf "https://localhost:9200/my_index"
curl -X PUT -H 'Content-Type: application/json' -k -u elastic:KJNQ2rMeC481nFwSsqyf "https://localhost:9200/my_index" -d'
{
"settings": {
"number_of_shards": 1
}
}
'
curl -X POST -H 'Content-Type: application/json' -k -u elastic:KJNQ2rMeC481nFwSsqyf "https://localhost:9200/my_index/_bulk" -d'
{ "index": { "_id": 1 }}
{ "title": "The quick brown fox" }
{ "index": { "_id": 2 }}
{ "title": "The quick brown fox jumps over the lazy dog" }
{ "index": { "_id": 3 }}
{ "title": "The quick brown fox jumps over the quick dog" }
{ "index": { "_id": 4 }}
{ "title": "Brown fox brown dog" }
'
curl -X GET -H 'Content-Type: application/json' -k -u elastic:KJNQ2rMeC481nFwSsqyf "https://localhost:9200/my_index/_search?pretty"
单个词查询
curl -X GET -H 'Content-Type: application/json' -k -u elastic:KJNQ2rMeC481nFwSsqyf "https://localhost:9200/my_index/_search?pretty" -d'
{
"query": {
"match": {
"title": "quick"
}
}
}
'
多个词查询
匹配 brown 或 dog
curl -X GET -H 'Content-Type: application/json' -k -u elastic:KJNQ2rMeC481nFwSsqyf "https://localhost:9200/my_index/_search?pretty" -d'
{
"query": {
"match": {
"title": "brown dog"
}
}
}
'
提高精度
必须同时匹配 brown 和 dog (operator的默认值是 or)
curl -X GET -H 'Content-Type: application/json' -k -u elastic:KJNQ2rMeC481nFwSsqyf "https://localhost:9200/my_index/_search?pretty" -d'
{
"query": {
"match": {
"title": {
"query": "brown dog",
"operator": "and"
}
}
}
}
'
控制精度
minimum_should_match 支持数值,表示需要匹配的词项数;或者百分比值
curl -X GET -H 'Content-Type: application/json' -k -u elastic:KJNQ2rMeC481nFwSsqyf "https://localhost:9200/my_index/_search?pretty" -d'
{
"query": {
"match": {
"title": {
"query": "quick brown dog",
"minimum_should_match": "75%"
}
}
}
}
'
组合查询
必须包含 quick,必须不包含 lazy,如果包含 brown dog 相关度会更高
curl -X GET -H 'Content-Type: application/json' -k -u elastic:KJNQ2rMeC481nFwSsqyf "https://localhost:9200/my_index/_search?pretty" -d'
{
"query": {
"bool": {
"must": {"match": {"title": "quick"}},
"must_not": {"match": {"title": "lazy"}},
"should": [
{"match": {"title": "brown"}},
{"match": {"title": "dog"}}
]
}
}
}
'
控制精度
minimum_should_match 至少有多少个 should 需要匹配
curl -X GET -H 'Content-Type: application/json' -k -u elastic:KJNQ2rMeC481nFwSsqyf "https://localhost:9200/my_index/_search?pretty" -d'
{
"query": {
"bool": {
"should": [
{"match": {"title": "brown"}},
{"match": {"title": "fox"}},
{"match": {"title": "dog"}}
],
"minimum_should_match": 2
}
}
}
'
提升权重
必须包含 brown dog,如果有 quick fox 会更匹配
curl -X GET -H 'Content-Type: application/json' -k -u elastic:KJNQ2rMeC481nFwSsqyf "https://localhost:9200/my_index/_search?pretty" -d'
{
"query": {
"bool": {
"must": {
"match": {
"title": {
"query": "brown dog",
"operator": "and"
}
}
},
"should": [
{"match": {"title": "quick"}},
{"match": {"title": "fox"}}
]
}
}
}
'
必须包含 brown dog,如果有 quick fox 会更匹配;quick 权重更高,fox 权重比默认高(默认为1)
boost == 1 默认,boost > 1 权重更高,boost > 0 and boost < 1 权重更低
curl -X GET -H 'Content-Type: application/json' -k -u elastic:KJNQ2rMeC481nFwSsqyf "https://localhost:9200/my_index/_search?pretty" -d'
{
"query": {
"bool": {
"must": {
"match": {
"title": {
"query": "brown dog",
"operator": "and"
}
}
},
"should": [
{"match": {"title": {"query": "quick", "boost": 2}}},
{"match": {"title": {"query": "fox", "boost": 1}}}
]
}
}
}
'
标签:index,brown,title,dog,搜索,query,Elasticsearch,全文,match
From: https://www.cnblogs.com/liaozibo/p/18441551