Prioritizing search results in Solr that start with the search string

884 Views Asked by At

I am using Solr 5.2, with a possible upgrade to 8.2 soon, along with edismax to search for a string among selected fields. Some of the query fields use ngram, so that results that contain the search string will show up in the search. I want to have the documents where there is a match that starts with the search word to show up at the beginning of the search results.

My first attempt was to put both the string field and text ngram field in the qf and search for both the string and starts with using a boost. For example, if the search string is "lorem", I would use:

q=(lorem*) or (lorem)

I would also use boosters for the string fields. However, the ngram matches seem to score more points, so matches beginning with the search words do not always show up first.

How do I have matches where the search words are at the beginning of the search string show up first?

1

There are 1 best solutions below

0
mkhludnev On

Sadly the syntax is awkward and not really predictable. Literally what you need is

q=({!prefix f=string_fied}lorem)^100 {!field f=ngram_field}electric&defType=lucene

Note:

  1. dismax (which you imply by referring to qf ) doesn't supportwildcard, there's a note in the refguide.

  2. don't start complex query from { there's one surprise here. You might even use space to start it.

  3. If you want to have mutitiple field in these two groups use {!max}, to mimic dismax mutifield behavior.

Good Luck. You really need it.

PS. Btw look at JSON Query DSL it might avoid such syntax bomb and a little bit cleaner.