How to search with analyzed text

Hi.
I'd like to know if there's a way to use analyzed text when querying.

The specific requirement is to search for a matched string in 'term' field && the document's 'term_length' field should be less than equal to the length of the string which is analyzed when searching(by search_analyzer) .

It would be like this :

GET api_test/_search
    {
    "query": {
        "bool" : {
            "must" : [
              {
                 "match": {
                      "term": ${a string that user typed}
                }
              },
              {
                "range" : {
                    "term_length": {
                       "lte":  ${analyzed string 's length}
                    }
                }
            }]
        }
    }
}

This query works fine for not-analyzed string. Still, I couldn't find how to access analyzed query's length when querying or in script. (also searched for script API)
If there's any idea let me know how to do that, or if my explanation is not enough, please tell me. :slight_smile:

Hi, and welcome!

Analysed text doesn't have a length. Analysers take the piece of text, filter out characters, generate tokens, filter tokens, and produce various data structures to allow you to query the text in various ways.

You can use a multi-field to store the analysed value in a text field, and the non-analysed value in a keyword field, then use the keyword field for getting the length.

I hope this helps.

George.

1 Like

Thanks for your kind explanation!

I got one more question from your reply, for the part 'Analysed text doesn't have a length'.
It still has a size of tokens doesn't it? When I use analyze API the response is like:

{
  "tokens" : [
    {
      "token" : "ㅇ",
      "start_offset" : 0,
      "end_offset" : 1,
      "type" : "<HANGUL>",
      "position" : 0
    },
    {
      "token" : "ㅇㅔ",
      "start_offset" : 0,
      "end_offset" : 1,
      "type" : "<HANGUL>",
      "position" : 1
    },
    {
      "token" : "ㅇㅔㄹ",
      "start_offset" : 0,
      "end_offset" : 1,
      "type" : "<HANGUL>",
      "position" : 2
    }
  ]
}

so I was wondering if I could get the size of the array in script or something.
Thank you so much :slight_smile:

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.