lucene .net模糊多词

时间:2015-11-05 17:06:21

标签: lucene lucene.net

我希望在两个术语上组合模糊搜索,例如: - 'magikal~0.8'和'mistery'~0.8,应该返回“魔法”和“神秘”两个词都存在的结果。

我目前的代码如下


Directory createIndex(DataTable table)
        {
            var directory = new RAMDirectory();

            using (Analyzer analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30))
            using (var writer = new IndexWriter(directory, analyzer, new IndexWriter.MaxFieldLength(1000)))
            {
                foreach (DataRow row in table.Rows)
                {
                    var document = new Document();
                    document.Add(new Field("DishName", row["DishName"].ToString(), Field.Store.YES, Field.Index.ANALYZED));
                    document.Add(new Field("CustomisationID", row["CustomisationID"].ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));

                    writer.AddDocument(document);
                }

                writer.Optimize();
                writer.Flush(true, true, true);
            }
            return directory;
        }

private DataTable SearchDishName(string textSearch)
        {
            string MatchingCutomisationIDs = "0"; //There is no Dish with ID zero, this is just to easen the coding.. 
            var ds = new DataSet();
            ds.ReadXml(System.Web.HttpContext.Current.Server.MapPath("~/App_data/MyDataset.xml"));
            DataTable Sample = new DataTable();
            Sample = ds.Tables[0];
            var table = Sample.Clone();
            var Index = createIndex(Sample);
            using (var reader = IndexReader.Open(Index, true))
            using (var searcher = new IndexSearcher(reader))
            {
                using (Analyzer analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30))
                {
                    var queryParser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "DishName", analyzer);
                    var collector = TopScoreDocCollector.Create(1000, true);
                    try
                    {
                        var query = queryParser.Parse(textSearch);
                        searcher.Search(query, collector);
                    }
                    catch
                    { }
                    var matches = collector.TopDocs().ScoreDocs;
                    foreach (var item in matches)
                    {
                        var id = item.Doc;
                        var doc = searcher.Doc(id);
                        var row = table.NewRow();
                        row["CustomisationID"] = doc.GetField("CustomisationID").StringValue;
                        table.Rows.Add(row);
                    }
                }
            }
            return table;
        }


此代码的另一个问题是。如果我使用&&运行正常查询操作员仍然无法获得准确的结果 例如: - 查询“魔法”和“神秘”的结果,包括只有“神奇”或只有“神秘”的情况。

0 个答案:

没有答案