Adjust Lucene search results for individual fields with the same name

I am currently using Lucene as a full-text search engine. But we need to sort the search result according to a specific field.

For example, if we have the following three documents in our index with exactly the contents, except for the id field.

  val document01 = new Document() val field0100 = new Field("id", "1", Field.Store.YES, Field.Index.ANALYZED) val field0101 = new Field("contents", "This is a test: Linux", Field.Store.YES, Field.Index.ANALYZED) val field0102 = new Field("contents", "This is a test: Windows", Field.Store.YES, Field.Index.ANALYZED) document01.add(field0100) document01.add(field0101) document01.add(field0102) val document02 = new Document() val field0200 = new Field("id", "2", Field.Store.YES, Field.Index.ANALYZED) val field0201 = new Field("contents", "This is a test: Linux", Field.Store.YES, Field.Index.ANALYZED) val field0202 = new Field("contents", "This is a test: Windows", Field.Store.YES, Field.Index.ANALYZED) document02.add(field0200) document02.add(field0201) document02.add(field0202) val document03 = new Document() val field0300 = new Field("id", "3", Field.Store.YES, Field.Index.ANALYZED) val field0301 = new Field("contents", "This is a test: Linux", Field.Store.YES, Field.Index.ANALYZED) val field0302 = new Field("contents", "This is a test: Windows", Field.Store.YES, Field.Index.ANALYZED) document03.add(field0300) document03.add(field0301) document03.add(field0302) 

Now, when I browse Linux using IndexSearcher, I got the following result:

 Document<stored,indexed,tokenized<id:1> stored,indexed,tokenized<contents:This is a test: Linux> stored,indexed,tokenized<contents:This is a test: Windows>> Document<stored,indexed,tokenized<id:2> stored,indexed,tokenized<contents:This is a test: Linux> stored,indexed,tokenized<contents:This is a test: Windows>> Document<stored,indexed,tokenized<id:3> stored,indexed,tokenized<contents:This is a test: Linux> stored,indexed,tokenized<contents:This is a test: Windows>> 

When I do a Windows search, I get the same result in the same order.

 Document<stored,indexed,tokenized<id:1> stored,indexed,tokenized<contents:This is a test: Linux> stored,indexed,tokenized<contents:This is a test: Windows>> Document<stored,indexed,tokenized<id:2> stored,indexed,tokenized<contents:This is a test: Linux> stored,indexed,tokenized<contents:This is a test: Windows>> Document<stored,indexed,tokenized<id:3> stored,indexed,tokenized<contents:This is a test: Linux> stored,indexed,tokenized<contents:This is a test: Windows>> 

The question is, is it possible to weigh certain fields when building the index? For example, I would like field0201 have a higher score if it was matched in the search.

In other words, when searching for Linux I would like to get the result in the following order:

 Document<stored,indexed,tokenized<id:2> stored,indexed,tokenized<contents:This is a test: Linux> stored,indexed,tokenized<contents:This is a test: Windows>> Document<stored,indexed,tokenized<id:1> stored,indexed,tokenized<contents:This is a test: Linux> stored,indexed,tokenized<contents:This is a test: Windows>> Document<stored,indexed,tokenized<id:3> stored,indexed,tokenized<contents:This is a test: Linux> stored,indexed,tokenized<contents:This is a test: Windows>> 

And when I search for Windows , it still remains in the original order, for example:

 Document<stored,indexed,tokenized<id:1> stored,indexed,tokenized<contents:This is a test: Linux> stored,indexed,tokenized<contents:This is a test: Windows>> Document<stored,indexed,tokenized<id:2> stored,indexed,tokenized<contents:This is a test: Linux> stored,indexed,tokenized<contents:This is a test: Windows>> Document<stored,indexed,tokenized<id:3> stored,indexed,tokenized<contents:This is a test: Linux> stored,indexed,tokenized<contents:This is a test: Windows>> 

I tried to use field0201.setBoost() , but it will change the order of the search result when searching for Linux and Windows .

+4
source share
1 answer

I think this should be possible if you put your data for different sources in fields with different names. You can set the index time increase, but if you use the same name, I think the increase will apply to all fields with the same name - based on setBoost javadoc . Therefore, if you do this:

 val field0201 = new Field("content-high", "This is a test: Linux", ...) field0201.setBoost(1.5f) val field0202 = new Field("content-low", "This is a test: Windows", ...) 

And then a query with content-high:Linux content-low:Linux (using a logical query with two sentences set as term Linux), then raising for content-high should increase the rating of the document if a match is in this field. Use explain to find out if this works.

+4
source

Source: https://habr.com/ru/post/1348020/


All Articles