1
votes

i will use lucene.net first time so couple of confusion arising in mind when see the line of code. i got a sample code for searching word with lucene and few line are not clear to me.here is sample code below.

Question 1

ListBox1.Items.Clear();
var searcher = new Lucene.Net.Search.IndexSearcher(MapPath("~/searchlucene/"));
var oParser = new Lucene.Net.QueryParsers.QueryParser("content", new StandardAnalyzer());

string sHeader = " OR (header:" + TextBox1.Text + ")";
string sType = " OR (type:" + TextBox1.Text + ")";
string sSearchQuery = "(" + TextBox1.Text + sHeader + sType + ")";

var oHitColl = searcher.Search(oParser.Parse(sSearchQuery));
for (int i = 0; i < oHitColl.Length(); i++)
{
    Document oDoc = oHitColl.Doc(i);
    ListBox1.Items.Add(new ListItem(oDoc.Get("header") + oDoc.Get("type") +  oDoc.Get("content")));            
}

searcher.Close();

Question 2

this below lines not clear what is going on...!! please discuss the objective of each line below.

string sHeader = " OR (header:" + TextBox1.Text + ")";
string sType = " OR (type:" + TextBox1.Text + ")";
string sSearchQuery = "(" + TextBox1.Text + sHeader + sType + ")";

var oHitColl = searcher.Search(oParser.Parse(sSearchQuery));
for (int i = 0; i < oHitColl.Length(); i++)
{
    Document oDoc = oHitColl.Doc(i);
    ListBox1.Items.Add(new ListItem(oDoc.Get("header") + oDoc.Get("type") +  oDoc.Get("content")));            
}

Question 3

what is header:

what is type:

why heade & type concatinated after search keyword like string sSearchQuery = "(" + TextBox1.Text + sHeader + sType + ")";

Question 4

why content is missing in searchquery content what would be the result if i write like

string sHeader = " OR (header:" + TextBox1.Text + ")";
string sType = " OR (type:" + TextBox1.Text + ")";
string sContent = " OR (content:" + TextBox1.Text + ")";
string sSearchQuery = "(" + TextBox1.Text + sHeader + sType + sContent ")";

why header, type & content is reading....what for?? *oDoc.Get("header") + oDoc.Get("type") + oDoc.Get("content")*

why i need to read header,type & content like oDoc.Get("header") + oDoc.Get("type") + oDoc.Get("content") we can read content only....why type & header is also required??

1

1 Answers

0
votes

The first code builds a query that searches several fields, assuming that the input in TextBox1 does not mess with the query (like containing parentheses or whitespaces). Building a search query with string concatenation is often hard to get right, I would use the MultiFieldQueryParser instead.

var fields = new[] { "content", "header", "type" };
var analyzer = new StandardAnalyzer(Version.LUCENE_30);
var queryParser = new MultiFieldQueryParser(Version.LUCENE_30, fields, analyzer);
var query = queryParser.Parse(TextBox1.Text);
var result = searcher.Search(query, 25); /* find 25 best matches */

Your for-loop iterates through the result and reads the values of the stored fields and add them to a listbox. This requires that the fields where indexed with Field.Store.YES to work.