title: ElasticSearch search through an array field as exclusive search
body: <p>I do have an array of data in a field in ElasticSearch with a keyword type. I want to search this array with exclusive values that I want to search i.e. to exclude array values thats not included in my search keyword. Please see the details below.</p>
  
  <p>Thanks!</p>
  
  <p>I have the following elastic search index mapping:</p>
  
  <pre><code>"exgroups": {
    "type": "keyword",
    "eager_global_ordinals": true
  },
  </code></pre>
  
  <p>With the following sample data:</p>
  
  <pre><code>"id": 1,
  "exgroups": ["TSX"]
  
  "id": 2,
  "exgroups": ["TSX", "OTC", "NSD"]
  </code></pre>
  
  <p>My search is like this:</p>
  
  <pre><code>{
    "bool" : {
      "filter" : {
  
          "term" : {
            "exgroups" : {
              "value" : "TSX"
            }
          }
  
      }
    }
  }
  </code></pre>
  
  <p>I've used MatchQueryBuilder, TermQueryBuilder, TermsQueryBuilder to no avail. By the ElasticSearch TermQuery definition, it should do the trick. <a href="https://www.elastic.co/guide/en/elasticsearch/reference/6.2/query-dsl-term-query.html" rel="nofollow noreferrer">https://www.elastic.co/guide/en/elasticsearch/reference/6.2/query-dsl-term-query.html</a>. But it does not, probably because the field is an array.</p>
  
  <p>In general, the Term*Query behaves like this:</p>
  
  <pre><code>iterate all the documents, for each document
    check if the exgroups contains 'tsx'
    if it does, return the document
  </code></pre>
  
  <p>This returns documents 1 and 2 since document 2 contains TSX also. However, I wanted for it to return <em>only</em> document 1 and no other else in the array.</p>
  
  <p>How do I accomplish this?</p>
  
  <p>Thanks in advance.</p>