2012-01-17 264 views
0

我已用2个的内容字段的索引(分析,索引存储&):
例如:namehobbies。 (该爱好字段可以具有不同的值被添加多次)。搜索Lucene索引

我有另一场是唯一索引(un_analyzed &不存储)用于过滤:
例如:country_code

现在,我想建立一个查询,将检索匹配(尽可能最好的文档可能的)一些“搜索”输入框,但其中country_code有一定的准确值只有这样的文件。

什么是最合适的组合查询语法/查询分析程序使用,以建立这样的查询。

回答

2

您可以使用下面的查询:

country_code:india +(name:search_value OR hobbies:search_value) 
+0

感谢,这是我需要的东西。我还在开头添加了另一个+。 – epeleg

2

你为什么不开始QueryParser,它可能工作为您的使用情况下,它需要的努力最少。

这不是从你的问题不清楚,但是让我们假设你有一个单一的输入域(“搜索”)和国家代码的组合框。然后,你会读这些值,并创建一个查询:

// you don't have to use two parsers, you can do this using one. 
QueryParser nameParser = new QueryParser(Version.LUCENE_CURRENT, "name", your_analyzer); 
QueryParser hobbiesParser = new QueryParser(Version.LUCENE_CURRENT, "hobbies", your_analyzer); 

BooleanQuery q = new BooleanQuery(); 
q.add(nameParser.parser(query), BooleanClause.Occur.SHOULD); 
q.add(hobbiesParser.parser(query), BooleanClause.Occur.SHOULD);BooleanClause.Occur.SHOULD); 

/* Filtering by country code can be done using a BooleanQuery 
* or a filter, the difference will be how Lucene scores matches. 
* For example, using a filter: 
*/ 
Filter countryCodeFilter = new QueryWrapperFilter(new TermQuery(new Term("country_code",))); 

//and finally searching: 
TopDocs topDocs = searcher.search(q, countryCodeFilter, 10); 
+0

可以添加更多的信息,再你说的关于这两个过滤替代品是什么?它会如何影响评分?那么表演呢? – epeleg

+0

这听起来像是另一个计算器的问题:) – milan