Wildcard search for exact phrase

We want to migrate from Solr to Elasticsearch and everything looks fine, but there is one task we are doing in Solr for which I cannot find a solution using Elasticsearch. I need to find exactly matching text in a content of large document, which can start and end at the middle of any word. For example, if the document has words in exact phrase: "one two three", I need to find this document if the user searches for: "*ne two thr*". Note one wildcard at the beginning of the phrase and one at the end. I need to find only exactly matching phrase, which contains spaces. In Solr I can achieve this using Complex Phrase Query Parser with ReversedWildcardFilterFactory filter. How can it be done in Elasticsearch?

Hi Vadim. Check out interval queries.

You may need to parse the user input (using the analyze API) and use the terms in the result to construct the equivalent JSON for the interval query you need.

Thank you Mark! Following your advice I tried the following query:

{
  "query": {
    "intervals" : {
      "_text_" : {
        "all_of" : {
          "ordered" : true,
          "intervals" : [
            {
              "wildcard": {
                "pattern": "*olicy"
              }
            },
            {
              "match": {
                "query": "changes. This endorsement ",
                "max_gaps" : 0,
                "ordered" : true
              }
            },
            {
              "wildcard": {
                "pattern": "modi*"
              }
            }
          ]
        }
      }
    }
  }
}

Works like a charm, even without reversed filter. Would reverse filter improve the search speed? Also it looks like I'll have to increase the number of partial matches using 'index-prefixes' option. Is there anything else I should do to improve such queries?

The use of reverse-indexed terms would help but I’m not clear if a positional query that mixes prefix and normal terms is smart enough to match positions across multiple Lucene fields - @romseygeek might be able to help with this question.

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