本文整理汇总了C#中Lucene.Net.Search.IndexSearcher.SetSimilarity方法的典型用法代码示例。如果您正苦于以下问题:C# IndexSearcher.SetSimilarity方法的具体用法?C# IndexSearcher.SetSimilarity怎么用?C# IndexSearcher.SetSimilarity使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Lucene.Net.Search.IndexSearcher
的用法示例。
在下文中一共展示了IndexSearcher.SetSimilarity方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SetUp
public override void SetUp()
{
base.SetUp();
index = new RAMDirectory();
IndexWriter writer = new IndexWriter(index, new WhitespaceAnalyzer(), true);
writer.SetSimilarity(sim);
// hed is the most important field, dek is secondary
// d1 is an "ok" match for: albino elephant
{
Lucene.Net.Documents.Document d1 = new Lucene.Net.Documents.Document();
d1.Add(new Field("id", "d1", Field.Store.YES, Field.Index.UN_TOKENIZED)); //Field.Keyword("id", "d1"));
d1.Add(new Field("hed", "elephant", Field.Store.YES, Field.Index.TOKENIZED)); //Field.Text("hed", "elephant"));
d1.Add(new Field("dek", "elephant", Field.Store.YES, Field.Index.TOKENIZED)); //Field.Text("dek", "elephant"));
writer.AddDocument(d1);
}
// d2 is a "good" match for: albino elephant
{
Lucene.Net.Documents.Document d2 = new Lucene.Net.Documents.Document();
d2.Add(new Field("id", "d2", Field.Store.YES, Field.Index.UN_TOKENIZED)); //Field.Keyword("id", "d2"));
d2.Add(new Field("hed", "elephant", Field.Store.YES, Field.Index.TOKENIZED)); //Field.Text("hed", "elephant"));
d2.Add(new Field("dek", "albino", Field.Store.YES, Field.Index.TOKENIZED)); //Field.Text("dek", "albino"));
d2.Add(new Field("dek", "elephant", Field.Store.YES, Field.Index.TOKENIZED)); //Field.Text("dek", "elephant"));
writer.AddDocument(d2);
}
// d3 is a "better" match for: albino elephant
{
Lucene.Net.Documents.Document d3 = new Lucene.Net.Documents.Document();
d3.Add(new Field("id", "d3", Field.Store.YES, Field.Index.UN_TOKENIZED)); //Field.Keyword("id", "d3"));
d3.Add(new Field("hed", "albino", Field.Store.YES, Field.Index.TOKENIZED)); //Field.Text("hed", "albino"));
d3.Add(new Field("hed", "elephant", Field.Store.YES, Field.Index.TOKENIZED)); //Field.Text("hed", "elephant"));
writer.AddDocument(d3);
}
// d4 is the "best" match for: albino elephant
{
Lucene.Net.Documents.Document d4 = new Lucene.Net.Documents.Document();
d4.Add(new Field("id", "d4", Field.Store.YES, Field.Index.UN_TOKENIZED)); //Field.Keyword("id", "d4"));
d4.Add(new Field("hed", "albino", Field.Store.YES, Field.Index.TOKENIZED)); //Field.Text("hed", "albino"));
d4.Add(new Field("hed", "elephant", Field.Store.YES, Field.Index.TOKENIZED)); //Field.Text("hed", "elephant"));
d4.Add(new Field("dek", "albino", Field.Store.YES, Field.Index.TOKENIZED)); //Field.Text("dek", "albino"));
writer.AddDocument(d4);
}
writer.Close();
r = IndexReader.Open(index);
s = new IndexSearcher(r);
s.SetSimilarity(sim);
}
示例2: TestCustomSimilarity
public virtual void TestCustomSimilarity()
{
RAMDirectory dir = new RAMDirectory();
InitIndex(dir, 10, true, "x"); // documents with two tokens "doc0" and "x", "doc1" and x, etc...
IndexSearcher srchr = new IndexSearcher(dir);
MultiSearcher msrchr = GetMultiSearcherInstance(new Searcher[]{srchr});
Similarity customSimilarity = new AnonymousClassDefaultSimilarity(this);
srchr.SetSimilarity(customSimilarity);
msrchr.SetSimilarity(customSimilarity);
Query query = new TermQuery(new Term("contents", "doc0"));
// Get a score from IndexSearcher
TopDocs topDocs = srchr.Search(query, null, 1);
float score1 = topDocs.GetMaxScore();
// Get the score from MultiSearcher
topDocs = msrchr.Search(query, null, 1);
float scoreN = topDocs.GetMaxScore();
// The scores from the IndexSearcher and Multisearcher should be the same
// if the same similarity is used.
Assert.AreEqual(score1, scoreN, 1e-6, "MultiSearcher score must be equal to single esrcher score!");
}
示例3: TestSimilarity_
public virtual void TestSimilarity_()
{
RAMDirectory store = new RAMDirectory();
IndexWriter writer = new IndexWriter(store, new SimpleAnalyzer(), true);
writer.SetSimilarity(new SimpleSimilarity());
Document d1 = new Document();
d1.Add(Field.Text("Field", "a c"));
Document d2 = new Document();
d2.Add(Field.Text("Field", "a b c"));
writer.AddDocument(d1);
writer.AddDocument(d2);
writer.Optimize();
writer.Close();
float[] scores = new float[4];
Searcher searcher = new IndexSearcher(store);
searcher.SetSimilarity(new SimpleSimilarity());
Term a = new Term("Field", "a");
Term b = new Term("Field", "b");
Term c = new Term("Field", "c");
searcher.Search(new TermQuery(b), new AnonymousClassHitCollector(this));
BooleanQuery bq = new BooleanQuery();
bq.Add(new TermQuery(a), false, false);
bq.Add(new TermQuery(b), false, false);
//System.out.println(bq.toString("Field"));
searcher.Search(bq, new AnonymousClassHitCollector1(this));
PhraseQuery pq = new PhraseQuery();
pq.Add(a);
pq.Add(c);
//System.out.println(pq.toString("Field"));
searcher.Search(pq, new AnonymousClassHitCollector2(this));
pq.SetSlop(2);
//System.out.println(pq.toString("Field"));
searcher.Search(pq, new AnonymousClassHitCollector3(this));
}
示例4: WrapUnderlyingReader
/// <summary> Given an IndexSearcher, returns a new IndexSearcher whose IndexReader
/// is a MultiReader containing the Reader of the original IndexSearcher,
/// as well as several "empty" IndexReaders -- some of which will have
/// deleted documents in them. This new IndexSearcher should
/// behave exactly the same as the original IndexSearcher.
/// </summary>
/// <param name="s">the searcher to wrap
/// </param>
/// <param name="edge">if negative, s will be the first sub; if 0, s will be in the middle, if positive s will be the last sub
/// </param>
public static IndexSearcher WrapUnderlyingReader(IndexSearcher s, int edge)
{
IndexReader r = s.GetIndexReader();
// we can't put deleted docs before the nested reader, because
// it will throw off the docIds
IndexReader[] readers = new IndexReader[] {
edge < 0 ? r : IndexReader.Open(MakeEmptyIndex(0), true),
IndexReader.Open(MakeEmptyIndex(0), true),
new MultiReader(new IndexReader[] {
IndexReader.Open(MakeEmptyIndex(edge < 0 ? 4 : 0), true),
IndexReader.Open(MakeEmptyIndex(0), true),
0 == edge ? r : IndexReader.Open(MakeEmptyIndex(0), true)
}),
IndexReader.Open(MakeEmptyIndex(0 < edge ? 0 : 7), true),
IndexReader.Open(MakeEmptyIndex(0), true),
new MultiReader(new IndexReader[] {
IndexReader.Open(MakeEmptyIndex(0 < edge ? 0 : 5), true),
IndexReader.Open(MakeEmptyIndex(0), true),
0 < edge ? r : IndexReader.Open(MakeEmptyIndex(0), true)
})
};
IndexSearcher out_Renamed = new IndexSearcher(new MultiReader(readers));
out_Renamed.SetSimilarity(s.GetSimilarity());
return out_Renamed;
}
示例5: TestSimilarity_Renamed
public virtual void TestSimilarity_Renamed()
{
RAMDirectory store = new RAMDirectory();
IndexWriter writer = new IndexWriter(store, new SimpleAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED);
writer.SetSimilarity(new SimpleSimilarity());
Document d1 = new Document();
d1.Add(new Field("field", "a c", Field.Store.YES, Field.Index.ANALYZED));
Document d2 = new Document();
d2.Add(new Field("field", "a b c", Field.Store.YES, Field.Index.ANALYZED));
writer.AddDocument(d1);
writer.AddDocument(d2);
writer.Optimize();
writer.Close();
Searcher searcher = new IndexSearcher(store);
searcher.SetSimilarity(new SimpleSimilarity());
Term a = new Term("field", "a");
Term b = new Term("field", "b");
Term c = new Term("field", "c");
searcher.Search(new TermQuery(b), new AnonymousClassCollector(this));
BooleanQuery bq = new BooleanQuery();
bq.Add(new TermQuery(a), BooleanClause.Occur.SHOULD);
bq.Add(new TermQuery(b), BooleanClause.Occur.SHOULD);
//System.out.println(bq.toString("field"));
searcher.Search(bq, new AnonymousClassCollector1(this));
PhraseQuery pq = new PhraseQuery();
pq.Add(a);
pq.Add(c);
//System.out.println(pq.toString("field"));
searcher.Search(pq, new AnonymousClassCollector2(this));
pq.SetSlop(2);
//System.out.println(pq.toString("field"));
searcher.Search(pq, new AnonymousClassCollector3(this));
}
示例6: SetUp
public override void SetUp()
{
base.SetUp();
RAMDirectory directory = new RAMDirectory();
PayloadAnalyzer analyzer = new PayloadAnalyzer(this);
IndexWriter writer = new IndexWriter(directory, analyzer, true);
writer.SetSimilarity(similarity);
//writer.infoStream = System.out;
for (int i = 0; i < 1000; i++)
{
Document doc = new Document();
Field noPayloadField = new Field("noPayLoad", English.IntToEnglish(i), Field.Store.YES, Field.Index.TOKENIZED);
noPayloadField.SetBoost(0);
doc.Add(noPayloadField);
doc.Add(new Field("field", English.IntToEnglish(i), Field.Store.YES, Field.Index.TOKENIZED));
doc.Add(new Field("multiField", English.IntToEnglish(i) + " " + English.IntToEnglish(i), Field.Store.YES, Field.Index.TOKENIZED));
writer.AddDocument(doc);
}
writer.Optimize();
writer.Close();
searcher = new IndexSearcher(directory);
searcher.SetSimilarity(similarity);
}
示例7: GetSearcher
public IndexSearcher GetSearcher(string indexName)
{
IndexSearcher ret;
if (indexSearchers.TryGetValue(indexName, out ret) && ret != null)
return ret;
var indexPath = Path.Combine(IndexesStoragePath, indexName);
if (!Directory.Exists(indexPath))
return null;
ret = new IndexSearcher(FSDirectory.Open(new DirectoryInfo(indexPath)), true);
ret.SetSimilarity(new BinaryCoordSimilarity()); // to boost docs containing all terms, to overcome issues
// concerning many terms in index produced by HebMorph
if (!indexSearchers.TryAdd(indexName, ret))
{
ret.Close();
return null;
}
return ret;
}