I'm trying to implement a fuzzy query for selected fields within a broader multi-term query. For example, let's say the fields are name
and email
. I want to do fuzzy searches on name, but I only want exact matches on the email
field .
At the moment, I construct the parser like this (this is using FlexLucene, so this is actually in C#, but it should have full parity with regular Java Lucene):
var parser = new MultiFieldQueryParser(fields, analyzer);
where fields
is a string[]
containing {"name", "email"}
, and analyzer
is the StandardAnalyzer
, which is the same one used at the time of indexing.
So what I'm shooting for is the following: a query string like "smith" should result in hits on { 'name': 'Harry Smith', 'email': 'harry@where.com' }
, { 'name': 'Tom Smythe', 'email': 'tom@tom.com' }
, and { 'name': 'Sara Jones', 'email': 'smith@xyz.com' }
, but not { 'name': 'Tom Jones', 'email': 'smythe@abc.com' }
.
If think of creation query from code the query should be created as (for java version):
//match Smith Smythe
Query name = new PrefixQuery(new Term("name", name))
//match smith@xyz.com but not a smythe@abc.com
Query email = new PrefixQuery(new Term("name", name+"@"))
BooleanQuery.Builder builder = new BooleanQuery.Builder();
builder.add(name, BooleanClause.Occur.SHOULD);
builder.add(name, BooleanClause.Occur.SHOULD);
Query query = builder.build();
But as I understand you is needed in a parser. Lucene parser has a sophisticated logic (wildcards, boosting etc.) and not clear, what you is actually needed from it. As for simple solution I can suggest to override method getFieldQuery
, if the field is not set, should create a query like describing above, otherwise pass call to parent.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.