【问题标题】:Not getting the value of the hit field?没有得到命中字段的值?
【发布时间】:2011-05-25 11:21:45
【问题描述】:

我正在使用 Lucene.Net 进行搜索项目。我们创建了一个索引,其中包含 5 个字段的 100 000 个文档。但是在搜索时,我无法追踪我的正确记录。有谁能够帮我?为什么会这样?

我的代码是这样的

List<int> ids = new List<int>();
List<Hits> hitList = new List<Hits>(); 
List<Document> results = new List<Document>();
int startPage = (pageIndex.Value - 1) * pageSize.Value;
string indexFileLocation = @"c:\\ResourceIndex\\";  //Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "ResourceIndex");
var fsDirectory = FSDirectory.Open(new DirectoryInfo(indexFileLocation));
Analyzer analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29);
IndexReader indexReader = IndexReader.Open(fsDirectory, true);
Searcher indexSearch = new IndexSearcher(indexReader);

//ids.AddRange(this.SearchPredicates(indexSearch, startPage, pageSize, query));

/*Searching From the ResourceIndex*/
Query resourceQuery = MultiFieldQueryParser.Parse(Lucene.Net.Util.Version.LUCENE_29,
    new string[] { productId.ToString(), languagelds, query },
    new string[] { "productId", "resourceLanguageIds", "externalIdentifier" },
    analyzer);
TermQuery descriptionQuery = new TermQuery(new Term("description", '"'+query+'"'));
//TermQuery identifierQuery = new TermQuery(new Term("externalIdentifier", query));
BooleanQuery filterQuery = new BooleanQuery();
filterQuery.Add(descriptionQuery, BooleanClause.Occur.MUST);
//filterQuery.Add(identifierQuery,BooleanClause.Occur.MUST_NOT);

Filter filter = new CachingWrapperFilter(new QueryWrapperFilter(filterQuery));
TopScoreDocCollector collector = TopScoreDocCollector.create(100, true);

//Hits resourceHit = indexSearch.Search(resourceQuery, filter);
indexSearch.Search(resourceQuery, filter, collector);
ScoreDoc[] hits = collector.TopDocs().scoreDocs;
//for (int i = startPage; i <= pageSize && i < resourceHit.Length(); i++)
//{
//    ids.Add(Convert.ToInt32(resourceHit.Doc(i).GetField("id")));
//}
for (int i = 0; i < hits.Length; i++)
{
    int docId = hits[i].doc;
    float score = hits[i].score;

    Lucene.Net.Documents.Document doc = indexSearch.Doc(docId);

    string result = "Score: " + score.ToString() +
                    " Field: " + doc.Get("id");
}

【问题讨论】:

    标签: c# lucene.net


    【解决方案1】:

    您正在调用 Document.Get("id"),它返回存储字段的值。索引时没有 Field.Store.YES 将无法工作。

    如果您在没有分析 (Field.Index.NOT_ANALYZED) 或使用 KeywordAnalyzer 的情况下对字段进行索引,则可以使用 FieldCache。 (意味着每个字段和文档一个术语。)

    您需要使用最里面的阅读器才能使 FieldCache 以最佳方式工作。这是来自FieldCache with frequently updated index 的代码粘贴,它以正确的方式使用 FieldCache,从 id 字段中读取整数值。

    // Demo values, use existing code somewhere here.
    var directory = FSDirectory.Open(new DirectoryInfo("index"));
    var reader = IndexReader.Open(directory, readOnly: true);
    var documentId = 1337;
    
    // Grab all subreaders.
    var subReaders = new List<IndexReader>();
    ReaderUtil.GatherSubReaders(subReaders, reader);
    
    // Loop through all subreaders. While subReaderId is higher than the
    // maximum document id in the subreader, go to next.
    var subReaderId = documentId;
    var subReader = subReaders.First(sub => {
        if (sub.MaxDoc() < subReaderId) {
            subReaderId -= sub.MaxDoc();
            return false;
        }
    
        return true;
    });
    
    var values = FieldCache_Fields.DEFAULT.GetInts(subReader, "id");
    var value = values[subReaderId];
    

    【讨论】:

      猜你喜欢
      • 2020-02-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-04
      • 2011-07-02
      • 1970-01-01
      • 1970-01-01
      • 2019-10-18
      • 1970-01-01
      相关资源
      最近更新 更多