13

Using the wildcard operator, I can match terms starting with some value:

{
    "query": {
        "query_string" : {
            "query" : "subject:cell*"
        }
    }
}

The subject field here is a keyword field (non-analyzed). This works fine, but I cannot figure out how to find terms starting with, say, "cellular contr". Trying double quotes did not yield the expected results:

{
    "query": {
        "query_string" : {
            "query" : "subject:\"cellular contr*\""
        }
    }
}

Note: phrase search works fine with exact matches, just not with the wildcard. My guess is that the star is not interpreted as a wildcard operator inside the double quotes. Is that correct? And is there any other way to use the wildcard operator with a phrase?

Note: I have to use Query String Query, since the query is coming from user input.

(I know I could resort to regexp, but would prefer not to)

3 Answers 3

11

In addition to the custom analyzer as pointed by Hemed, you need to do search as below -

{
    "query": {
        "query_string" : {
            "query" : "subject:cellular\\ contr*"
        }
    }
}

Found it after a lot of research and tries!

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for this hint! "\\ " is telling elastic that it IS a space in the phrase and you can (or must) omit the quotes of the searched string.
Thanks for the details, hard to point any post regarding this
2

EDIT: Define custom analyzer for searching:-

settings:
   index:
     analysis:
       analyzer:
         keyword_analyzer:
           type: custom
           tokenizer: keyword
           filter:
             - lowercase

Found out that you need to use Prefix Query in this case, because Query String Query always segments on spaces during parsing.

But since you are using lowecase filter in this field and Prefix Query does not support analyzer, you would have to lowercase user input before appending it to the query.

New query becomes:-

   {
        "query": {
            "prefix" : {
                "subject" : "cellular contr"
            }
        }
    }

Alternatively, you can use Match Phrase Query which supports analyzer.

{
    "query": {
        "match_phrase_prefix" : {
            "subject" : {
                 "query" : "Cellular contr",
                  "analyzer" : "keyword_analyzer",
                  "max_expansions" : 100
                 }
              }
         }
    }

5 Comments

Would work if I didn't need a phrase query. But I don't want the query "cellular contr*" to match, say, "control of cellular material", which is why I'm indexing the field as keyword.
I think I misunderstood your question. Is it correct that you want to search for query string "cellular contr" and get a result for "control of cellular material" in the field subject of type keyword which is not analyzed?
No, if I search for "cellular contr", I want it to match "cellular control", but not "control of cellular material". So what I want is really subject:/cellular contr.*/, but without using regexp.
Please see my edit. I think you would have to specify custom search analyzer when searching in this particular field.
Seems like it gives exactly the same result. Probably still interpreted as (subject:cellular) OR (contr*).
0

Try this:

{
    "query": {
        "query_string" : {
            "query" : "subject:"cellular contr*",
            "split_on_whitespace" : false
        }
    }
}

1 Comment

The default value is false, so setting it explicitly to false shouldn't make any difference. But I tried just in case, and it's interpreted as "(subject:cellular) OR (contr*)", not what I want. I also tried playing with analyze_wildcard and auto_generate_phrase_queries without success (the documentation is a bit thin on exactly what they do, though)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.