简体   繁体   English

关于单词搜索和lucene.net C#

[英]Regarding word search and lucene.net c#

i will use lucene.net first time so couple of confusion arising in mind when see the line of code. 我将第一次使用lucene.net,因此在看到代码行时会引起一些混乱。 i got a sample code for searching word with lucene and few line are not clear to me.here is sample code below. 我得到了一个用lucene搜索单词的示例代码,对我来说不清楚几行。这是下面的示例代码。

Question 1 问题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 问题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 问题3

what is header: 什么是标头:

what is type: 什么是类型:

why heade & type concatinated after search keyword like string sSearchQuery = "(" + TextBox1.Text + sHeader + sType + ")"; 为什么标题和类型在搜索关键字(例如字符串 sSearchQuery =“(” + TextBox1.Text + sHeader + sType +“)”)之后重复显示;

Question 4 问题4

why content is missing in searchquery content what would be the result if i write like 为什么在searchquery内容中缺少内容,如果我这样写将会是什么结果

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") * * oDoc.Get(“标题”)+ oDoc.Get(“类型”)+ oDoc.Get(“内容”) *

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?? 为什么我需要读取标题,类型和内容,例如oDoc.Get(“ header”)+ oDoc.Get(“ type”)+ oDoc.Get(“ content”)我们只能读取内容。还需要吗?

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). 第一个代码构建了一个查询多个字段的查询,假设TextBox1中的输入不会与查询混淆(例如包含括号或空格)。 Building a search query with string concatenation is often hard to get right, I would use the MultiFieldQueryParser instead. 用字符串连接构建搜索查询通常很难正确,我会改用MultiFieldQueryParser

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. 您的for循环遍历结果,并读取存储的字段的值并将其添加到列表框中。 This requires that the fields where indexed with Field.Store.YES to work. 这要求使用Field.Store.YES索引的字段起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM