ElasticSearch - Match or less

is there an way to query the following using elasticsearch (version 2) :

indexed documents examples :

{_id=1 , "tags": [ "t1"]}

{_id=2 , "tags": [ "t1", "t2" ]}

{_id=3 , "tags": [ "t1", "t2", "t3" ]}

The following are query examples and expected results :

"tags": ["t1"] ---> document _ids=1

"tags": ["t1","t2"] ---> document _ids=1,2 (document #1 is expected too)

"tags": ["t1","t2","t3"] ---> document _ids=1,2,3

"tags": ["t2","t3"] ---> document _ids=NONE

"tags": ["t1","t3"] ---> document _ids=1 only

PUT my-index-000001/_doc/1
{  "tags":  [ "t1"]}

PUT my-index-000001/_doc/2
{  "tags":  [ "t1", "t2" ]}

PUT my-index-000001/_doc/3
{  "tags":  [ "t1", "t2", "t3" ]}

The following query yield more than expected and include document #3 too in addition to #1&#2

GET my-index-000001/_search
{
  "query": {
    "match": {
      "tags": ["t1","t2"] 
    }
  }
}

And on the following, document #3 is not matched but either the expected #1

GET metric_*/_search
{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "tags": "t1"
                    }
                },
                {
                    "match": {
                        "tags": "t2"
                    }
                }
            ]
        }
    }
}

I need a way exclude any document that has tag value that is not exist in the query tag list

thanks in advance

I do not think I can find any way of doing this in Elasticsearch 2.x, but in recent versions (7.x) I suspect you might be able do this through a terms set query if you also index the size of the array in the document and use this as a minimum_should_match_field field.

taking the blacklist approach solve the issue (using must_not). since we know the full list of options in the tag field and it's relatively small.

query example :

GET tags/_search
{
    "query": {
        "bool": {
            "must_not": {
                "terms": {
                    "orgSourceIds": [
                        "t1",
                        "t2"
                    ]
                }
            }
        }
    }
}

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.