0
votes

I have an index of documents. The index contains body of documents and the type of document e.g pdf, jpeg, png etc. I can query the index with a word and one document type using must just fine.

                $params = [
                'index' => 'trial2',
                'type' => '_doc',
                'body' => [
                  'query' => [
                    'bool' => [
                      'must' => [
                        [ 'match' => [ 'file.extension' => "png" ] ],
                        [ 'match' => [ 'content' => "abc" ] ],
                      ]
                    ]
                  ]
                ]
              ];

screenshot The challenge is, I would like to query the index still using must but with an array of document type (png but jpeg, gif, svg, tiff) so that I classify it as an image. How do I replace png with an array so that at lease one is true.

2

2 Answers

0
votes

You can use a "terms" query:

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html

The terms query returns documents that contain one or more exact terms in a provided field.

0
votes

If file.extension is of text type:

Just add more tokens next to Match Query. Make sure you go through this (Analysis) and to this (Analyzer) to understand how it works internally.

POST my_png/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "file.extension": "jpg jpeg png"        <---- Note this.
          }
        },
        {
          "match": {
            "content": "abc"
          }
        }
      ]
    }
  }
}

If file.extension is of keyword type (Recommended)

Or if you have a keyword sibling in file.extension.keyword, you can make use of Terms Query

POST my_png/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "terms": {                         <---- Terms Query
            "file.extension.keyword": [      <---- Or 'file.extension' field, whichever must of be type `keyword`
              "jpg",
              "jpeg",
              "png"
            ]
          }
        },
        {
          "match": {
            "content": "abc"
          }
        }
      ]
    }
  }
}

Based on your requirement, I like to think that you must be using the second option as for exact matches, you would need to use Terms Query on keyword field.

Hope that helps!