Finding multiple fields with Lucene

I'm having problems with the search I'm trying to implement. I need the user to be able to enter a search query in the web interface and for internal Java search to search in multiple fields. An example of this might be better:

Say that I have a list containing Person objects. Let's say that each object contains two string fields about a person:

FirstName: Jack Surname: Smith FirstName Mary Surname: Jackson 

If the user enters "nest", I need a search to match both objects, the first by last name and the second by first name.

I was looking for using MultiFieldQueryParser, but can't set fields properly. Any help on this or pointing to a good tutorial would be appreciated.

+4
source share
1 answer

MultiFieldQueryParser is what you want, as you say.

Make sure that:

  • Field names are always used sequentially.
  • The same Analyzer used for both fields, as well as for the query analyzer
  • You will not find partial words by default, so if you are looking for jack , you will not find jackson . (You can search for jack* in this case.)

As for the field name, I always configure enum for my field names, then use, for example, MyFieldEnum.firstname.name() when passing Lucene field names, so if I make a spelling mistake, the compiler can catch it, and this is also good a place to host the Javadoc so you can see what the fields are for, as well as a place where you can see the full list of fields that you want to support in your Lucene documents.

+3
source

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


All Articles