getsetcode getsetcode - 3 months ago 14
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

_type
and will only ever contain
typeone
or
typetwo
.

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
typeone
in their
_type
field, whilst still returning
typetwo
records (albeit with a lower relevancy score)?

UPDATE

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)

AR1 AR1
Answer

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:

defType=edismax&bq=_type:"typeone"^3

or

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

defType=edismax&bf=if(termfreq(_type,"typeone"),3,if(termfreq(_type,"typetwo"),2,1))

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.

Comments