Question

Currently I have an issue with the Lucene search (version 2.9).

I have a search term and I need to use it on several fields. Therefore, I have to use MultiFieldQueryParser. On the other hand, I have to use the WhildcardQuery(), because our customer wants to search for a term in a phrase (e.g. "CMH" should match "KRC250/CMH/830/T/H").

I have tried to replace the slashes ('/') with stars ('*') and use a BooleanQuery with enclosed stars for the term. Unfortunately whichout any success.

Does anyone have any Idea?

Was it helpful?

Solution 2

Sorry, maybe I have described it a little bit wrong.

I took something like this:

BooleanQuery bq = new BooleanQuery();

foreach (string field in fields)
{
    foreach (string tok in tokArr)
    {
        bq.Add(new WildcardQuery(new Term(field, " *" + tok + "* ")), BooleanClause.Occur.SHOULD);
    }
}

return bq;

but unfortunately it did not work.

I have modified it like this

string newterm = string.Empty;
string[] tok = term.Split(new[] { ' ', '/' }, StringSplitOptions.RemoveEmptyEntries);
tok.ForEach(x => newterm += x.EnsureStartsWith(" *").EnsureEndsWith("* "));

var version = Lucene.Net.Util.Version.LUCENE_29;
var analyzer = new StandardAnalyzer(version);
var parser = new MultiFieldQueryParser(version, fields, analyzer);
parser.SetDefaultOperator(QueryParser.Operator.AND);
parser.SetAllowLeadingWildcard(true);

return parser.Parse(newterm);

and my customer love it :-)

OTHER TIPS

Yes, if the field shown is a single token, setting setAllowLeadingWildcard to be true would be necessary, like:

parser.setAllowLeadingWildcard(true);
Query query = parser.parse("*CMH*");

However:

You don't mention how the field is analyzed. By default, the StandardAnalyzer is used, which will split it into tokens at slashes (or asterisks, when indexing data). If you are using this sort of analysis, you could simply create a TermQuery searching for "cmh" (StandardAnalyzer includes a LowercaseFilter), or simply:

String[] fields = {"this", "that", "another"};
QueryParser parser = MultiFieldQueryParser(Version.LUCENE_29, fields, analyzer) //Assuming StandardAnalyzer
Query simpleQuery = parser.parse("CMH");
//Or even...
Query slightlyMoreComplexQuery = parser.parse("\"CMH/830/T\"");

I don't understand what you mean by a BooleanQuery with enclosed stars, if you can include code to elucidate that, it might help.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top