1

我正在尝试在文档字段上设置提升以使搜索结果更准确,但我认为它不起作用

但是这是我的代码

索引:

  private static void _addToLuceneIndex(Datafile Datafile, IndexWriter writer)
    {
        // remove older index entry
        var searchQuery = new TermQuery(new Term("Id", Datafile.article.Id.ToString()));
        writer.DeleteDocuments(searchQuery);

        // add new index entry
        var doc = new Document();
        var id = new Field("Id", Datafile.article.Id.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED);
        var content = new Field("Content", Datafile.article.Content, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS);
        content.Boost = 4;
        var title = new Field("Title", Datafile.article.Title, Field.Store.YES, Field.Index.ANALYZED);
        title.Boost = 6;
        doc.Add(id);
        doc.Add(content);
        doc.Add(title);
        foreach (var item in Datafile.article.Article_Tag)
        {
            var tmpta = new Field("Atid", item.Id.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED);
            var tagname = new Field("Tagname", item.Tag.name, Field.Store.YES, Field.Index.ANALYZED);
            tagname.Boost = 8;
            doc.Add(tmpta);
            doc.Add(tagname);

        }
        // add lucene fields mapped to db fields

        // add entry to index
        writer.AddDocument(doc);
    }

我已经使用Lukenet来查看字段是否提升了,但它没有提升并且提升仍然等于1.0

所以我尝试运行并测试它,但结果还是让我失望了

这是我的搜索代码:

搜索:

 private static IEnumerable<Datafile> _search(string searchQuery, string searchField = "")
    {
        // validation
        if (string.IsNullOrEmpty(searchQuery.Replace("*", "").Replace("?", "")))
            return new List<Datafile>();
        var indexReader = IndexReader.Open(Directory, false);
        // set up lucene searcher
        using (var searcher = new IndexSearcher(indexReader))
        {
            var hits_limit = 1000;
            // search by single field
            var enanalyzer = new SnowballAnalyzer(Version.LUCENE_30, "English");
            var aranalyzer = new SnowballAnalyzer(Version.LUCENE_30, "Arabic");
            string[] fields = new string[] { "Title", "Content", "Tagname" };
            // Dictionary<string, float> boosts = new Dictionary<string, float>();
            // boosts.Add("Title", 5);
            // boosts.Add("Content", 3);
            // boosts.Add("Tagname", 7);
            var enparser = new MultiFieldQueryParser(Version.LUCENE_30, fields, enanalyzer);
            var arparser = new MultiFieldQueryParser(Version.LUCENE_30, fields, aranalyzer);
            var query = QueryModel(searchQuery, new QueryParser[] { enparser, arparser });
            searcher.SetDefaultFieldSortScoring(true, false);
            TopFieldCollector collector = TopFieldCollector.Create(new Sort(new SortField(null, SortField.SCORE, false), new SortField("Title", SortField.STRING, true), new SortField("Tagname", SortField.STRING, true), new SortField("Content", SortField.STRING, true)),
            hits_limit,
            false,         // fillFields - not needed, we want score and doc only
            true,          // trackDocScores - need doc and score fields
            true,          // trackMaxScore - related to trackDocScores
            false); // should docs be in docId order?
            searcher.Search(query, collector);
            var hits = collector.TopDocs().ScoreDocs;
            var results = new List<Datafile>();
            foreach (var hit in hits)
            {
                var doc = searcher.Doc(hit.Doc);
                var df = _mapLuceneDocumentToData(doc);
                df.score = hit.Score;
                results.Add(df);
            }
            searcher.Dispose();
            return results;
            // search by multiple fields (ordered by RELEVANCE)

        }
    }

查询模型方法:

private static Query QueryModel(string searchQuery, QueryParser[] parsers)
    {

        BooleanQuery query = new BooleanQuery();
        searchQuery = "*" + searchQuery + "*";

        foreach (var parser in parsers)
        {
            parser.AllowLeadingWildcard = true;

            var thequery = parser.Parse(searchQuery);

            query.Add(new BooleanClause(thequery, Occur.SHOULD));
        }
        return query;
    }

我是 lucene.net 的新手,我喜欢它,但我无法解决这个问题

PS:

我也想得到一个模糊查询,就像用户输入时一样:

city in russua得到一个结果,就好像他输入:city in russia 我试过FuzzyQueryClass 但它无论如何都不起作用,是否有必要使用FuzzyQueryClass 以获得该结果

4

1 回答 1

0

因此,由于没有人回答我的问题,并且我找到了解决此问题的方法,因此我使用了搜索时间查询增强,这是我的代码:

var QParser = new QueryParser(Version.LUCENE_30, "Content", analyzer);
QParser.AllowLeadingWildcard = true;
var Query = new QParser.Parse(searchQuery);
Query.Boost = 7.0f;
return Query;

BooleanQuery如果你想做一个,你可以使用OrAnd搜索

于 2016-05-16T16:14:01.797 回答