0

All my documents have a field, tags of type Array. I want to search and return all the documents that have an intersection of tags with a user-input array. The number of elements is variable, not a fixed size.

Examples:
tags:["python", "flask", "gunicorn"]
input:["python"]

  • This would return true because all the elements in input is in tags.

tags:["nginx", "pm2"]
input:["nodejs", "nginx", "pm2", "microservice"]

  • This would return false because "nodejs" and "microservice" is not in tags.

I looked into terms query but I do not think it works for arrays.

I also found this, Elasticsearch array property must contain given array items, but the solution is for old versions of Elasticsearch and the syntax has changed.

Hid
  • 533
  • 6
  • 18

1 Answers1

0

I believe you're looking for a terms_set - reference: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-set-query.html

PUT tags

POST tags/_doc
{
  "tags": ["python", "flask", "gunicorn"]
}

POST tags/_doc
{
  "tags": ["nginx", "pm2"]
}

GET tags/_search
{
  "query": {
    "terms_set": {
      "tags": {
        "terms": ["nginx", "pm2"],
        "minimum_should_match_script": {
          "source": "params.num_terms"
        }
      }
    }
  }
}

Returned:

  "hits" : {
    "total" : 1,
    "max_score" : 0.5753642,
    "hits" : [
      {
        "_index" : "tags",
        "_type" : "_doc",
        "_id" : "XZqN_mkB94Kxh8PwtQs_",
        "_score" : 0.5753642,
        "_source" : {
          "tags" : [
            "nginx",
            "pm2"
          ]
        }
      }
    ]
  }

Querying the full list in your example:

GET tags/_search
{
  "query": {
    "terms_set": {
      "tags": {
        "terms": ["nodejs", "nginx", "pm2", "microservice"],
        "minimum_should_match_script": {
          "source": "params.num_terms"
        }
      }
    }
  }
}

Yields no results, as expected:

  "hits" : {
    "total" : 0,
    "max_score" : null,
    "hits" : [ ]
  }
James Pittiglio
  • 551
  • 3
  • 7
  • Thank you, that worked. I cannot believe I could not find that piece of documentation with the huge load of googling I have done. – Hid Apr 09 '19 at 02:54