本文整理汇总了C#中Lucene.Net.Search.PhraseQuery类的典型用法代码示例。如果您正苦于以下问题:C# PhraseQuery类的具体用法?C# PhraseQuery怎么用?C# PhraseQuery使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
PhraseQuery类属于Lucene.Net.Search命名空间,在下文中一共展示了PhraseQuery类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。
示例1: TestPhrase
public virtual void TestPhrase()
{
PhraseQuery query = new PhraseQuery();
query.Add(new Term("Field", "seventy"));
query.Add(new Term("Field", "seven"));
CheckHits(query, new int[]{77, 177, 277, 377, 477, 577, 677, 777, 877, 977});
}
开发者ID:emtees,项目名称:old-code,代码行数:7,代码来源:TestBasics.cs
示例2: VisitPhraseQuery
public override Query VisitPhraseQuery(PhraseQuery phraseq)
{
_dump.Append("PhraseQ(");
var terms = phraseq.GetTerms();
PhraseQuery newQuery = null;
int index = 0;
int count = terms.Length;
while (index < count)
{
var visitedTerm = VisitTerm(terms[index]);
if (newQuery != null)
{
newQuery.Add(visitedTerm);
}
else if (visitedTerm != terms[index])
{
newQuery = new PhraseQuery();
for (int i = 0; i < index; i++)
newQuery.Add(terms[i]);
newQuery.Add(visitedTerm);
}
index++;
if (index < count)
_dump.Append(", ");
}
_dump.Append(", Slop:").Append(phraseq.GetSlop()).Append(BoostToString(phraseq)).Append(")");
if (newQuery != null)
return newQuery;
return phraseq;
}
开发者ID:jhuntsman,项目名称:FlexNet,代码行数:32,代码来源:DumpVisitor.cs
示例3: ExactPhraseScorer
internal ExactPhraseScorer(Weight weight, PhraseQuery.PostingsAndFreq[] postings, Similarity.SimScorer docScorer)
: base(weight)
{
this.DocScorer = docScorer;
ChunkStates = new ChunkState[postings.Length];
EndMinus1 = postings.Length - 1;
// min(cost)
Cost_Renamed = postings[0].Postings.Cost();
for (int i = 0; i < postings.Length; i++)
{
// Coarse optimization: advance(target) is fairly
// costly, so, if the relative freq of the 2nd
// rarest term is not that much (> 1/5th) rarer than
// the first term, then we just use .nextDoc() when
// ANDing. this buys ~15% gain for phrases where
// freq of rarest 2 terms is close:
bool useAdvance = postings[i].DocFreq > 5 * postings[0].DocFreq;
ChunkStates[i] = new ChunkState(postings[i].Postings, -postings[i].Position, useAdvance);
if (i > 0 && postings[i].Postings.NextDoc() == DocIdSetIterator.NO_MORE_DOCS)
{
NoDocs = true;
return;
}
}
}
开发者ID:Cefa68000,项目名称:lucenenet,代码行数:29,代码来源:ExactPhraseScorer.cs
示例4: Phrase
public static PhraseQuery Phrase(this BooleanQuery inputQuery, BooleanClause.Occur occur = null)
{
BooleanQuery parentQuery = GetParentQuery(inputQuery);
PhraseQuery query = new PhraseQuery();
SetOccurValue(inputQuery, ref occur);
parentQuery.Add(query, occur);
return query;
}
开发者ID:Code-Miners,项目名称:Lucinq,代码行数:8,代码来源:BooleanQueryExtensions.cs
示例5: ProcessRequest
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string searchKey = context.Request["wd"];
string indexPath = context.Server.MapPath("../IndexData");
FSDirectory directory = FSDirectory.Open(new DirectoryInfo(indexPath), new NoLockFactory());
IndexReader reader = IndexReader.Open(directory, true);
IndexSearcher searcher = new IndexSearcher(reader);
//搜索条件
PhraseQuery query = new PhraseQuery();
//把用户输入的关键字进行分词
foreach (string word in Picture.Utility.SplitContent.SplitWords(searchKey))
{
query.Add(new Term("tag", word));
}
//query.Add(new Term("content", "C#"));//多个查询条件时 为且的关系
query.SetSlop(100); //指定关键词相隔最大距离
//TopScoreDocCollector盛放查询结果的容器
TopScoreDocCollector collector = TopScoreDocCollector.create(1000, true);
searcher.Search(query, null, collector);//根据query查询条件进行查询,查询结果放入collector容器
//TopDocs 指定0到GetTotalHits() 即所有查询结果中的文档 如果TopDocs(20,10)则意味着获取第20-30之间文档内容 达到分页的效果
ScoreDoc[] docs = collector.TopDocs(0,10).scoreDocs;
//展示数据实体对象集合
var tagModels = new List<Picture.Model.TagModel>();
for (int i = 0; i < docs.Length; i++)
{
int docId = docs[i].doc;//得到查询结果文档的id(Lucene内部分配的id)
Document doc = searcher.Doc(docId);//根据文档id来获得文档对象Document
Picture.Model.TagModel tag = new Picture.Model.TagModel();
//picture.ImgSummary = doc.Get("summary");
tag.TagName= Picture.Utility.SplitContent.HightLight(searchKey, doc.Get("tag"));
//book.ContentDescription = doc.Get("content");//未使用高亮
//搜索关键字高亮显示 使用盘古提供高亮插件
//book.ContentDescription = Picture.Utility.SplitContent.HightLight(Request.QueryString["SearchKey"], doc.Get("content"));
tag.TId = Convert.ToInt32(doc.Get("id"));
tagModels.Add(tag);
}
SearchPreviewResult result = new SearchPreviewResult()
{
q=searchKey,
p=false
};
foreach (var item in tagModels)
{
result.s.Add(item.TagName);
}
System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer();
context.Response.Write(jss.Serialize(result));
}
开发者ID:RunningStudent,项目名称:PictureWebSite,代码行数:57,代码来源:SearchPreview.ashx.cs
示例6: Test1
public virtual void Test1()
{
BooleanQuery q = new BooleanQuery();
PhraseQuery phraseQuery = new PhraseQuery();
phraseQuery.Slop = 1;
phraseQuery.Add(new Term(FIELD, "w1"));
phraseQuery.Add(new Term(FIELD, "w2"));
q.Add(phraseQuery, Occur.MUST);
q.Add(Snear(St("w2"), Sor("w5", "zz"), 4, true), Occur.SHOULD);
q.Add(Snear(Sf("w3", 2), St("w2"), St("w3"), 5, true), Occur.SHOULD);
Query t = new FilteredQuery(new TermQuery(new Term(FIELD, "xx")), new ItemizedFilter(new int[] { 1, 3 }));
t.Boost = 1000;
q.Add(t, Occur.SHOULD);
t = new ConstantScoreQuery(new ItemizedFilter(new int[] { 0, 2 }));
t.Boost = 30;
q.Add(t, Occur.SHOULD);
DisjunctionMaxQuery dm = new DisjunctionMaxQuery(0.2f);
dm.Add(Snear(St("w2"), Sor("w5", "zz"), 4, true));
dm.Add(new TermQuery(new Term(FIELD, "QQ")));
BooleanQuery xxYYZZ = new BooleanQuery();
xxYYZZ.Add(new TermQuery(new Term(FIELD, "xx")), Occur.SHOULD);
xxYYZZ.Add(new TermQuery(new Term(FIELD, "yy")), Occur.SHOULD);
xxYYZZ.Add(new TermQuery(new Term(FIELD, "zz")), Occur.MUST_NOT);
dm.Add(xxYYZZ);
BooleanQuery xxW1 = new BooleanQuery();
xxW1.Add(new TermQuery(new Term(FIELD, "xx")), Occur.MUST_NOT);
xxW1.Add(new TermQuery(new Term(FIELD, "w1")), Occur.MUST_NOT);
dm.Add(xxW1);
DisjunctionMaxQuery dm2 = new DisjunctionMaxQuery(0.5f);
dm2.Add(new TermQuery(new Term(FIELD, "w1")));
dm2.Add(new TermQuery(new Term(FIELD, "w2")));
dm2.Add(new TermQuery(new Term(FIELD, "w3")));
dm.Add(dm2);
q.Add(dm, Occur.SHOULD);
BooleanQuery b = new BooleanQuery();
b.MinimumNumberShouldMatch = 2;
b.Add(Snear("w1", "w2", 1, true), Occur.SHOULD);
b.Add(Snear("w2", "w3", 1, true), Occur.SHOULD);
b.Add(Snear("w1", "w3", 3, true), Occur.SHOULD);
q.Add(b, Occur.SHOULD);
Qtest(q, new int[] { 0, 1, 2 });
}
开发者ID:Cefa68000,项目名称:lucenenet,代码行数:55,代码来源:TestComplexExplanations.cs
示例7: AddExactFieldValueClause
protected void AddExactFieldValueClause(Index index, BooleanQuery query, string fieldName, string fieldValue)
{
//if (String.IsNullOrEmpty(fieldValue)) return;
fieldValue = IdHelper.ProcessGUIDs(fieldValue);
var phraseQuery = new PhraseQuery();
phraseQuery.Add(new Term(fieldName.ToLowerInvariant(), fieldValue));
query.Add(phraseQuery, BooleanClause.Occur.MUST);
}
开发者ID:katebutenko,项目名称:SitecoreSearchContrib,代码行数:11,代码来源:FieldSearchParam.cs
示例8: TestExactPhraseVersusBooleanAnd
public virtual void TestExactPhraseVersusBooleanAnd()
{
Term t1 = RandomTerm();
Term t2 = RandomTerm();
PhraseQuery q1 = new PhraseQuery();
q1.Add(t1);
q1.Add(t2);
BooleanQuery q2 = new BooleanQuery();
q2.Add(new TermQuery(t1), Occur.MUST);
q2.Add(new TermQuery(t2), Occur.MUST);
AssertSubsetOf(q1, q2);
}
开发者ID:WakeflyCBass,项目名称:lucenenet,代码行数:12,代码来源:TestSimpleSearchEquivalence.cs
示例9: SearchContent
/// <summary>
/// 搜索
/// </summary>
protected void SearchContent(string kw)
{
string indexPath = @"D:\lucenedir";
kw = kw.ToLower();//默认情况下盘古分词区分大小写,需转换成小写进行搜索
FSDirectory directory = FSDirectory.Open(new DirectoryInfo(indexPath), new NoLockFactory());
IndexReader reader = IndexReader.Open(directory, true);
IndexSearcher searcher = new IndexSearcher(reader);
//搜索条件
PhraseQuery queryMsg = new PhraseQuery();
foreach (string word in Common.WebCommon.PanGuSplit(kw))//先用空格,让用户去分词,空格分隔的就是词“计算机 专业”
{
queryMsg.Add(new Term("msg", word));//根据文章内容进行搜索
}
//query.Add(new Term("body","语言"));--可以添加查询条件,两者是add关系.顺序没有关系.
//query.Add(new Term("body", "大学生"));
queryMsg.SetSlop(100);//多个查询条件的词之间的最大距离.在文章中相隔太远 也就无意义.(例如 “大学生”这个查询条件和"简历"这个查询条件之间如果间隔的词太多也就没有意义了。)
PhraseQuery queryTitle = new PhraseQuery();
foreach (string word in Common.WebCommon.PanGuSplit(kw))
{
queryTitle.Add(new Term("title", word));
}
queryTitle.SetSlop(100);
BooleanQuery query = new BooleanQuery();
query.Add(queryMsg, BooleanClause.Occur.SHOULD);
query.Add(queryTitle, BooleanClause.Occur.SHOULD);
//TopScoreDocCollector是盛放查询结果的容器
TopScoreDocCollector collector = TopScoreDocCollector.create(1000, true);
searcher.Search(query, null, collector);//根据query查询条件进行查询,查询结果放入collector容器
ScoreDoc[] docs = collector.TopDocs(0, collector.GetTotalHits()).scoreDocs;//得到所有查询结果中的文档,GetTotalHits():表示总条数 TopDocs(300, 20);//表示得到 300(从300开始)到320(结束)的文档内容.可以用来实现分页功能
List<SearchResult> list = new List<SearchResult>();
for (int i = 0; i < docs.Length; i++)
{
//搜索ScoreDoc[]只能获得文档的id,这样不会把查询结果的Document一次性加载到内存中。降低了内存压力,需要获得文档的详细内容的时候通过searcher.Doc来根据文档id来获得文档的详细内容对象Document.
int docId = docs[i].doc;//得到查询结果文档的id(Lucene内部分配的id)
Document doc = searcher.Doc(docId);//找到文档id对应的文档详细信息
SearchResult result = new SearchResult();
result.ContentDescription = WebCommon.Highlight(kw,WebCommon.CutString(doc.Get("msg"),150));//分词高亮显示
result.Title = doc.Get("title");
result.Id = Convert.ToInt32(doc.Get("id"));
result.PublishDate = Convert.ToDateTime(doc.Get("PublishDate"));
result.ISBN = doc.Get("ISBN");
result.Author = doc.Get("Author");
result.UnitPrice = Convert.ToDecimal(doc.Get("UnitPrice"));
list.Add(result);
}
this.BookListRepeater.DataSource = list;
this.BookListRepeater.DataBind();
AddKeyWord(kw);
}
开发者ID:hdkn235,项目名称:OnlineBookShop,代码行数:56,代码来源:SearchBooks.aspx.cs
示例10: GetSearchQuery
public string GetSearchQuery(params ISearchableMetadataValue[] searchableMetadataValues)
{
var query = new PhraseQuery();
foreach (var searchableMetadataValue in searchableMetadataValues)
{
query.Add(new Term(searchableMetadataValue.Metadata.SearchName, searchableMetadataValue.Value));
}
var filter = query.ToString();
return GetSearchQuery(filter, new SearchableMetadata[] { });
}
开发者ID:telefunkenvf14,项目名称:Orc.Search,代码行数:13,代码来源:SearchQueryService.cs
示例11: GetFieldQuery
// This is a simplified query builder which works for single Terms and single Phrases
// Returns null, TermQuery, or PhraseQuery
public static Lucene.Net.Search.Query GetFieldQuery(Analyzer analyzer, string field, string queryText)
{
TokenStream stream = analyzer.TokenStream(field, new StringReader(queryText));
TokenFilter filter = new CachingTokenFilter(stream);
filter.Reset();
// This attribute way of getting token properties isn't very good, but it's the non-obsolete one.
var attr1 = filter.GetAttribute<ITermAttribute>();
Func<string> getText = () => attr1 != null ? attr1.Term : null;
Func<int> getPositionIncrement;
if (filter.HasAttribute<IPositionIncrementAttribute>())
{
var attr = filter.GetAttribute<IPositionIncrementAttribute>();
getPositionIncrement = () => attr.PositionIncrement;
}
else
{
getPositionIncrement = () => 1;
}
// 0 tokens
if (!filter.IncrementToken())
{
return new BooleanQuery();
}
// 1 token?
string token1 = getText();
int position = 0;
if (!filter.IncrementToken())
{
return new TermQuery(new Term(field, token1));
}
// many tokens - handle first token
PhraseQuery ret = new PhraseQuery();
ret.Add(new Term(field, token1));
do
{
// handle rest of tokens
string tokenNext = getText();
position += getPositionIncrement();
ret.Add(new Term(field, tokenNext), position);
}
while (filter.IncrementToken());
return ret;
}
开发者ID:atrevisan,项目名称:NuGetGallery,代码行数:52,代码来源:AnalysisHelper.cs
示例12: TestANDPhrase
public virtual void TestANDPhrase()
{
PhraseQuery phrase1 = new PhraseQuery();
phrase1.Add(new Term("field", "foo"));
phrase1.Add(new Term("field", "bar"));
PhraseQuery phrase2 = new PhraseQuery();
phrase2.Add(new Term("field", "star"));
phrase2.Add(new Term("field", "wars"));
BooleanQuery expected = new BooleanQuery();
expected.Add(phrase1, BooleanClause.Occur.MUST);
expected.Add(phrase2, BooleanClause.Occur.MUST);
assertEquals(expected, Parse("\"foo bar\"+\"star wars\""));
}
开发者ID:ChristopherHaws,项目名称:lucenenet,代码行数:14,代码来源:TestSimpleQueryParser.cs
示例13: SetUp
public virtual void SetUp()
{
directory = new RAMDirectory();
IndexWriter writer = new IndexWriter(directory, new WhitespaceAnalyzer(), true);
Document doc = new Document();
doc.Add(Field.Text("Field", "one two three four five"));
writer.AddDocument(doc);
writer.Optimize();
writer.Close();
searcher = new IndexSearcher(directory);
query = new PhraseQuery();
}
开发者ID:runefs,项目名称:Marvin,代码行数:15,代码来源:TestPhraseQuery.cs
示例14: BuildExactFieldValueClause
public static Query BuildExactFieldValueClause(Index index, string fieldName, string fieldValue)
{
Assert.ArgumentNotNull(index, "Index");
if (string.IsNullOrEmpty(fieldName) || string.IsNullOrEmpty(fieldValue))
{
return null;
}
fieldValue = IdHelper.ProcessGUIDs(fieldValue);
var phraseQuery = new PhraseQuery();
phraseQuery.Add(new Term(fieldName.ToLowerInvariant(), fieldValue.ToLowerInvariant()));
return phraseQuery;
}
开发者ID:NetworkTen,项目名称:SitecoreSearchContrib,代码行数:16,代码来源:QueryBuilder.cs
示例15: TestIncreasingSloppiness
public virtual void TestIncreasingSloppiness()
{
Term t1 = RandomTerm();
Term t2 = RandomTerm();
PhraseQuery q1 = new PhraseQuery();
q1.Add(t1);
q1.Add(t2);
PhraseQuery q2 = new PhraseQuery();
q2.Add(t1);
q2.Add(t2);
for (int i = 0; i < 10; i++)
{
q1.Slop = i;
q2.Slop = i + 1;
AssertSubsetOf(q1, q2);
}
}
开发者ID:Cefa68000,项目名称:lucenenet,代码行数:17,代码来源:TestSloppyPhraseQuery2.cs
示例16: PhaseQueryHasHits
protected bool PhaseQueryHasHits(string[] phrases, int i)
{
using (var dir = FSDirectory.Open(TestEnvironment.TestIndexDirectory))
using (var indexSearcher = new IndexSearcher(dir))
{
var phraseQuery = new PhraseQuery
{
Slop = 0
};
foreach (var phrase in phrases)
{
phraseQuery.Add(new Term("field", phrase));
}
// Search, without subcategories
var topDocs = indexSearcher.Search(phraseQuery, 10);
return topDocs.TotalHits > 0;
}
}
开发者ID:joshball,项目名称:Lucene.In.Action.NET,代码行数:18,代码来源:PhraseQueryTest.cs
示例17: TestDemo
public virtual void TestDemo()
{
Analyzer analyzer = new MockAnalyzer(random());
// Store the index in memory:
Directory directory = newDirectory();
// To store an index on disk, use this instead:
// Directory directory = FSDirectory.open(new File("/tmp/testindex"));
RandomIndexWriter iwriter = new RandomIndexWriter(random(), directory, analyzer);
Document doc = new Document();
string longTerm = "longtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongterm";
string text = "this is the text to be indexed. " + longTerm;
doc.add(newTextField("fieldname", text, Field.Store.YES));
iwriter.addDocument(doc);
iwriter.close();
// Now search the index:
IndexReader ireader = DirectoryReader.open(directory); // read-only=true
IndexSearcher isearcher = newSearcher(ireader);
Assert.AreEqual(1, isearcher.search(new TermQuery(new Term("fieldname", longTerm)), 1).totalHits);
Query query = new TermQuery(new Term("fieldname", "text"));
TopDocs hits = isearcher.search(query, null, 1);
Assert.AreEqual(1, hits.totalHits);
// Iterate through the results:
for (int i = 0; i < hits.scoreDocs.length; i++)
{
Document hitDoc = isearcher.doc(hits.scoreDocs[i].doc);
Assert.AreEqual(text, hitDoc.get("fieldname"));
}
// Test simple phrase query
PhraseQuery phraseQuery = new PhraseQuery();
phraseQuery.add(new Term("fieldname", "to"));
phraseQuery.add(new Term("fieldname", "be"));
Assert.AreEqual(1, isearcher.search(phraseQuery, null, 1).totalHits);
ireader.close();
directory.close();
}
开发者ID:Cefa68000,项目名称:lucenenet,代码行数:40,代码来源:TestDemo.cs
示例18: SearchFromIndexData
/// <summary>
/// 从索引库中检索关键字
/// </summary>
private void SearchFromIndexData() {
string indexPath = Context.Server.MapPath("~/IndexData");
FSDirectory directory = FSDirectory.Open(new DirectoryInfo(indexPath), new NoLockFactory());
IndexReader reader = IndexReader.Open(directory, true);
IndexSearcher searcher = new IndexSearcher(reader);
//搜索条件
PhraseQuery query = new PhraseQuery();
//把用户输入的关键字进行分词
foreach(string word in Common.SplitContent.SplitWords(Request.QueryString["SearchKey"])) {
query.Add(new Term("content", word));
}
//query.Add(new Term("content", "C#"));//多个查询条件时 为且的关系
query.SetSlop(100); //指定关键词相隔最大距离
//TopScoreDocCollector盛放查询结果的容器
TopScoreDocCollector collector = TopScoreDocCollector.create(1000, true);
searcher.Search(query, null, collector);//根据query查询条件进行查询,查询结果放入collector容器
//TopDocs 指定0到GetTotalHits() 即所有查询结果中的文档 如果TopDocs(20,10)则意味着获取第20-30之间文档内容 达到分页的效果
ScoreDoc[] docs = collector.TopDocs(0, collector.GetTotalHits()).scoreDocs;
//展示数据实体对象集合
List<PZYM.Shop.Model.Books> bookResult = new List<PZYM.Shop.Model.Books>();
for(int i = 0; i < docs.Length; i++) {
int docId = docs[i].doc;//得到查询结果文档的id(Lucene内部分配的id)
Document doc = searcher.Doc(docId);//根据文档id来获得文档对象Document
PZYM.Shop.Model.Books book = new PZYM.Shop.Model.Books();
book.Title = doc.Get("title");
//book.ContentDescription = doc.Get("content");//未使用高亮
//搜索关键字高亮显示 使用盘古提供高亮插件
book.ContentDescription = Common.SplitContent.HightLight(Request.QueryString["SearchKey"], doc.Get("content"));
book.Id = Convert.ToInt32(doc.Get("id"));
bookResult.Add(book);
}
Repeater1.DataSource = bookResult;
Repeater1.DataBind();
}
开发者ID:a14907,项目名称:zhanneisousuo,代码行数:41,代码来源:BookList.aspx.cs
示例19: AutomaticallyClosesDanglingQuotes
public void AutomaticallyClosesDanglingQuotes()
{
// arrange
var queryText = "title:\"dot NET version:1.2.3";
var phraseQuery = new PhraseQuery();
phraseQuery.Add(new Term("Title", "dot"));
phraseQuery.Add(new Term("Title", "net"));
phraseQuery.Add(new Term("Title", "version"));
phraseQuery.Add(new Term("Title", "1"));
phraseQuery.Add(new Term("Title", "2"));
phraseQuery.Add(new Term("Title", "3"));
var expected = new BooleanQuery
{
new BooleanClause(new BooleanQuery { new BooleanClause(new BooleanQuery { new BooleanClause(phraseQuery, Occur.SHOULD) }, Occur.SHOULD) }, Occur.MUST)
};
// act
var actual = NuGetQuery.MakeQuery(queryText);
// assert
Assert.Equal(expected, actual);
}
开发者ID:NuGet,项目名称:NuGet.Services.Metadata,代码行数:23,代码来源:NuGetQueryTests.cs
示例20: GetFieldQuery
/// <exception cref="ParseException">throw in overridden method to disallow
/// </exception>
protected internal virtual Query GetFieldQuery(System.String field, System.String queryText)
{
// Use the analyzer to get all the tokens, and then build a TermQuery,
// PhraseQuery, or nothing based on the term count
TokenStream source = analyzer.TokenStream(field, new System.IO.StringReader(queryText));
System.Collections.ArrayList v = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10));
Lucene.Net.Analysis.Token t;
int positionCount = 0;
bool severalTokensAtSamePosition = false;
while (true)
{
try
{
t = source.Next();
}
catch (System.IO.IOException e)
{
t = null;
}
if (t == null)
break;
v.Add(t);
if (t.GetPositionIncrement() != 0)
positionCount += t.GetPositionIncrement();
else
severalTokensAtSamePosition = true;
}
try
{
source.Close();
}
catch (System.IO.IOException e)
{
// ignore
}
if (v.Count == 0)
return null;
else if (v.Count == 1)
{
t = (Lucene.Net.Analysis.Token) v[0];
return new TermQuery(new Term(field, t.TermText()));
}
else
{
if (severalTokensAtSamePosition)
{
if (positionCount == 1)
{
// no phrase query:
BooleanQuery q = new BooleanQuery(true);
for (int i = 0; i < v.Count; i++)
{
t = (Lucene.Net.Analysis.Token) v[i];
TermQuery currentQuery = new TermQuery(new Term(field, t.TermText()));
q.Add(currentQuery, BooleanClause.Occur.SHOULD);
}
return q;
}
else
{
// phrase query:
MultiPhraseQuery mpq = new MultiPhraseQuery();
System.Collections.ArrayList multiTerms = new System.Collections.ArrayList();
for (int i = 0; i < v.Count; i++)
{
t = (Lucene.Net.Analysis.Token) v[i];
if (t.GetPositionIncrement() == 1 && multiTerms.Count > 0)
{
mpq.Add((Term[]) multiTerms.ToArray(typeof(Term)));
multiTerms.Clear();
}
multiTerms.Add(new Term(field, t.TermText()));
}
mpq.Add((Term[]) multiTerms.ToArray(typeof(Term)));
return mpq;
}
}
else
{
PhraseQuery q = new PhraseQuery();
q.SetSlop(phraseSlop);
for (int i = 0; i < v.Count; i++)
{
q.Add(new Term(field, ((Lucene.Net.Analysis.Token) v[i]).TermText()));
}
return q;
}
}
}
开发者ID:ArsenShnurkov,项目名称:beagle-1,代码行数:94,代码来源:QueryParser.cs
注:本文中的Lucene.Net.Search.PhraseQuery类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论