首页 > 解决方案 > Lucene.net 不返回搜索结果

问题描述

我有 8 个文本文件,其中包含大约 500 万行类似的编码数据-

- 通过文档 #12478901 将 TRNS_Legacy 类型的路径 TRNS_Legacy_773 分配给 DEV\svcInformational_1_7。现在有 35 条这样的路径,其中 1 条正在使用中

-Msg (len:482) 在 TRNS_Legacy_773 上发送:'FETCHIT IMG 771777777291;1;577;17;SAI13761671;1;577;IMG 771777771191;1;1;15;707747 261174767;23;19;07;187;34 315;316;318;327;486;480;670;230;238;239;247;279;427;421;109;512;924;935;936;IMG 771777777501;1;1;15;707747 261174767; 9;5;64;65;66;01;06;122;184;940;IMG 771777777641;1;1;15;82748/4141541;9;245;246;249;248;244;243;242;241 ;620;IMG 771777771321;1;1;15;707747 261174767;29;4;19;25;20;62;64;65;66;07;87;81;82;122;187;194;241;245 ;370;315;316;319;579;517;512;525;587;027;935;936;'

- 消息 rcvd 并在 TRNS_Legacy_773 上相关,相关因子为“IAS1376167”。响应时间为 31 毫秒。

我已经使用 lucene.net 标准分析器对文件进行了索引。当我尝试搜索“4141541”或“FETCHIT”等子字符串时,搜索返回 0 个结果。我使用了 Luke,可以看到索引中存在数据。我可以使用行号进行搜索,但不能使用文本。有人可以帮我吗?

我尝试过使用通配符搜索,尝试不同的查询,尝试不同的分析器,将我的文本保持为标记化和非标记化,但它要么返回 0 个结果,要么只返回 1 或 2 行,即使应该返回 1000 行。

private Analyzer analyzer = new StandardAnalyzer();

建筑指数-

public void BuildIndex(string item)
    {

        string indexPath = string.Format(baseIndexPath, item);
        if (System.IO.Directory.Exists(indexPath))
        {
            System.IO.Directory.Delete(indexPath, true);
        }

        luceneIndexDirectory = FSDirectory.GetDirectory(indexPath);
        writer = new IndexWriter(indexPath, analyzer, true);
        //writer = new IndexWriter(luceneIndexDirectory, analyzer, true);

        string file = string.Format(@"LogFile.txt");
        string line=string.Empty;
        int count = 0;
        StreamReader fileReader = new StreamReader(file);
        while ((line = fileReader.ReadLine()) != null)
        {
            count++;
            Document doc = new Document();
            doc.Add(new Field("LineNumber", count.ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
            doc.Add(new Field("LineText", line, Field.Store.YES, Field.Index.UN_TOKENIZED));

            writer.AddDocument(doc);
        }
        writer.Optimize();
        writer.Flush();
        writer.Close();
        luceneIndexDirectory.Close();
    }

公共 IEnumerable 搜索(字符串 searchTerm){

        IndexSearcher searcher = new IndexSearcher(luceneIndexDirectory);
        QueryParser parser = new QueryParser("LineText", analyzer);
        TermQuery tQuery = new TermQuery(new Term("LineText",searchTerm));
        Query query = parser.Parse(searchTerm);
        Hits hitsFound = searcher.Search(query); //returns 0 results
        hitsFound = searcher.Search(tQuery); //returns 0 results
        List<SampleDataFileRow> results = new List<SampleDataFileRow>();
        SampleDataFileRow sampleDataFileRow = null;

        for (int i = 0; i < hitsFound.Length(); i++)
        {
            sampleDataFileRow = new SampleDataFileRow();
            Document doc = hitsFound.Doc(i);
            sampleDataFileRow.LineNumber = int.Parse(doc.Get("LineNumber"));
            sampleDataFileRow.LineText = doc.Get("LineText");


            results.Add(sampleDataFileRow);
        }

        return results.ToList();
    }

标签: .netlucenelucene.net

解决方案


我怀疑问题在于 StandardAnalyzer 如何分解令牌。当您使用 Luke 时,我怀疑您在条款中看不到4141541 或看不到FETCHIT单独的标记,LineText如果您想搜索这些值并获得结果,则需要它们。


推荐阅读