本文整理汇总了C#中Lucene.Net.Search.MultiPhraseQuery.Add方法的典型用法代码示例。如果您正苦于以下问题:C# MultiPhraseQuery.Add方法的具体用法?C# MultiPhraseQuery.Add怎么用?C# MultiPhraseQuery.Add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Lucene.Net.Search.MultiPhraseQuery
的用法示例。
在下文中一共展示了MultiPhraseQuery.Add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestPhrasePrefix
public virtual void TestPhrasePrefix()
{
RAMDirectory indexStore = new RAMDirectory();
IndexWriter writer = new IndexWriter(indexStore, new SimpleAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED);
Document doc1 = new Document();
Document doc2 = new Document();
Document doc3 = new Document();
Document doc4 = new Document();
Document doc5 = new Document();
doc1.Add(new Field("body", "blueberry pie", Field.Store.YES, Field.Index.ANALYZED));
doc2.Add(new Field("body", "blueberry strudel", Field.Store.YES, Field.Index.ANALYZED));
doc3.Add(new Field("body", "blueberry pizza", Field.Store.YES, Field.Index.ANALYZED));
doc4.Add(new Field("body", "blueberry chewing gum", Field.Store.YES, Field.Index.ANALYZED));
doc5.Add(new Field("body", "piccadilly circus", Field.Store.YES, Field.Index.ANALYZED));
writer.AddDocument(doc1);
writer.AddDocument(doc2);
writer.AddDocument(doc3);
writer.AddDocument(doc4);
writer.AddDocument(doc5);
writer.Optimize();
writer.Close();
IndexSearcher searcher = new IndexSearcher(indexStore);
//PhrasePrefixQuery query1 = new PhrasePrefixQuery();
MultiPhraseQuery query1 = new MultiPhraseQuery();
//PhrasePrefixQuery query2 = new PhrasePrefixQuery();
MultiPhraseQuery query2 = new MultiPhraseQuery();
query1.Add(new Term("body", "blueberry"));
query2.Add(new Term("body", "strawberry"));
System.Collections.ArrayList termsWithPrefix = new System.Collections.ArrayList();
IndexReader ir = IndexReader.Open(indexStore);
// this TermEnum gives "piccadilly", "pie" and "pizza".
System.String prefix = "pi";
TermEnum te = ir.Terms(new Term("body", prefix + "*"));
do
{
if (te.Term().Text().StartsWith(prefix))
{
termsWithPrefix.Add(te.Term());
}
}
while (te.Next());
query1.Add((Term[]) termsWithPrefix.ToArray(typeof(Term)));
query2.Add((Term[]) termsWithPrefix.ToArray(typeof(Term)));
ScoreDoc[] result;
result = searcher.Search(query1, null, 1000).scoreDocs;
Assert.AreEqual(2, result.Length);
result = searcher.Search(query2, null, 1000).scoreDocs;
Assert.AreEqual(0, result.Length);
}
示例2: Build
public virtual Query Build(IQueryNode queryNode)
{
MultiPhraseQueryNode phraseNode = (MultiPhraseQueryNode)queryNode;
MultiPhraseQuery phraseQuery = new MultiPhraseQuery();
IList<IQueryNode> children = phraseNode.GetChildren();
if (children != null)
{
IDictionary<int?, List<Term>> positionTermMap = new SortedDictionary<int?, List<Term>>();
foreach (IQueryNode child in children)
{
FieldQueryNode termNode = (FieldQueryNode)child;
TermQuery termQuery = (TermQuery)termNode
.GetTag(QueryTreeBuilder.QUERY_TREE_BUILDER_TAGID);
List<Term> termList;
if (!positionTermMap.TryGetValue(termNode.PositionIncrement, out termList) || termList == null)
{
termList = new List<Term>();
positionTermMap[termNode.PositionIncrement] = termList;
}
termList.Add(termQuery.Term);
}
foreach (int positionIncrement in positionTermMap.Keys)
{
List<Term> termList = positionTermMap[positionIncrement];
phraseQuery.Add(termList.ToArray(/*new Term[termList.size()]*/),
positionIncrement);
}
}
return phraseQuery;
}
示例3: TestMPQ7
public virtual void TestMPQ7()
{
MultiPhraseQuery q = new MultiPhraseQuery();
q.Add(Ta(new System.String[]{"w1"}));
q.Add(Ta(new System.String[]{"w2"}));
q.SetSlop(1);
q.SetBoost(0.0f);
Bqtest(q, new int[]{0, 1, 2});
}
示例4: TestPhrasePrefix
public virtual void TestPhrasePrefix()
{
Directory indexStore = NewDirectory();
RandomIndexWriter writer = new RandomIndexWriter(Random(), indexStore);
Document doc1 = new Document();
Document doc2 = new Document();
Document doc3 = new Document();
Document doc4 = new Document();
Document doc5 = new Document();
doc1.Add(NewTextField("body", "blueberry pie", Field.Store.YES));
doc2.Add(NewTextField("body", "blueberry strudel", Field.Store.YES));
doc3.Add(NewTextField("body", "blueberry pizza", Field.Store.YES));
doc4.Add(NewTextField("body", "blueberry chewing gum", Field.Store.YES));
doc5.Add(NewTextField("body", "piccadilly circus", Field.Store.YES));
writer.AddDocument(doc1);
writer.AddDocument(doc2);
writer.AddDocument(doc3);
writer.AddDocument(doc4);
writer.AddDocument(doc5);
IndexReader reader = writer.Reader;
writer.Dispose();
IndexSearcher searcher = NewSearcher(reader);
// PhrasePrefixQuery query1 = new PhrasePrefixQuery();
MultiPhraseQuery query1 = new MultiPhraseQuery();
// PhrasePrefixQuery query2 = new PhrasePrefixQuery();
MultiPhraseQuery query2 = new MultiPhraseQuery();
query1.Add(new Term("body", "blueberry"));
query2.Add(new Term("body", "strawberry"));
LinkedList<Term> termsWithPrefix = new LinkedList<Term>();
// this TermEnum gives "piccadilly", "pie" and "pizza".
string prefix = "pi";
TermsEnum te = MultiFields.GetFields(reader).Terms("body").Iterator(null);
te.SeekCeil(new BytesRef(prefix));
do
{
string s = te.Term().Utf8ToString();
if (s.StartsWith(prefix))
{
termsWithPrefix.AddLast(new Term("body", s));
}
else
{
break;
}
} while (te.Next() != null);
query1.Add(termsWithPrefix.ToArray(/*new Term[0]*/));
query2.Add(termsWithPrefix.ToArray(/*new Term[0]*/));
ScoreDoc[] result;
result = searcher.Search(query1, null, 1000).ScoreDocs;
Assert.AreEqual(2, result.Length);
result = searcher.Search(query2, null, 1000).ScoreDocs;
Assert.AreEqual(0, result.Length);
reader.Dispose();
indexStore.Dispose();
}
示例5: TestSetPosition
public virtual void TestSetPosition()
{
Analyzer analyzer = new AnalyzerAnonymousInnerClassHelper(this);
Directory store = NewDirectory();
RandomIndexWriter writer = new RandomIndexWriter(Random(), store, analyzer);
Document d = new Document();
d.Add(NewTextField("field", "bogus", Field.Store.YES));
writer.AddDocument(d);
IndexReader reader = writer.Reader;
writer.Dispose();
IndexSearcher searcher = NewSearcher(reader);
DocsAndPositionsEnum pos = MultiFields.GetTermPositionsEnum(searcher.IndexReader, MultiFields.GetLiveDocs(searcher.IndexReader), "field", new BytesRef("1"));
pos.NextDoc();
// first token should be at position 0
Assert.AreEqual(0, pos.NextPosition());
pos = MultiFields.GetTermPositionsEnum(searcher.IndexReader, MultiFields.GetLiveDocs(searcher.IndexReader), "field", new BytesRef("2"));
pos.NextDoc();
// second token should be at position 2
Assert.AreEqual(2, pos.NextPosition());
PhraseQuery q;
ScoreDoc[] hits;
q = new PhraseQuery();
q.Add(new Term("field", "1"));
q.Add(new Term("field", "2"));
hits = searcher.Search(q, null, 1000).ScoreDocs;
Assert.AreEqual(0, hits.Length);
// same as previous, just specify positions explicitely.
q = new PhraseQuery();
q.Add(new Term("field", "1"), 0);
q.Add(new Term("field", "2"), 1);
hits = searcher.Search(q, null, 1000).ScoreDocs;
Assert.AreEqual(0, hits.Length);
// specifying correct positions should find the phrase.
q = new PhraseQuery();
q.Add(new Term("field", "1"), 0);
q.Add(new Term("field", "2"), 2);
hits = searcher.Search(q, null, 1000).ScoreDocs;
Assert.AreEqual(1, hits.Length);
q = new PhraseQuery();
q.Add(new Term("field", "2"));
q.Add(new Term("field", "3"));
hits = searcher.Search(q, null, 1000).ScoreDocs;
Assert.AreEqual(1, hits.Length);
q = new PhraseQuery();
q.Add(new Term("field", "3"));
q.Add(new Term("field", "4"));
hits = searcher.Search(q, null, 1000).ScoreDocs;
Assert.AreEqual(0, hits.Length);
// phrase query would find it when correct positions are specified.
q = new PhraseQuery();
q.Add(new Term("field", "3"), 0);
q.Add(new Term("field", "4"), 0);
hits = searcher.Search(q, null, 1000).ScoreDocs;
Assert.AreEqual(1, hits.Length);
// phrase query should fail for non existing searched term
// even if there exist another searched terms in the same searched position.
q = new PhraseQuery();
q.Add(new Term("field", "3"), 0);
q.Add(new Term("field", "9"), 0);
hits = searcher.Search(q, null, 1000).ScoreDocs;
Assert.AreEqual(0, hits.Length);
// multi-phrase query should succed for non existing searched term
// because there exist another searched terms in the same searched position.
MultiPhraseQuery mq = new MultiPhraseQuery();
mq.Add(new Term[] { new Term("field", "3"), new Term("field", "9") }, 0);
hits = searcher.Search(mq, null, 1000).ScoreDocs;
Assert.AreEqual(1, hits.Length);
q = new PhraseQuery();
q.Add(new Term("field", "2"));
q.Add(new Term("field", "4"));
hits = searcher.Search(q, null, 1000).ScoreDocs;
Assert.AreEqual(1, hits.Length);
q = new PhraseQuery();
q.Add(new Term("field", "3"));
q.Add(new Term("field", "5"));
hits = searcher.Search(q, null, 1000).ScoreDocs;
Assert.AreEqual(1, hits.Length);
q = new PhraseQuery();
q.Add(new Term("field", "4"));
q.Add(new Term("field", "5"));
hits = searcher.Search(q, null, 1000).ScoreDocs;
Assert.AreEqual(1, hits.Length);
q = new PhraseQuery();
q.Add(new Term("field", "2"));
//.........这里部分代码省略.........
示例6: TestPhrasePrefixWithBooleanQuery
public virtual void TestPhrasePrefixWithBooleanQuery()
{
RAMDirectory indexStore = new RAMDirectory();
IndexWriter writer = new IndexWriter(indexStore, new StandardAnalyzer(new System.Collections.Hashtable(0)), true, IndexWriter.MaxFieldLength.LIMITED);
Add("This is a test", "object", writer);
Add("a note", "note", writer);
writer.Close();
IndexSearcher searcher = new IndexSearcher(indexStore);
// This query will be equivalent to +type:note +body:"a t*"
BooleanQuery q = new BooleanQuery();
q.Add(new TermQuery(new Term("type", "note")), BooleanClause.Occur.MUST);
MultiPhraseQuery trouble = new MultiPhraseQuery();
trouble.Add(new Term("body", "a"));
trouble.Add(new Term[]{new Term("body", "test"), new Term("body", "this")});
q.Add(trouble, BooleanClause.Occur.MUST);
// exception will be thrown here without fix for #35626:
ScoreDoc[] hits = searcher.Search(q, null, 1000).scoreDocs;
Assert.AreEqual(0, hits.Length, "Wrong number of hits");
searcher.Close();
}
示例7: TestPhrasePrefix
public virtual void TestPhrasePrefix()
{
RAMDirectory indexStore = new RAMDirectory();
IndexWriter writer = new IndexWriter(indexStore, new SimpleAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED);
Add("blueberry pie", writer);
Add("blueberry strudel", writer);
Add("blueberry pizza", writer);
Add("blueberry chewing gum", writer);
Add("bluebird pizza", writer);
Add("bluebird foobar pizza", writer);
Add("piccadilly circus", writer);
writer.Optimize();
writer.Close();
IndexSearcher searcher = new IndexSearcher(indexStore);
// search for "blueberry pi*":
MultiPhraseQuery query1 = new MultiPhraseQuery();
// search for "strawberry pi*":
MultiPhraseQuery query2 = new MultiPhraseQuery();
query1.Add(new Term("body", "blueberry"));
query2.Add(new Term("body", "strawberry"));
System.Collections.ArrayList termsWithPrefix = new System.Collections.ArrayList();
IndexReader ir = IndexReader.Open(indexStore);
// this TermEnum gives "piccadilly", "pie" and "pizza".
System.String prefix = "pi";
TermEnum te = ir.Terms(new Term("body", prefix));
do
{
if (te.Term().Text().StartsWith(prefix))
{
termsWithPrefix.Add(te.Term());
}
}
while (te.Next());
query1.Add((Term[]) termsWithPrefix.ToArray(typeof(Term)));
Assert.AreEqual("body:\"blueberry (piccadilly pie pizza)\"", query1.ToString());
query2.Add((Term[]) termsWithPrefix.ToArray(typeof(Term)));
Assert.AreEqual("body:\"strawberry (piccadilly pie pizza)\"", query2.ToString());
ScoreDoc[] result;
result = searcher.Search(query1, null, 1000).scoreDocs;
Assert.AreEqual(2, result.Length);
result = searcher.Search(query2, null, 1000).scoreDocs;
Assert.AreEqual(0, result.Length);
// search for "blue* pizza":
MultiPhraseQuery query3 = new MultiPhraseQuery();
termsWithPrefix.Clear();
prefix = "blue";
te = ir.Terms(new Term("body", prefix));
do
{
if (te.Term().Text().StartsWith(prefix))
{
termsWithPrefix.Add(te.Term());
}
}
while (te.Next());
query3.Add((Term[]) termsWithPrefix.ToArray(typeof(Term)));
query3.Add(new Term("body", "pizza"));
result = searcher.Search(query3, null, 1000).scoreDocs;
Assert.AreEqual(2, result.Length); // blueberry pizza, bluebird pizza
Assert.AreEqual("body:\"(blueberry bluebird) pizza\"", query3.ToString());
// test slop:
query3.SetSlop(1);
result = searcher.Search(query3, null, 1000).scoreDocs;
Assert.AreEqual(3, result.Length); // blueberry pizza, bluebird pizza, bluebird foobar pizza
MultiPhraseQuery query4 = new MultiPhraseQuery();
try
{
query4.Add(new Term("field1", "foo"));
query4.Add(new Term("field2", "foobar"));
Assert.Fail();
}
catch (System.ArgumentException e)
{
// okay, all terms must belong to the same field
}
searcher.Close();
indexStore.Close();
}
示例8: TestMultiSloppyWithRepeats
public virtual void TestMultiSloppyWithRepeats() //LUCENE-3821 fixes sloppy phrase scoring, except for this known problem
{
Directory indexStore = NewDirectory();
RandomIndexWriter writer = new RandomIndexWriter(Random(), indexStore, Similarity, TimeZone);
Add("a b c d e f g h i k", writer);
IndexReader r = writer.Reader;
writer.Dispose();
IndexSearcher searcher = NewSearcher(r);
MultiPhraseQuery q = new MultiPhraseQuery();
// this will fail, when the scorer would propagate [a] rather than [a,b],
q.Add(new Term[] { new Term("body", "a"), new Term("body", "b") });
q.Add(new Term[] { new Term("body", "a") });
q.Slop = 6;
Assert.AreEqual(1, searcher.Search(q, 1).TotalHits); // should match on "a b"
r.Dispose();
indexStore.Dispose();
}
示例9: TestMPQ6
public virtual void TestMPQ6()
{
MultiPhraseQuery q = new MultiPhraseQuery();
q.Add(Ta(new System.String[]{"w1", "w3"}));
q.Add(Ta(new System.String[]{"w2"}));
q.Slop = 1;
Qtest(q, new int[]{0, 1, 2, 3});
}
示例10: TestZeroPosIncr
public virtual void TestZeroPosIncr()
{
Directory dir = new RAMDirectory();
Token[] tokens = new Token[3];
tokens[0] = new Token();
tokens[0].Append("a");
tokens[0].PositionIncrement = 1;
tokens[1] = new Token();
tokens[1].Append("b");
tokens[1].PositionIncrement = 0;
tokens[2] = new Token();
tokens[2].Append("c");
tokens[2].PositionIncrement = 0;
RandomIndexWriter writer = new RandomIndexWriter(Random(), dir, Similarity, TimeZone);
Document doc = new Document();
doc.Add(new TextField("field", new CannedTokenStream(tokens)));
writer.AddDocument(doc);
doc = new Document();
doc.Add(new TextField("field", new CannedTokenStream(tokens)));
writer.AddDocument(doc);
IndexReader r = writer.Reader;
writer.Dispose();
IndexSearcher s = NewSearcher(r);
MultiPhraseQuery mpq = new MultiPhraseQuery();
//mpq.setSlop(1);
// NOTE: not great that if we do the else clause here we
// get different scores! MultiPhraseQuery counts that
// phrase as occurring twice per doc (it should be 1, I
// think?). this is because MultipleTermPositions is able to
// return the same position more than once (0, in this
// case):
if (true)
{
mpq.Add(new Term[] { new Term("field", "b"), new Term("field", "c") }, 0);
mpq.Add(new Term[] { new Term("field", "a") }, 0);
}
else
{
mpq.Add(new Term[] { new Term("field", "a") }, 0);
mpq.Add(new Term[] { new Term("field", "b"), new Term("field", "c") }, 0);
}
TopDocs hits = s.Search(mpq, 2);
Assert.AreEqual(2, hits.TotalHits);
Assert.AreEqual(hits.ScoreDocs[0].Score, hits.ScoreDocs[1].Score, 1e-5);
/*
for(int hit=0;hit<hits.TotalHits;hit++) {
ScoreDoc sd = hits.ScoreDocs[hit];
System.out.println(" hit doc=" + sd.Doc + " score=" + sd.Score);
}
*/
r.Dispose();
dir.Dispose();
}
示例11: TestMPQ3
public virtual void TestMPQ3()
{
MultiPhraseQuery q = new MultiPhraseQuery();
q.Add(Ta(new string[] { "w1", "xx" }));
q.Add(Ta(new string[] { "w2", "w3" }));
Qtest(q, new int[] { 0, 1, 2, 3 });
}
示例12: TestNoDocs
public virtual void TestNoDocs()
{
Directory indexStore = NewDirectory();
RandomIndexWriter writer = new RandomIndexWriter(Random(), indexStore, Similarity, TimeZone);
Add("a note", "note", writer);
IndexReader reader = writer.Reader;
IndexSearcher searcher = NewSearcher(reader);
MultiPhraseQuery q = new MultiPhraseQuery();
q.Add(new Term("body", "a"));
q.Add(new Term[] { new Term("body", "nope"), new Term("body", "nope") });
Assert.AreEqual(0, searcher.Search(q, null, 1).TotalHits, "Wrong number of hits");
// just make sure no exc:
searcher.Explain(q, 0);
writer.Dispose();
reader.Dispose();
indexStore.Dispose();
}
示例13: TestCustomIDF
public virtual void TestCustomIDF()
{
Directory indexStore = NewDirectory();
RandomIndexWriter writer = new RandomIndexWriter(Random(), indexStore, Similarity, TimeZone);
Add("this is a test", "object", writer);
Add("a note", "note", writer);
IndexReader reader = writer.Reader;
IndexSearcher searcher = NewSearcher(reader);
searcher.Similarity = new DefaultSimilarityAnonymousInnerClassHelper(this);
MultiPhraseQuery query = new MultiPhraseQuery();
query.Add(new Term[] { new Term("body", "this"), new Term("body", "that") });
query.Add(new Term("body", "is"));
Weight weight = query.CreateWeight(searcher);
Assert.AreEqual(10f * 10f, weight.ValueForNormalization, 0.001f);
writer.Dispose();
reader.Dispose();
indexStore.Dispose();
}
示例14: TestPhrasePrefixWithBooleanQuery
public virtual void TestPhrasePrefixWithBooleanQuery()
{
Directory indexStore = NewDirectory();
RandomIndexWriter writer = new RandomIndexWriter(Random(), indexStore, Similarity, TimeZone);
Add("this is a test", "object", writer);
Add("a note", "note", writer);
IndexReader reader = writer.Reader;
IndexSearcher searcher = NewSearcher(reader);
// this query will be equivalent to +type:note +body:"a t*"
BooleanQuery q = new BooleanQuery();
q.Add(new TermQuery(new Term("type", "note")), BooleanClause.Occur.MUST);
MultiPhraseQuery trouble = new MultiPhraseQuery();
trouble.Add(new Term("body", "a"));
trouble.Add(new Term[] { new Term("body", "test"), new Term("body", "this") });
q.Add(trouble, BooleanClause.Occur.MUST);
// exception will be thrown here without fix for #35626:
ScoreDoc[] hits = searcher.Search(q, null, 1000).ScoreDocs;
Assert.AreEqual(0, hits.Length, "Wrong number of hits");
writer.Dispose();
reader.Dispose();
indexStore.Dispose();
}
示例15: TestBooleanQueryContainingSingleTermPrefixQuery
public virtual void TestBooleanQueryContainingSingleTermPrefixQuery()
{
// this tests against bug 33161 (now fixed)
// In order to cause the bug, the outer query must have more than one term
// and all terms required.
// The contained PhraseMultiQuery must contain exactly one term array.
Directory indexStore = NewDirectory();
RandomIndexWriter writer = new RandomIndexWriter(Random(), indexStore, Similarity, TimeZone);
Add("blueberry pie", writer);
Add("blueberry chewing gum", writer);
Add("blue raspberry pie", writer);
IndexReader reader = writer.Reader;
IndexSearcher searcher = NewSearcher(reader);
// this query will be equivalent to +body:pie +body:"blue*"
BooleanQuery q = new BooleanQuery();
q.Add(new TermQuery(new Term("body", "pie")), BooleanClause.Occur.MUST);
MultiPhraseQuery trouble = new MultiPhraseQuery();
trouble.Add(new Term[] { new Term("body", "blueberry"), new Term("body", "blue") });
q.Add(trouble, BooleanClause.Occur.MUST);
// exception will be thrown here without fix
ScoreDoc[] hits = searcher.Search(q, null, 1000).ScoreDocs;
Assert.AreEqual(2, hits.Length, "Wrong number of hits");
// just make sure no exc:
searcher.Explain(q, 0);
writer.Dispose();
reader.Dispose();
indexStore.Dispose();
}