使用通配符搜索进行搜索时,在文档中匹配术语

时间:2011-04-10 17:26:00

标签: c# lucene lucene.net

我正在寻找一种方法,在Lucene中使用waldcard搜索找到文档中匹配的术语。我使用解释器试图找到条款,但这失败了。相关代码的一部分如下。

ScoreDoc[] myHits = myTopDocs.scoreDocs;
int hitsCount = myHits.Length;
for (int myCounter = 0; myCounter < hitsCount; myCounter++)
{
    Document doc = searcher.Doc(myHits[myCounter].doc);
    Explanation explanation = searcher.Explain(myQuery, myCounter);
    string myExplanation = explanation.ToString();
    ...

当我搜索说micro *时,会找到文件并进入循环,但myExplanation包含NON-MATCH而没有其他信息。

如何获得本文档中的术语?

非常感谢任何帮助。

此致

2 个答案:

答案 0 :(得分:8)

    class TVM : TermVectorMapper
    {
        public List<string> FoundTerms = new List<string>();
        HashSet<string> _termTexts = new HashSet<string>();

        public TVM(Query q, IndexReader r) : base()
        {
            List<Term> allTerms = new List<Term>();
            q.Rewrite(r).ExtractTerms(allTerms);
            foreach (Term t in allTerms) _termTexts.Add(t.Text());
        }

        public override void SetExpectations(string field, int numTerms, bool storeOffsets, bool storePositions)
        {
        }

        public override void Map(string term, int frequency, TermVectorOffsetInfo[] offsets, int[] positions)
        {
            if (_termTexts.Contains(term)) FoundTerms.Add(term);
        }
    }

    void TermVectorMapperTest()
    {
        RAMDirectory dir = new RAMDirectory();
        IndexWriter writer = new IndexWriter(dir, new Lucene.Net.Analysis.Standard.StandardAnalyzer(), true);
        Document d = null;

        d = new Document();
        d.Add(new Field("text", "microscope aaa", Field.Store.YES, Field.Index.ANALYZED,Field.TermVector.WITH_POSITIONS_OFFSETS));
        writer.AddDocument(d);

        d = new Document();
        d.Add(new Field("text", "microsoft bbb", Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS));
        writer.AddDocument(d);

        writer.Close();

        IndexReader reader = IndexReader.Open(dir);
        IndexSearcher searcher = new IndexSearcher(reader);

        QueryParser queryParser = new QueryParser("text", new Lucene.Net.Analysis.Standard.StandardAnalyzer());
        queryParser.SetMultiTermRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE); 
        Query query = queryParser.Parse("micro*");

        TopDocs results = searcher.Search(query, 5);
        System.Diagnostics.Debug.Assert(results.TotalHits == 2);

        TVM tvm = new TVM(query, reader);
        for (int i = 0; i < results.ScoreDocs.Length; i++)
        {
            Console.Write("DOCID:" + results.ScoreDocs[i].Doc + " > ");
            reader.GetTermFreqVector(results.ScoreDocs[i].Doc, "text", tvm);
            foreach (string term in tvm.FoundTerms) Console.Write(term + " ");
            tvm.FoundTerms.Clear();
            Console.WriteLine();
        }
    }

答案 1 :(得分:4)

一种方法是使用荧光笔;另一种方法是通过使用适当的重写器调用myQuery.rewrite()来重写您的查询来模仿Highlighter所做的事情;这可能与你正在尝试的精神更接近。这会将查询重写为包含所有匹配条件的BooleanQuery;你可以很容易地从那些词中得到这些词。这足以让你前进吗?

这是我心中的想法;抱歉混淆re:重写查询;它在这里并不重要。

  TokenStream tokens = TokenSources.getAnyTokenStream(IndexReader reader, int docId, String field, Analyzer analyzer);
CharTermAttribute termAtt = tokens.addAttribute(CharTermAttribute.class);
while (tokens.incrementToken()) {
  // do something with termAtt, which holds the matched term
}