tags:

views:

88

answers:

1

I have created a 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 am trying to search a term like "Kansas City" in "SearchKey" field and another filed "Type" must be "Airport"

for that 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 it gives me NO result ,

if I remove 'f' from

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

then it gives result but "Type" field contain values other then "Airport" .

any Idea where I am going wrong ?

+1  A: 

Looking at your code I think you need to add each query (one for the SearchKey and one for the Type) to the BooleanQuery like 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.

Kane