lucene搜索字段包含空格

时间:2014-08-26 04:38:46

标签: search lucene

这是我的Lucene.Net示例。

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;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Directory = Lucene.Net.Store.Directory;
using Version = Lucene.Net.Util.Version;

namespace LuceneTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello Lucene.net");

            var Book1 = new Document();
            Book1.Add(new Field("Id", "1", Field.Store.YES, Field.Index.NOT_ANALYZED));
            Book1.Add(new Field("Author", "Tom and Jerry".ToLower(), Field.Store.YES, Field.Index.ANALYZED));
            Book1.Add(new Field("Series", "Science".ToLower(), Field.Store.YES, Field.Index.NOT_ANALYZED));
            Book1.Add(new Field("Location", "Melbourne", Field.Store.YES, Field.Index.NOT_ANALYZED));



            var Book2 = new Document();
            Book2.Add(new Field("Id", "2", Field.Store.YES, Field.Index.NOT_ANALYZED));
            Book2.Add(new Field("Author", "Michael Jordan".ToLower(), Field.Store.YES, Field.Index.ANALYZED));
            Book2.Add(new Field("Series", "Science and Fiction".ToLower(), Field.Store.YES, Field.Index.NOT_ANALYZED));
            Book2.Add(new Field("Location", "Sydeny", Field.Store.YES, Field.Index.NOT_ANALYZED));

            var Book3 = new Document();
            Book3.Add(new Field("Id", "3", Field.Store.YES, Field.Index.NOT_ANALYZED));
            Book3.Add(new Field("Author", "David Beckham".ToLower(), Field.Store.YES, Field.Index.ANALYZED));
            Book3.Add(new Field("Series", "Math Science".ToLower(), Field.Store.YES, Field.Index.NOT_ANALYZED));
            Book3.Add(new Field("Location", "London", Field.Store.YES, Field.Index.NOT_ANALYZED));

            var Book4 = new Document();
            Book4.Add(new Field("Id", "4", Field.Store.YES, Field.Index.NOT_ANALYZED));
            Book4.Add(new Field("Author", "Michael David".ToLower(), Field.Store.YES, Field.Index.ANALYZED));
            Book4.Add(new Field("Series", "Computer Science".ToLower(), Field.Store.YES, Field.Index.NOT_ANALYZED));
            Book4.Add(new Field("Location", "New York", Field.Store.YES, Field.Index.NOT_ANALYZED));

            var Book5 = new Document();
            Book5.Add(new Field("Id", "5", Field.Store.YES, Field.Index.NOT_ANALYZED));
            Book5.Add(new Field("Author", "Kobe Bryant".ToLower(), Field.Store.YES, Field.Index.ANALYZED));
            Book5.Add(new Field("Series", "Computer".ToLower(), Field.Store.YES, Field.Index.NOT_ANALYZED));
            Book5.Add(new Field("Location", "Los Angeles", Field.Store.YES, Field.Index.NOT_ANALYZED));


            Directory directory = FSDirectory.Open(new DirectoryInfo(Environment.CurrentDirectory + "\\LuceneIndex"));

            Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
            Analyzer analyzer2 = new KeywordAnalyzer();

            var writer = new IndexWriter(directory, analyzer, true, IndexWriter.MaxFieldLength.LIMITED);

            writer.AddDocument(Book1);
            writer.AddDocument(Book2);
            writer.AddDocument(Book3);
            writer.AddDocument(Book4);
            writer.AddDocument(Book5);

            writer.Optimize();
            writer.Dispose();

            IndexReader indexReader = IndexReader.Open(directory, true);
            Searcher seacher = new IndexSearcher(indexReader);

            var queryParser = new QueryParser(Version.LUCENE_30, "Series", analyzer);
            var query = queryParser.Parse("\"Computer Science\"");
            Console.WriteLine("Searching For " + query);

            TopDocs resultDocs = seacher.Search(query, indexReader.MaxDoc);

            var hits = resultDocs.ScoreDocs;
            foreach (var hit in hits)
            {
                var documentFromSearch = seacher.Doc(hit.Doc);
                Console.WriteLine(documentFromSearch.Get("Id") + ", " + documentFromSearch.Get("Author") + ", " + documentFromSearch.Get("Location") + ", " + documentFromSearch.Get("Series"));
            }

            if (hits.Count() == 0)
                Console.WriteLine("No result.");
            Console.ReadKey();
        }
    }
}

Line 78, var query = queryParser.Parse(“\”Computer Science \“”);` 如果我制作参数“计算机科学”,它将是“计算机”或“科学”系列的书。(记录1和5)我想要的实际上是具有“计算机科学”系列的记录(Book4) )。我应该改变什么? 提前谢谢

1 个答案:

答案 0 :(得分:0)

处理此问题的常用方法是在Lucene中使用短语查询 - 例如,在查询语法中,您可以使用"Computer Science" book4在计算机科学中查找“book4”。