全文搜索
# 示例
POST /my_index/my_type/_bulk
{ "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" }
# 单个词查询
GET /my_index/_search
{
"query": {
"match": {
"title": "QUICK"
}
}
}
# 多词查询
# 匹配brown or dog
GET /my_index/_search
{
"query": {
"match": {
"title": "BROWN DOG"
}
}
}
# 匹配brown and dog
GET /my_index/_search
{
"query": {
"match": {
"title": {
"query": "BROWN DOG",
"operator": "and"
}
}
}
}
# 匹配三个词中的任意两个
GET /my_index/_search
{
"query": {
"match": {
"title": {
"query": "brown lazy dog",
"minimum_should_match": "67%"
}
}
}
}
# 组合查询
# must_not、filter不影响评分,只是排除文档
GET /my_index/_search
{
"query": {
"bool": {
"must": [
{"match": {
"title": "quick"
}}
],
"must_not": [
{"match": {
"title": "lazy"
}}
],
"should": [
{"match": {
"title": "brown"
}},
{"match": {
"title": "dog"
}}
]
}
}
}
# 控制should匹配的数量
# 至少匹配两项
GET /my_index/_search
{
"query": {
"bool": {
"should": [
{"match": {
"title": "brown"
}},
{"match": {
"title": "fox"
}},
{"match": {
"title": "dog"
}}
],
"minimum_should_match": 2
}
}
}
# 查询语句提升权重
GET /_search
{
"query": {
"bool": {
"must": [
{"match": {
"title": {
"query": "银行",
"operator": "and"
}
}}
],
"should": [
{"match": {
"title": {
"query": "农商",
"boost": 1
}
}},
{"match": {
"attachment.content": {
"query": "农商",
"boost": 0.5
}
}}
]
}
},
"_source": ["title","attachment.content"]
}