Search with PhraseQuery and keyword analyzer

204 views
Skip to first unread message

Chander S

unread,
May 11, 2016, 7:48:39 PM5/11/16
to bleve
I am looking for an exact match search for multi-word strings, so I set up keyword analyzer and used PhraseQuery, but it is not working. I have created a test case here

This should work similar to QueryBuilders.termQuery in ElasticSearch. Let me know if this is not supported

Thanks
Chander

Marty Schoch

unread,
May 11, 2016, 8:07:57 PM5/11/16
to bl...@googlegroups.com
So, there are a couple of things to note:

1.  The first 2 queries target the wrong filed "desc" not "Body":

sreq := NewSearchRequest(NewPhraseQuery([]string{"Texas", "Wings"}, "desc"))
sreq = NewSearchRequest(NewPhraseQuery([]string{"Texas", "Roadstar"}, "desc"))

But, even after changing that, they still don't work as expected, because...

2.  The keyword analyzer indexes the fields *exactly* as they are.  That means that "Texas Wings" is indexed as "Texas Wings" (note thats all 1 term, not 2 separate terms).  That means that a phrase search cannot match it, as its not a phrase, just a single term.  To illustrate this, here is a query that does match the data as indexed:

q := NewTermQuery("Texas Wings").SetField("Body")
sreq := NewSearchRequest(q)

3.  The query strings you're using aren't doing what you want either.  For example:

+Body:Texas Wings

This is only restricting the term Texas to the field body, and it's search for Wings in any field.  If you wanted to do a phrase search you must put the phrase in quotes like:

+Body:"Texas Wings"

But, again that still won't match.  So, how can you make this work?  It really depends what you're trying to do.  If you want to be able to do phrase searches, you have to tokenize the input so that the terms are indexed separately.  But, with a custom analyzer you could do this without any lower-casing, stemming or stop-word removal (so that it still behaves more like exact match).

Alternatively, if you really want exact match on the whole field value, you can use term search as illustrated above.

Or if you want something else entirely, maybe describe that a little bit more.

Thanks,
marty


--
You received this message because you are subscribed to the Google Groups "bleve" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bleve+un...@googlegroups.com.
To post to this group, send email to bl...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bleve/24a50c7e-50f6-4883-8b3f-d9c744279a78%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Chander S

unread,
May 11, 2016, 9:16:10 PM5/11/16
to bleve
Thanks for your response. Its working with the TermQuery.
I was actually trying to track down the issue from couchbase fts (with couchbase  java client 2.2.7), where the Term/PhraseQuery doesn't work when the keyword analyser is used from the UI, so was just checking to see if this is an issue in Bleve or CBFT.

Thanks again !
Chander
Reply all
Reply to author
Forward
0 new messages