.net lucene Multi-user search

I created the index as

Document doc = new Document();
        doc.Add(new Field("SearchKey", (item.FullTextColumn ?? item.Code), Field.Store.NO, Field.Index.TOKENIZED));
        doc.Add(new Field("Type", item.Type.ToString(), Field.Store.YES, Field.Index.TOKENIZED));
        doc.Add(new Field("Name", item.Name, Field.Store.YES, Field.Index.UN_TOKENIZED));
        doc.Add(new Field("Code", item.Code ?? string.Empty, Field.Store.YES, Field.Index.UN_TOKENIZED));

      

etc.

and I'm trying to find a term like "Kansas City" in the "SearchKey" field and the other registered "Type" should be "Airport"

for this i am writing

QueryParser parser = new QueryParser("SearchKey", analyzer);
        Query searchQuery = parser.Parse(text);
 TermQuery typeQuery = new TermQuery(new Term("Type", "Airport"));
 BooleanQuery filterQuery = new BooleanQuery();
        filterQuery.Add(typeQuery, BooleanClause.Occur.MUST);
        Filter f = new QueryFilter(filterQuery);
 Hits results = searcher.Search(searchQuery,f);

      

but that doesn't give me any result,

if i remove 'f' from

Hits results = searcher.Search(searchQuery,f);

      

then it gives the result, but the Type field contains values ​​other than Airport.

any idea where i am wrong?

+2


source to share


1 answer


Looking at your code, I think you need to add every query (one for SearchKey and one for Type) to BooleanQuery as shown below.

var standardLuceneAnalyzer = new StandardAnalyzer();

var query1 = new QueryParser("SearchKey", standardLuceneAnalyzer).Parse("Kansas City*");
var query2 = new QueryParser("Type", standardLuceneAnalyzer).Parse("Airport");

BooleanQuery filterQuery = new BooleanQuery();
filterQuery.Add(query1, BooleanClause.Occur.MUST);
filterQuery.Add(query1, BooleanClause.Occur.MUST);

TopDocs results = searcher.Search(filterQuery);

      



I haven't tested the code, but it should work.

+2


source







All Articles