Lucene搜索语法 [英] Lucene Search Syntax

查看:63
本文介绍了Lucene搜索语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助找出在给定情况下要使用的查询类型.

I need help figuring out which query types to use in given situations.

我认为我的说法是正确的,如果我将"FORD"一词存储在lucene Field中,并且想找到完全匹配的内容,我会使用TermQuery吗?

I think i'm right in saying that if i stored the word "FORD" in a lucene Field and i wanted to find an exact match i would use a TermQuery?

但是,如果我要查找单词"FORD",那么该字段的内容存储为:-

But which query type should i use if I was looking for the word "FORD" where the contents of the field where stored as :-

"FORD | HONDA | SUZUKI"

"FORD|HONDA|SUZUKI"

如果我要搜索整个页面的内容,寻找一个短语怎么办?例如请帮助我"?

What if i was to search the contents of an entire page, looking for a phrase? such as "please help me"?

推荐答案

如果要在FORD|HONDA|SUZUKI中搜索FORD,请使用 Field.Index.ANALYZED 进行索引,或将其存储为以下内容:使用 TermQuery

If you want to search FORD in FORD|HONDA|SUZUKI, either index with Field.Index.ANALYZED, or store it as below to use TermQuery

var analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);
var fs = FSDirectory.Open("test.index");

//Index a Test Document
IndexWriter wr = new IndexWriter(fs, analyzer, true, IndexWriter.MaxFieldLength.LIMITED);
var doc = new Document();

doc.Add(new Field("Model", "FORD", Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.Add(new Field("Model", "HONDA", Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.Add(new Field("Model", "SUZUKI", Field.Store.YES, Field.Index.NOT_ANALYZED));

doc.Add(new Field("Text", @"What if i was to search the contents of an entire page, looking for a phrase? such as ""please help me""?", 
                    Field.Store.YES, Field.Index.ANALYZED));

wr.AddDocument(doc);
wr.Commit();

var reader = wr.GetReader();
var searcher = new IndexSearcher(reader);

//Use TermQuery for "NOT_ANALYZED" fields
var result = searcher.Search(new TermQuery(new Term("Model", "FORD")), 100);
foreach (var item in result.ScoreDocs)
{
    Console.WriteLine("1)" + reader.Document(item.Doc).GetField("Text").StringValue);
}

//Use QueryParser for "ANALYZED" fields
var qp = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "Text", analyzer);
result = searcher.Search(qp.Parse(@"""HELP ME"""), 100);
foreach (var item in result.ScoreDocs)
{
    Console.WriteLine("2)" + reader.Document(item.Doc).GetField("Text").StringValue);
}

TermQuery 表示您想搜索存储在索引中的术语,这取决于您对该字段进行索引的方式(NOT_ANALYZED,ANALYZED + WhichAnalyzer).它最常见的用法是与 NOT_ANALYZED 字段一起使用.

TermQuery means you want to search the term as it is stored in index which depends on how you indexed that field(NOT_ANALYZED, ANALYZED+WhichAnalyzer). Most common use of it is with NOT_ANALYZED fields.

您也可以在 ANALYZED 字段中使用 TermQuery ,但是您应该知道 analyzer 是如何对输入字符串进行标记化的.下面是一个示例,以查看分析器如何标记您的输入

You can use TermQuery with ANALYZED fields too, but then you should know how the analyzer tokenizes your input string. Below is a sample to see what how analyzers tokenize your input

var text = @"What if i was to search the contents of an entire page, looking for a phrase? such as ""please help me""?";
var analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30 );
//var analyzer = new WhitespaceAnalyzer();
//var analyzer = new KeywordAnalyzer();
//var analyzer = new SimpleAnalyzer();

var ts = analyzer.TokenStream("", new StringReader(text));
var termAttr = ts.GetAttribute<ITermAttribute>();

while (ts.IncrementToken())
{
    Console.Write("[" + termAttr.Term + "] " );    
}

这篇关于Lucene搜索语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆