简体   繁体   中英

Minimum Number Should Match, Should match with POST

I'm trying to query an elasticsearch 5.2 rest endpoint:

{
    "query": {
        "bool": {
            "should": [
                {"match": { "file-source": "css" }},
                {"match": { "file-source": "js" }}
            ],
            "minimum_number_should_match": 2
        }
    }
}

However, this returns NO HITS in ElasticSearch, I don't understand why this doesn't work and the ElasticSearch documentation has very few examples.

I'm trying to match both. But it could be either OR.

There is file-source: "css", file-source: "js", file-source: "json"

But I want an option of BOTH CSS AND JS...

Here's some example Java code when testing the same with Java API:

// conditions met from the form
query.should(QueryBuilders.matchQuery("file-source", "css"));
query.should(QueryBuilders.matchQuery("file-source", "js"));
query.minimumNumberShouldMatch(2);

Lets explain the given query.

"should": [
            {"match": { "file-source": "css" }},
            {"match": { "file-source": "js" }}
        ]

This will return all the documents that have file-source as css or js.

      A = <documents with file-source = css or jss>

     "minimum_number_should_match": 2
      B = A <documents with file-source at least 2 of css, jss>

This will narrow the results and will restrict A to the documents that has both css and js. Remember "css OR js" contains "css and js" as well. But here in your case it seems like each doc has unique file source so A will become an empty set as there is no "css and js".

In this case B becomes empty as there is no document that have file-source = css and jss

Try removing - "minimum_number_should_match": 2 . It should work.

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