简体   繁体   English

使用Lucene搜索多个字段

[英]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. 我需要用户能够将搜索查询输入到Web界面中,并且后端Java需要在许多字段中搜索查询。 An example of this might be best: 这方面的一个例子可能是最好的:

Say I have a List containing "Person" objects. 假设我有一个包含“Person”对象的List。 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. 如果用户输入“jack”,我需要搜索以匹配两个对象,第一个在Surname上,第二个在FirstName上。

I've been looking at using a MultiFieldQueryParser but can't get the fields set up right. 我一直在寻找使用MultiFieldQueryParser,但无法正确设置字段。 Any help on this or pointing to a good tutorial would be greatly appreciated. 任何有关这方面的帮助或指向一个好的教程将不胜感激。

MultiFieldQueryParser is what you want, as you say. 正如你所说, MultiFieldQueryParser就是你想要的。

Make sure: 确保:

  • The field names are always used consistently 始终使用字段名称
  • The same Analyzer is used on both fields, and also on the query parser 两个字段以及查询解析器上都使用相同的Analyzer
  • You won't find partial words by default, so if you search for jack you won't find jackson . 默认情况下你不会找到部分单词,所以如果你搜索jack你将找不到jackson (You can search for jack* in that case.) (在这种情况下你可以搜索jack* 。)

Regarding field name, I always set up an enum for my field names, then use eg 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. 关于字段名称,我总是为我的字段名称设置一个enum ,然后在将字段名称传递给Lucene时使用例如MyFieldEnum.firstname.name() ,这样如果我犯了拼写错误,编译器就可以捕获它,它也是一个放置Javadoc的好地方,以便您可以查看字段的用途,还可以在Lucene文档中查看要支持的完整字段列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM