-1

I'm using Lucene.NET 3.0.3 to make a search for two fields (I have two fields named "Noidung", "Dapa"), I'm using BooleanQuery in C# to make this, but when I run this code nothing returns as a result, I'm sure that nothing is wrong with the source to search because I tested it. So can someone that uses Lucene.NET 3.0.3 provide some explanation?

IndexReader indexreader = IndexReader.Open(directory, true);
Searcher indexsearch = new IndexSearcher(indexreader);

BooleanQuery bq = new BooleanQuery();

Query q1 = new TermQuery(new Term("Noidung", PhanSoSanh));
Query q2 = new TermQuery(new Term("Dapa", txtText.Text.ToString().Trim()));
bq.Add(q1, Occur.MUST);
bq.Add(q2, Occur.MUST);

TopDocs ketqua = indexsearch.Search(bq, null, 500);

var hits = ketqua.ScoreDocs;
foreach (var hit in hits)
{
    // Return result
}

I have 2 field in index ( "Noidung" , "Dapa" ). Now i wanna search in field "Noidung" and then take result to search with "Dapa" . I use booleanQuery but notthing return in result ? help me?

Nam
  • 1
  • 2
  • 2
    The way you're creating your term queries suggest that you'll be doing exact matches, both regarding to casing and tokenization. Are you indexing the documents with the KeywordAnalyzer? What's are the values of PhanSoSanh and txtText.Text? Why are you not using QueryParser? – sisve Mar 16 '13 at 15:43
  • PhanSoSanh and txtText.text are string to search i take it in textbox. Indexing is OK ! cause I use QueryParser to search in one field when i index that's ok. But now i must search in 2 field in index of 2 data different. and that you see – Nam Mar 16 '13 at 16:22

1 Answers1

3

Can you try the QueryParser instead?

BooleanQuery booleanQuery = new BooleanQuery();

var noidungQuery = new QueryParser(version, "Noidung", analyzer)
    .Parse(PhanSoSanh);
var dapaQuery = new QueryParser(version, "Dapa", analyzer)
    .Parse(txtText.Text.ToString().Trim());

booleanQuery.Add(noidungQuery, Occur.MUST);
booleanQuery.Add(dapaQuery, Occur.MUST);

Using TermQuery sends the text raw against the index, as mention by @SimonSvensson, which is causing the zero results, as explained further in this answer.

Community
  • 1
  • 1
rae1
  • 6,066
  • 4
  • 27
  • 48
  • I have 2 field in index ( "Noidung" , "Dapa" ). Now i wanna search in field "Noidung" and then take result to search with "Dapa" . I use booleanQuery but notthing return in result ? help me? – Nam Mar 19 '13 at 12:03
  • @rae1n Hi. Can you please look at my question http://stackoverflow.com/questions/16906689/filter-not-working-with-text-values-lucene-3-0-3. Nobody replying to Lucene posts. – Huzaifa Jun 04 '13 at 16:37