简体   繁体   中英

Minimum should match with filter doesn't return any result

I have a complicated query which works fine.the proble is that I'm going to add a condition(filter) to it to filter the result.I need the exact result that I currently get with filtering based on the field called "field7".

"query": {
    "bool": {
        "should": [
            {
                "match_bool_prefix": {
                    "field1": {
                        "query": "test",
                        "fuzziness": "auto",
                        "boost": 1
                    }
                }
            },
            {
                "match": {
                    "field2": {
                        "query": "test",
                        "boost": 10
                    }
                }
            },
            {
                "exists": {
                    "field": "field3",
                    "boost": 15
                }
            },
            {
                "exists": {
                    "field": "field4",
                    "boost": 10
                }
            },
            {
                "match_phrase_prefix": {
                    "field5": {
                        "query": ""
                    }
                }
                }
               
        ],
        "must": [
            {
                "bool": {
                    "filter": [
                      {
                        "match": {
                          "field6": "A"
                        }
                      },
                      {"terms": { "field7": [3,4,5]}}
                    ]
                }
               
            }
           
        ],
          "minimum_should_match": 3
    }
},
"size": 20

I have to use "minimum_should_match": 3,to meet my requirements(If i remove it I get unrelated results) but when i use it with filter the result gets notthing.Is there any suggestion how to get current result and filter it based on field7?

@Paris I believe you can use filter term query for field7 since you want to apply filter on the result-set from should+must query. So basically this should suffice:

"query": {
  "bool": {
    "should": [
        {
            "match_bool_prefix": {
                "field1": {
                    "query": "test",
                    "fuzziness": "auto",
                    "boost": 1
                }
            }
        },
        {
            "match": {
                "field2": {
                    "query": "test",
                    "boost": 10
                }
            }
        },
        {
            "exists": {
                "field": "field3",
                "boost": 15
            }
        },
        {
            "exists": {
                "field": "field4",
                "boost": 10
            }
        },
        {
            "match_phrase_prefix": {
                "field5": {
                    "query": ""
                }
            }
            }
           
    ],
    "must": {
            {"match": {"field6": "A"}}
     },
    "filter": {
        {"term" : {"field7" : 3}},
        {"term" : {"field7" : 4}},
        {"term" : {"field7" : 5}},
      }
   }
},
"size": 20

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM