在 Lucene.net 中创建 WildcardQuery

Create WildcardQuery in Lucene.net

i 在 c#

中使用 lucene.net v3.0.3

我这样创建一个查询:

         var analyzer = new StandardAnalyzer(Version.LUCENE_29);            
         BooleanQuery query = new BooleanQuery();
         var firstParser = new QueryParser(Version.LUCENE_29, firstKey, analyzer);
         var firstFilter = parseQuery(firstValue, firstParser);
         query.Add(firstFilter, Occur.MUST);

查询工作正常,但只有在输入全字时 像苹果 - 板

但是我需要在输入Letter时进行strat search 所以我像这样使用 wildcardQuery

        WildcardQuery wildcardQuery = new WildcardQuery(new Term(firstKey, "*" + firstValue + "*"));
        query.Add(wildcardQuery, Occur.MUST);

当我输入任何字母时该查询显示结果但是当输入 space 结果消失了

我需要一个查询这是这个结果的组合

请帮帮我

这是示例模糊搜索 对不起,我是 Lucene 的新手,但它可以工作

using Lucene.Net.Analysis;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Documents;
using Lucene.Net.Index;
using Lucene.Net.QueryParsers;
using Lucene.Net.Search;
using Lucene.Net.Store;
using System;
using System.Collections.Generic;

namespace FuzzySearchTest
{
    public class Program
    {
        static void Main(string[] args)
        {

            //
            // Initialize the Directory and the IndexWriter.
            //
            var dirInfo = new System.IO.DirectoryInfo("LuceneIndex");
            Directory directory = FSDirectory.Open(dirInfo);
            Analyzer analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29);
            IndexWriter writer = new IndexWriter(directory, analyzer, IndexWriter.MaxFieldLength.LIMITED);

            //
            // Add Documents to the Index.
            //
            Document doc = new Document();
            doc.Add(new Field("id", "1", Field.Store.YES, Field.Index.NO));
            doc.Add(new Field("postBody", "my favorit animal is cat", Field.Store.YES, Field.Index.ANALYZED));
            writer.AddDocument(doc);

            writer.Optimize();
            writer.Commit();
            //writer.Close();

            //
            // Create the Query.
            //
            QueryParser parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "postBody", analyzer);
            //This is Fuzzy  between 0.5f and 1.0f
            parser.FuzzyMinSim = 0.7f;

            //similor text for search
            Query query = parser.Parse("fevorit  cat");

            //
            // Pass the Query to the IndexSearcher.
            //
            IndexSearcher searcher = new IndexSearcher(directory, readOnly: true);
            TopDocs hits = searcher.Search(query, 200);

            //
            // Iterate over the Results.
            //
            int resultsCount = hits.ScoreDocs.Length;
            Console.WriteLine("Found {0} results", resultsCount);
            for (int ixResult = 0; ixResult < resultsCount; ixResult++)
            {
                var docResult = searcher.Doc(hits.ScoreDocs[ixResult].Doc);
                float score = hits.ScoreDocs[ixResult].Score;
                Console.WriteLine("Result num {0}, score {1}", ixResult + 1, score);
                Console.WriteLine("ID: {0}", doc.Get("id"));
                Console.WriteLine("Text found: {0}" + Environment.NewLine, doc.Get("postBody"));
            }

            Console.WriteLine("Press ENTER to quit...");
            Console.ReadLine();
        }
    }
}

请添加 nugets

Lucene.Net
configurationManager

希望对你有所帮助