Lucene短语查询不起作用

Lucene短语查询不起作用,lucene,lucene.net,Lucene,Lucene.net,我不知道如何使短语查询工作。它返回精确的数学,但slop选项似乎没有什么区别 这是我的密码: static void Main(string[] args) { using (Directory directory = new RAMDirectory()) { Analyzer analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29);

我不知道如何使短语查询工作。它返回精确的数学,但slop选项似乎没有什么区别
这是我的密码:

static void Main(string[] args)
    { 
     using (Directory directory = new RAMDirectory())
        {
            Analyzer analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29);

            using (IndexWriter writer = new IndexWriter(directory, analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED))
            {
                // index a few documents
                writer.AddDocument(createDocument("1", "henry morgan"));
                writer.AddDocument(createDocument("2", "henry junior morgan"));
                writer.AddDocument(createDocument("3", "henry immortal jr morgan"));
                writer.AddDocument(createDocument("4", "morgan henry"));
            }

            // search for documents that have "foo bar" in them
            String sentence = "henry morgan";
            IndexSearcher searcher = new IndexSearcher(directory, true);
            PhraseQuery query = new PhraseQuery()
            {
                //allow inverse order
                Slop = 3
            };

            query.Add(new Term("contents", sentence));

            // display search results
            List<string> results = new List<string>();
            Console.WriteLine("Looking for \"{0}\"...", sentence);
            TopDocs topDocs = searcher.Search(query, 100);
            foreach (ScoreDoc scoreDoc in topDocs.ScoreDocs)
            {
                var matchedContents = searcher.Doc(scoreDoc.Doc).Get("contents");
                results.Add(matchedContents);
                Console.WriteLine("Found: {0}", matchedContents);
            }
        }

private static Document createDocument(string id, string content)
    {
        Document doc = new Document();
        doc.Add(new Field("id", id, Field.Store.YES, Field.Index.NOT_ANALYZED));
        doc.Add(new Field("contents", content, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS));
        return doc;
    }
static void Main(字符串[]args)
{ 
使用(Directory Directory=new RAMDirectory())
{
Analyzer Analyzer=新的StandardAnalyzer(Lucene.Net.Util.Version.Lucene_29);
使用(IndexWriter writer=new IndexWriter(目录,分析器,true,IndexWriter.MaxFieldLength.UNLIMITED))
{
//索引一些文件
writer.AddDocument(createDocument(“1”,“henry morgan”));
writer.AddDocument(createDocument(“2”,“亨利·小摩根”));
writer.AddDocument(createDocument(“3”,“亨利•摩根”);
writer.AddDocument(createDocument(“4”,“摩根·亨利”));
}
//搜索包含“foo-bar”的文档
字符串句子=“亨利·摩根”;
IndexSearcher=newindexSearcher(目录,true);
PhraseQuery查询=新建PhraseQuery()
{
//允许逆序
斜率=3
};
添加(新术语(“内容”,句子));
//显示搜索结果
列表结果=新列表();
Console.WriteLine(“查找\“{0}\”…”,句子);
TopDocs TopDocs=searcher.Search(查询,100);
foreach(topDocs.ScoreDocs中的ScoreDoc ScoreDoc)
{
var matchedContents=searcher.Doc(scoreDoc.Doc).Get(“内容”);
结果。添加(匹配内容);
WriteLine(“找到:{0}”,matchedContents);
}
}
私有静态文档createDocument(字符串id、字符串内容)
{
单据单据=新单据();
添加文档(新字段(“id”,id,Field.Store.YES,Field.Index.NOT_analysis));
添加文档(新字段(“内容”,内容,Field.Store.YES,Field.Index.ANALYZED,Field.TermVector.WITH_POSITIONS_offset));
退货单;
}

我认为除了id=3的文档之外,所有选项都应该匹配,但只有第一个选项匹配。我是否遗漏了什么?

在Lucene In Action 2nd中,3.4.6按短语搜索:短语查询

PhraseQuery使用此信息定位术语彼此之间在一定距离内的文档

当然,一个普通术语查询可以找到这个文档 知道这两个词中的任何一个,但在这种情况下,我们只需要文档 有两个单词并排的短语 (快速狐狸)或中间有一个词(快速[无关]狐狸)

所以短语查询实际上是在术语之间使用的,这一章中的示例代码也证明了这一点 分析后将是亨利和摩根。因此,您不能将“亨利摩根”添加为一个术语

/*
   Sets the number of other words permitted between words 
   in query phrase.If zero, then this is an exact phrase search.  
*/
 public void setSlop(int s) { slop = s; }
setSlop的定义可以进一步解释这种情况。 在你的代码上做了一点修改后,我就把它搞定了

// code in Scala
val query = new PhraseQuery();
query.setSlop(3)
List("henry", "morgan").foreach { word =>
    query.add(new Term("contents", word))
}
在本例中,这四个文档将全部匹配。如果您有任何进一步的问题,我建议您阅读《Lucene In Action 2》中的那一章。 这可能会有帮助