elasticsearch - Document contained within search term

Viewed 22

I have an index as follows:

"_source": {
  "iD": "378",
  "containsTerms": [
    {
      "containsTerm": "search term to match"
    },
    {
      "containsTerm": "another search term to match"
    },
    {
      "containsTerm": "etcetera"
    }
  ],
  "startdate": "2022-09-08T14:07:00",
  "enddate": "2022-09-30T14:07:00"
}

And its mapping is:

"mappings":{
   "properties":{
      "containsTerms":{
         "properties":{
            "containsTerm":{
               "type":"text",
               "fields":{
                  "keyword":{
                     "type":"keyword",
                     "ignore_above":256
                  }
               }
            }
         }
      },
      "enddate":{
         "type":"date"
      },
      "startdate":{
         "type":"date"
      }
   }
}

The following query running on the index works as expected:

{
   "query":{
      "bool":{
         "must":[
            {
               "match":{
                  "containsTerms.containsTerm":{
                     "query":"search term to match"
                  }
               }
            },
            {
               "range":{
                  "startdate":{
                     "lte":"2022-09-22T14:43:38"
                  }
               }
            },
            {
               "range":{
                  "enddate":{
                     "gte":"2022-09-22T15:43:38"
                  }
               }
            }
         ]
      }
   }
}

elasticsearch returns the documents on all partial search phrases e.g.

  • search
  • search term
  • search term to
  • search term to match

and also on larger phrases e.g.

  • search term to match blah blah blah

so far so good.

However, the requirement is that the document is only returned on an exact match. Changing the query to use keyword

{
  "match":{
    "containsTerms.containsTerm.keyword":{
      "query":"search term to match"
    }
  }
}

ensures that only a search for 'search term to match' will return the document. This works fine.

However... the requirement is to return a document with an exact match on the term contained within a larger phrase.

For instance:

"blah blah search term to match blah blah" should return the document, whereas "blah blah search term to blah blah" should not.

It is almost as if, rather than returning results where the search term is contained within the document, we should be returning results where the document is contained within the search term.

I am not sure whether this is even possible, or what type of query to use.

1 Answers

You can use match_phrase type of query which will match phrase into large text.

"blah blah search term to match blah blah" should return the document, whereas "blah blah search term to blah blah" should not.

below query will cover your above usecase.

{
  "query": {
    "match_phrase": {
      "containsTerms.containsTerm": "search term to match"
    }
  }
}

I hope you want to get entire document when query match to atlest one element of your containsTerms object.

Related