getsetcode getsetcode - 11 months ago 55
ASP.NET (C#) Question

Can I boost Lucene query parser results based on expected data?

I have a Lucene index where one of the indexed fields contains a string that identifies the type of content.

For simplicity, say this field is called

and will only ever contain

I am using Lucene query parser syntax to query this index. Say my query is:

(+fieldone:term^3.0 +classname:term^2.0)

Is it possible to extend this to boost any results that have
in their
field, whilst still returning
records (albeit with a lower relevancy score)?


I've found a syntax which works but it uses the wildcard 'all documents' syntax which I suspect is not efficient. Advice appreciated.

(+fieldone:term^3.0 +classname:term^2.0) +(*:* _type:typeone^1.1)

Answer Source

Using just Lucene syntax you can simply keep the _type boost as SHOULD in the following way:

+fieldone:term^3.0 +classname:term^2.0 (_type:typeone)^2 

you don't need wildcards.

Another solution would be using eDismax query parser then you can use the bq or bf parameter in order to boost a particular value for a field. You can use one of the following solutions:

Solution 1: you can boost your term in the following way:



Solution 2: you can use a query function in the following way:


where the results having _type=typeone are boosted by 3, the ones having typetwo are boosted by 2, otherwise it will be 1. You can modify that query according your needs.