Search for substrings in specific order of appearance

Hi
Is it possible to search for substrings in the same order as they appear in a text?
I know I can run something like term:first AND second, but it will also return results where second appearing before first.
I've tried using regex (/first.*second/ and /.*first.*second.*/) but it didn't return any results

@Bargs help please?

Thanks,
Bhavya

For a regex to work you'll need to query against the keyword version of the field. The reason it does not match the text version of the field is that the analyzer breaks the string into separate tokens, first and second. Neither token contains both first and second so nothing matches.

Thank you @Bargs
The field I'm trying to query is not indexed.
In fact, I'm looking for different values of the same key in array (held in a single item)

{
  "arr":
  [
    {
      "key": "val1"
    },
    {
      "key": "val2"
    }
  ]
}

So here I would want to discover the item when I look for val1 followed by val2

Elasticsearch can't do queries based on the JSON structure of the original document like this. If you indexed the array as a string with the terms occurring in the same order as in the array you could use a phrase query to do what you want though.

Sounds good.
Are you referring to this?
https://www.elastic.co/guide/en/elasticsearch/reference/6.7/query-dsl-match-query-phrase.html

Is there a page with more info about how should I use it?

Yes, but you wouldn't have to write query DSL. You could just query the field in the query bar like this:

arr.key:"val1 val2"

The double quotes around the values implies a phrase search. You can learn more about that syntax here: https://www.elastic.co/guide/en/kibana/current/kuery-query.html

1 Like

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