public virtual void TestPhrasePrefix() { RAMDirectory indexStore = new RAMDirectory(); IndexWriter writer = new IndexWriter(indexStore, new SimpleAnalyzer(), true); Document doc1 = new Document(); Document doc2 = new Document(); Document doc3 = new Document(); Document doc4 = new Document(); Document doc5 = new Document(); doc1.Add(Field.Text("body", "blueberry pie")); doc2.Add(Field.Text("body", "blueberry strudel")); doc3.Add(Field.Text("body", "blueberry pizza")); doc4.Add(Field.Text("body", "blueberry chewing gum")); doc5.Add(Field.Text("body", "piccadilly circus")); 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(); PhrasePrefixQuery query2 = new PhrasePrefixQuery(); 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))); Hits result; result = searcher.Search(query1); Assert.AreEqual(2, result.Length()); result = searcher.Search(query2); Assert.AreEqual(0, result.Length()); }
public virtual void TestPhrasePrefix() { RAMDirectory indexStore = new RAMDirectory(); IndexWriter writer = new IndexWriter(indexStore, new SimpleAnalyzer(), true); Document doc1 = new Document(); Document doc2 = new Document(); Document doc3 = new Document(); Document doc4 = new Document(); Document doc5 = new Document(); doc1.Add(Field.Text("body", "blueberry pie")); doc2.Add(Field.Text("body", "blueberry strudel")); doc3.Add(Field.Text("body", "blueberry pizza")); doc4.Add(Field.Text("body", "blueberry chewing gum")); doc5.Add(Field.Text("body", "piccadilly circus")); 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(); PhrasePrefixQuery query2 = new PhrasePrefixQuery(); 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))); Hits result; result = searcher.Search(query1); Assert.AreEqual(2, result.Length()); result = searcher.Search(query2); Assert.AreEqual(0, result.Length()); }
public PhrasePrefixWeight(PhrasePrefixQuery enclosingInstance, Searcher searcher) { InitBlock(enclosingInstance); this.similarity = Enclosing_Instance.GetSimilarity(searcher); // compute idf System.Collections.IEnumerator i = Enclosing_Instance.termArrays.GetEnumerator(); while (i.MoveNext()) { Term[] terms = (Term[])i.Current; for (int j = 0; j < terms.Length; j++) { idf += Enclosing_Instance.GetSimilarity(searcher).Idf(terms[j], searcher); } } }
public PhrasePrefixWeight(PhrasePrefixQuery enclosingInstance, Searcher searcher) { InitBlock(enclosingInstance); this.similarity = Enclosing_Instance.GetSimilarity(searcher); // compute idf System.Collections.IEnumerator i = Enclosing_Instance.termArrays.GetEnumerator(); while (i.MoveNext()) { Term[] terms = (Term[]) i.Current; for (int j = 0; j < terms.Length; j++) { idf += Enclosing_Instance.GetSimilarity(searcher).Idf(terms[j], searcher); } } }
private void InitBlock(PhrasePrefixQuery enclosingInstance) { this.enclosingInstance = enclosingInstance; }