Lucene.net 4.8:添加重要方面的顺序?

时间:2017-12-12 21:39:28

标签: lucene.net faceted-search

假设我有2个文件,每个文件有2个标签。

文档1:标记1,标记2 文件2:标签2,标签1

我在lucene中构建一个文档,如下所示:

        var doc = new Document
        {
            new StoredField("Id", blogPost.Id),
            new Int32Field("ModuleId", blogPost.ModuleId, Field.Store.YES),
            new TextField("Title", blogPost.Title, Field.Store.YES),
            new StringField("Slug", blogPost.Slug, Field.Store.YES),
            new StoredField("ImagePath", blogPost.ImagePath),
            new TextField("Intro", blogPost.Intro, Field.Store.YES),
            new TextField("Html", blogPost.Title, Field.Store.YES),
            new Int64Field("PublishDate", blogPost.PublishDate.Ticks, Field.Store.YES),
            new FacetField("PublishDateTag", blogPost.PublishDate.Year.ToString(), blogPost.PublishDate.Month.ToString(), blogPost.PublishDate.Year.ToString())
        };

        foreach (var tag in blogPost.TagObjects)
        {
            doc.Add(new Int32AssociationFacetField(1,"Tags", tag.Name));
            doc.Add(new StringField("Tag", tag.Name, Field.Store.YES));

            doc.Add(new Int32AssociationFacetField(1, "TagSlugs", tag.Slug));
            doc.Add(new StringField("TagSlug", tag.Slug, Field.Store.YES));

因为我想要强硬,所以不能正常工作。当我这样搜索时:

        var facetsConfig = ConfigFacets();

        IList<FacetResult> results = new List<FacetResult>();

        using (var indexReader = DirectoryReader.Open(IndexDir))
        using (var taxoReader = new DirectoryTaxonomyReader(TaxoDir))
        {
            var searcher = new IndexSearcher(indexReader);
            var facetsCollector = new FacetsCollector();

            // MatchAllDocsQuery is for "browsing" (counts facets
            // for all non-deleted docs in the index); normally
            // you'd use a "normal" query:
            FacetsCollector.Search(searcher, new MatchAllDocsQuery(), 10, facetsCollector);

            // Retrieve results
            Facets tags = new TaxonomyFacetSumInt32Associations("$tag", taxoReader, facetsConfig, facetsCollector);
            results.Add(tags.GetTopChildren(10, "Tags"));

        } // Disposes indexReader and taxoReader

        if (results[0] == null)
            return new Dictionary<string, int>();

        return results.Where(x => x.Dim == "Tags").SelectMany(x => x.LabelValues).ToDictionary(x => x.Label, x => Convert.ToInt32(x.Value));

结果如下:标签1(1),标签1(1),标签2(1),标签2(1)

看起来标签的顺序在某种程度上扮演了一个角色,我完全不想要。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

该死的!原来是一个愚蠢的错误。

我正在分割一个字符串,但之后没有修剪它。所以我正在添加&#34;标签1&#34;,&#34;标签2&#34;对于第一个文件和&#34;标签2,&#34;标签1&#34;为了第二个。