title: Elasticsearch: get all documents where array contains one of many values
body: <p>I have the following document data structure in Elasticsearch:</p>
  
  <pre><code>{
      "topics": [ "a", "b", "c", "d" ]
  }
  </code></pre>
  
  <p>I have a selection list where the user can filter which topics to show. When the user is OK with their filter, they will be presented with all documents that have any of the topics they selected in the array "topics"</p>
  
  <p>I've tried the query</p>
  
  <pre><code>{
      "query": {
          "terms": {
               "topics": ["a", "b"]
           }
      }
  }
  </code></pre>
  
  <p>but this returns no results.</p>
  
  <p>To expand on the query. For example, the list <code>["a", "b"]</code> would match the first, second and third objects in the array below.</p>
  
  <p><a href="https://i.stack.imgur.com/RBTBM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RBTBM.png" alt="Example"></a></p>
  
  <p>Is there a good way to do this in Elasticsearch? Obviously I could do multiple "match" queries but that's verbose as I have hundreds of topics</p>
  
  <p>Edit: my mapping</p>
  
  <pre><code>{
      "fb-cambodia-post": {
          "mappings": {
              "scrapedpost": {
                  "properties": {
                      "topics": {
                          "type": "text",
                          "fields": {
                              "keyword": {
                                  "type": "keyword",
                                  "ignore_above": 256
                              }
                          }
                      },
                  }
              }
          }
      }
  }
  </code></pre>