ElasticSearch should/must 子句未按预期工作
ElasticSearch should/must clause not working as expected
下面是我的弹性查询
GET _search
{
"query": {
"bool": {
"must": {
"match": {
"marriages.marriage_year": "1630"
}
},
"should": {
"match": {
"first_name": {
"query": "mary",
"fuzziness": "2"
}
}
},
"must": {
"range": {
"marriages.marriage_year": {
"gt": "1620",
"lte": "1740"
}
}
}
}
}
}
返回的数据为 marriages.marriage_year= "1630"
,Mary 为 first_name 最高 score.I 还想包括 marriages.marriage_year
在 1620 - 1740 之间,结果中未显示.它仅显示 marriage_year
1630
的数据
那是因为您有两个 bool/must
子句,而第二个子句在解析 JSON 查询时被删除。改为这样重写它,它将起作用:
{
"query": {
"bool": {
"must": [
{
"match": {
"marriages.marriage_year": "1630"
}
},
{
"range": {
"marriages.marriage_year": {
"gt": "1620",
"lte": "1740"
}
}
}
],
"should": {
"match": {
"first_name": {
"query": "mary",
"fuzziness": "2"
}
}
}
}
}
}
更新
然后你需要做不同的事情,在 bool/must
中你只需要 range
查询并将 match
移动到 bool/should
部分:
{
"query": {
"bool": {
"must": [
{
"range": {
"marriages.marriage_year": {
"gt": "1620",
"lte": "1740"
}
}
}
],
"should": [
{
"match": {
"first_name": {
"query": "mary",
"fuzziness": "2"
}
}
},
{
"match": {
"marriages.marriage_year": "1630"
}
}
]
}
}
}
下面是我的弹性查询
GET _search
{
"query": {
"bool": {
"must": {
"match": {
"marriages.marriage_year": "1630"
}
},
"should": {
"match": {
"first_name": {
"query": "mary",
"fuzziness": "2"
}
}
},
"must": {
"range": {
"marriages.marriage_year": {
"gt": "1620",
"lte": "1740"
}
}
}
}
}
}
返回的数据为 marriages.marriage_year= "1630"
,Mary 为 first_name 最高 score.I 还想包括 marriages.marriage_year
在 1620 - 1740 之间,结果中未显示.它仅显示 marriage_year
1630
那是因为您有两个 bool/must
子句,而第二个子句在解析 JSON 查询时被删除。改为这样重写它,它将起作用:
{
"query": {
"bool": {
"must": [
{
"match": {
"marriages.marriage_year": "1630"
}
},
{
"range": {
"marriages.marriage_year": {
"gt": "1620",
"lte": "1740"
}
}
}
],
"should": {
"match": {
"first_name": {
"query": "mary",
"fuzziness": "2"
}
}
}
}
}
}
更新
然后你需要做不同的事情,在 bool/must
中你只需要 range
查询并将 match
移动到 bool/should
部分:
{
"query": {
"bool": {
"must": [
{
"range": {
"marriages.marriage_year": {
"gt": "1620",
"lte": "1740"
}
}
}
],
"should": [
{
"match": {
"first_name": {
"query": "mary",
"fuzziness": "2"
}
}
},
{
"match": {
"marriages.marriage_year": "1630"
}
}
]
}
}
}