【问题标题】:Multiple word Autosuggest using Lucene.Net使用 Lucene.Net 的多词自动提示
【发布时间】:2010-06-08 19:45:38
【问题描述】:

我目前正在开发一个使用 Lucene.Net 将数据库中的数据索引到索引文件的搜索应用程序。我有一个产品目录,其中包含名称、短描述和长描述、sku 和其他字段。数据使用 StandardAnalyzer 存储在 Index 中。我正在尝试为文本字段添加自动建议,并使用 TermEnum 从索引中获取所有关键字术语及其分数。但返回的条款是单一条款。例如,如果我输入 co,则返回的建议是服装、计数、收藏、牛仔、组合等。但我希望建议返回短语。例如,如果我搜索 co,建议应该是牛仔装、成人装、密码锁等。

以下是获取建议的代码:

public string[] GetKeywords(string strSearchExp)
{

IndexReader rd = IndexReader.Open(mIndexLoc);
TermEnum tenum = rd.Terms(new Term("Name", strSearchExp));
string[] strResult = new string[10];
int i = 0;
Dictionary<string, double> KeywordList = new Dictionary<string, double>();
do
{
    //terms = tenum.Term();
    if (tenum.Term() != null)
    {
        //strResult[i] = terms.text.ToString();
        KeywordList.Add(tenum.Term().text.ToString(), tenum.DocFreq());
    }
} while (tenum.Next() && tenum.Term().text.StartsWith(strSearchExp) && tenum.Term().text.Length > 1);

var sortedDict = (from entry in KeywordList orderby entry.Value descending select entry);

foreach (KeyValuePair<string, double> data in sortedDict)
{
    if (data.Key.Length > 1)
    {
        strResult[i] = data.Key;
        i++;
    }
    if (i >= 10)    //Exit the for Loop if the count exceeds 10
        break;
}
tenum.Close();
rd.Close();
return strResult;

}

任何人都可以给我指导来实现这一目标吗?感谢您对此进行调查。

【问题讨论】:

    标签: lucene.net


    【解决方案1】:

    您可以简单地使用Field.Index.NOT_ANALYZED 参数或KeywordAnalyzer 在不同的字段中索引您的产品名称,然后对其运行通配符查询或前缀查询。

    【讨论】:

      【解决方案2】:

      正如您所说,“返回的条款是单一条款”。因此,您需要创建由短语组成的术语。

      您可以使用内置的 ShingleFilter 标记过滤器来创建您的短语术语:

      http://lucene.apache.org/java/2_4_0/api/org/apache/lucene/analysis/shingle/ShingleFilter.html

      您可能需要为此使用单独的字段,因为我不确定 ShingleFilter 是否真的会产生单个术语 - 您可能需要对此进行试验。

      【讨论】:

      • 谢谢肯。我正在研究 Lucene.net,但不确定 .Net 中与 ShingleFIlter 等效的过滤器。让我试着找出答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-05
      • 2011-07-13
      • 2015-03-10
      • 1970-01-01
      • 1970-01-01
      • 2019-05-09
      相关资源
      最近更新 更多