【发布时间】:2017-12-12 21:39:27
【问题描述】:
假设我有 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)
看起来标签的顺序在某种程度上起到了作用,这是我完全不想要的。我该如何解决这个问题?
【问题讨论】:
-
没有人可以帮助我吗? :(