lucene通配符查询与空间 [英] lucene wildcard query with space
问题描述
我有Lucene索引,其中有城市名称. 考虑到我想搜索新德里".我有一个字符串"New Del",我想将其传递给Lucene搜索者,我期望输出为"New Delhi". 如果我生成类似Name:New Del *的查询,它将为我提供所有带有"New and Del"的城市. 有什么方法可以创建带空格的Lucene查询通配符查询? 我引用了@@ a并尝试了几种解决方案,方法如下: http://www.gossamer-threads .com/lists/lucene/java-user/5487
I have Lucene index which has city names. Consider I want to search for 'New Delhi'. I have string 'New Del' which I want to pass to Lucene searcher and I am expecting output as 'New Delhi'. If I generate query like Name:New Del* It will give me all cities with 'New and Del'in it. Is there any way by which I can create Lucene query wildcard query with spaces in it? I referred and tried few solutions given @ http://www.gossamer-threads.com/lists/lucene/java-user/5487
推荐答案
听起来您已通过分析将城市名称编入索引.这将使这变得更加困难.通过分析,新"和德里"是单独的术语,因此必须这样对待.像这样使用通配符搜索多个词会变得有些困难.
It sounds like you have indexed your city names with analysis. That will tend to make this more difficult. With analysis, "new" and "delhi" are separate terms, and must be treated as such. Searching over multiple terms with wildcards like this tends to be a bit more difficult.
最简单的解决方案是在不标记的情况下索引您的城市名称(不过,用小写字母可能不是一个坏主意).然后,您只需通过转义空格即可使用查询解析器进行搜索:
The easiest solution would be to index your city names without tokenization (lowercasing might not be a bad idea though). Then you would be able to search with the query parser simply by escaping the space:
QueryParser parser = new QueryParser("defaultField", analyzer);
Query query = parser.parse("cityname:new\\ del*");
或者您可以使用简单的WildcardQuery
:
Or you could use a simple WildcardQuery
:
Query query = new WildcardQuery(new Term("cityname", "new del*"));
使用标准分析仪分析该字段:
With the field analyzed by standard analyzer:
您将需要依赖SpanQueries,如下所示:
You will need to rely on SpanQueries, something like this:
SpanQuery queryPart1 = new SpanTermQuery(new Term("cityname", "new"));
SpanQuery queryPart2 = new SpanMultiTermQueryWrapper(new WildcardQuery(new Term("cityname", "del*")));
Query query = new SpanNearQuery(new SpanQuery[] {query1, query2}, 0, true);
或者,您可以通过W(new, del*)
这样的查询使用环绕查询解析器(该查询解析器旨在提供跨度查询的更强大的支持):
Or, you can use the surround query parser (which provides query syntax intended to provide more robust support of span queries), using a query like W(new, del*)
:
org.apache.lucene.queryparser.surround.parser.QueryParser surroundparser = new org.apache.lucene.queryparser.surround.parser.QueryParser();
SrndQuery srndquery = surroundparser.parse("W(new, del*)");
query = srndquery.makeLuceneQueryField("cityname", new BasicQueryFactory());
这篇关于lucene通配符查询与空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!