Derek
Derek

Reputation: 8628

Lucene Search Syntax

I need help figuring out which query types to use in given situations.

I think i'm right in saying that if i stored the word "FORD" in a lucene Field and i wanted to find an exact match i would use a TermQuery?

But which query type should i use if I was looking for the word "FORD" where the contents of the field where stored as :-

"FORD|HONDA|SUZUKI"

What if i was to search the contents of an entire page, looking for a phrase? such as "please help me"?

Upvotes: 0

Views: 1821

Answers (2)

Mark Leighton Fisher
Mark Leighton Fisher

Reputation: 5693

I would turn the problem sideways, so I put the multiple values for each field separately in the index -- this should make searching simpler. Looking at Field Having Multiple Values might be helpful.

Upvotes: 1

L.B
L.B

Reputation: 116118

If you want to search FORD in FORD|HONDA|SUZUKI, either index with Field.Index.ANALYZED, or store it as below to use TermQuery

var analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);
var fs = FSDirectory.Open("test.index");

//Index a Test Document
IndexWriter wr = new IndexWriter(fs, analyzer, true, IndexWriter.MaxFieldLength.LIMITED);
var doc = new Document();

doc.Add(new Field("Model", "FORD", Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.Add(new Field("Model", "HONDA", Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.Add(new Field("Model", "SUZUKI", Field.Store.YES, Field.Index.NOT_ANALYZED));

doc.Add(new Field("Text", @"What if i was to search the contents of an entire page, looking for a phrase? such as ""please help me""?", 
                    Field.Store.YES, Field.Index.ANALYZED));

wr.AddDocument(doc);
wr.Commit();

var reader = wr.GetReader();
var searcher = new IndexSearcher(reader);

//Use TermQuery for "NOT_ANALYZED" fields
var result = searcher.Search(new TermQuery(new Term("Model", "FORD")), 100);
foreach (var item in result.ScoreDocs)
{
    Console.WriteLine("1)" + reader.Document(item.Doc).GetField("Text").StringValue);
}

//Use QueryParser for "ANALYZED" fields
var qp = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "Text", analyzer);
result = searcher.Search(qp.Parse(@"""HELP ME"""), 100);
foreach (var item in result.ScoreDocs)
{
    Console.WriteLine("2)" + reader.Document(item.Doc).GetField("Text").StringValue);
}

TermQuery means you want to search the term as it is stored in index which depends on how you indexed that field(NOT_ANALYZED, ANALYZED+WhichAnalyzer). Most common use of it is with NOT_ANALYZED fields.

You can use TermQuery with ANALYZED fields too, but then you should know how the analyzer tokenizes your input string. Below is a sample to see what how analyzers tokenize your input

var text = @"What if i was to search the contents of an entire page, looking for a phrase? such as ""please help me""?";
var analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30 );
//var analyzer = new WhitespaceAnalyzer();
//var analyzer = new KeywordAnalyzer();
//var analyzer = new SimpleAnalyzer();

var ts = analyzer.TokenStream("", new StringReader(text));
var termAttr = ts.GetAttribute<ITermAttribute>();

while (ts.IncrementToken())
{
    Console.Write("[" + termAttr.Term + "] " );    
}

Upvotes: 3

Related Questions