以下是我的弹性查询
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作为最高得分。我还希望在1620 - 1740之间包含marriages.marriage_year
,这些未显示在结果中。它仅显示marriage_year
1630
答案 0 :(得分:2)
那是因为你有两个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"
}
}
]
}
}
}