gpt4 book ai didi

asp.net - Lucene.net多字段搜索

转载 作者:行者123 更新时间:2023-12-04 04:32:15 24 4
gpt4 key购买 nike

为了获得一些与上下文相关的搜索结果,我决定尝试使用lucene.net,尽管我对此并不陌生,但我发现它并不是我遇到的最直观的库。缺少相关示例来帮助我解决问题,这无济于事。

我正在使用simple lucene来建立索引,这似乎运行得很好:

Field f = null;
Document document = new Document();

document.Add(new Field("id", dl.Id.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));

f = new Field("category", dl.CategoryName.ToLowerInvariant(), Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS);
f.SetBoost(5);
document.Add(f);

f = new Field("company_name", dl.CompanyName.ToLowerInvariant(), Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS);
f.SetBoost(2);
document.Add(f);

document.Add(new Field("description", dl.Description.ToLowerInvariant(), Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS));
document.Add(new Field("meta_keywords", dl.Meta_Keywords.ToLowerInvariant(), Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS));
document.Add(new Field("meta_description", dl.Meta_Description.ToLowerInvariant(), Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS));

//And a few more fields

基于此索引,我首先尝试按照以下方式进行查询:
var whatParser = new MultiFieldQueryParser(
global::Lucene.Net.Util.Version.LUCENE_29,
new string[] { "company_name", "description", "meta_keywords", "meta_description", "category" },
analyzer);

whatQuery = whatParser.Parse("search".ToLowerInvariant());

直到搜寻字词超过1个字词为止,这个方法的效果一直很好。接下来是短语查询。
whatQuery = new PhraseQuery();
whatQuery.Add(new Term("company_name", what));
whatQuery.Add(new Term("description", what));
whatQuery.Add(new Term("meta_keywords", what));
whatQuery.Add(new Term("meta_description", what));
whatQuery.Add(new Term("category", what));

然后我发现该错误引发了错误: All phrase terms must be in the same field
那么,我要去哪里错了?您对如何解决有什么建议?如果有更好的建议,我愿意完全改变搜索技术。

一些可能有用的附加信息
  • 所有结果都通过new Sort(new SortField[] {new SortField("is_featured", SortField.STRING, true),SortField.FIELD_SCORE})排序到最后
  • 还有一些其他搜索条件,因此每个查询都将添加到 bool 查询中,并且将发生设置为

  • 谢谢你的帮助。

    最佳答案

    我认为BooleanClause.Occur.SHOULD是问题。我们这样使用它:

    string[] fieldList = { "field1", "field2", "field3"; 

    //for us the field list varies .. there are other ways to create this array of course
    List<BooleanClause.Occur> occurs = new List<BooleanClause.Occur>();
    foreach (string field in fieldList)
    occurs.Add(BooleanClause.Occur.SHOULD);

    if(!string.IsNullOrEmpty(multiWordPhrase))
    {
    Query q = MultiFieldQueryParser.Parse(multiWordPhrase, fieldList, occurs.ToArray(), new StandardAnalyzer());
    return q;
    }

    关于asp.net - Lucene.net多字段搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5098931/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com