本文整理汇总了C#中Lucene.Net.Search.PhraseQuery类的典型用法代码示例。如果您正苦于以下问题:C# PhraseQuery类的具体用法?C# PhraseQuery怎么用?C# PhraseQuery使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
PhraseQuery类属于Lucene.Net.Search命名空间,在下文中一共展示了PhraseQuery类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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});
}
示例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;
}
示例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;
}
}
}
示例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;
}
示例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));
}
示例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 });
}
示例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);
}
示例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);
}
示例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);
}
示例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[] { });
}
示例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;
}
示例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\""));
}
示例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();
}
示例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;
}
示例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);
}
}