开发者

Searching multiple fields with Lucene

I'm having some trouble with a search I'm trying to implement. I need for a user to be able to enter a search query into a web interface and for the back-end Java to search for the query in a number of fields. An example of this might be best:

Say I have a List containing "Person" objects. Say each object holds two String fields about the person:

FirstName:  Jack
Surname:    Smith

FirstName   Mary
Surname:    Jackson

If a user enters, "jack", I need the search to match both objects, the first on 开发者_高级运维Surname, and the second on FirstName.

I've been looking at using a MultiFieldQueryParser but can't get the fields set up right. Any help on this or pointing to a good tutorial would be greatly appreciated.


MultiFieldQueryParser is what you want, as you say.

Make sure:

  • The field names are always used consistently
  • The same Analyzer is used on both fields, and also on the query parser
  • You won't find partial words by default, so if you search for jack you won't find jackson. (You can search for jack* in that case.)

Regarding field name, I always set up an enum for my field names, then use e.g. MyFieldEnum.firstname.name() when passing field names to Lucene, so that if I make a spelling mistake the compiler can catch it, and it's also a good place to put Javadoc so you can see what the fields are for, and also a place where you can see the complete list of fields you wish to support in your Lucene documents.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜