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!"); }
/// <summary> Given a Searcher, returns a new MultiSearcher wrapping the /// the original Searcher, /// as well as several "empty" IndexSearchers -- some of which will have /// deleted documents in them. This new MultiSearcher /// should behave exactly the same as the original Searcher. /// </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 hte middle, if positive s will be the last sub /// </param> public static MultiSearcher WrapSearcher(Searcher s, int edge) { // we can't put deleted docs before the nested reader, because // it will through off the docIds Searcher[] searchers = new Searcher[] { edge < 0?s:new IndexSearcher(MakeEmptyIndex(0)), new MultiSearcher(new Searcher[] { new IndexSearcher(MakeEmptyIndex(edge < 0?65:0)), new IndexSearcher(MakeEmptyIndex(0)), 0 == edge?s:new IndexSearcher(MakeEmptyIndex(0)) }), new IndexSearcher(MakeEmptyIndex(0 < edge?0:3)), new IndexSearcher(MakeEmptyIndex(0)), new MultiSearcher(new Searcher[] { new IndexSearcher(MakeEmptyIndex(0 < edge?0:5)), new IndexSearcher(MakeEmptyIndex(0)), 0 < edge?s:new IndexSearcher(MakeEmptyIndex(0)) }) }; MultiSearcher out_Renamed = new MultiSearcher(searchers); out_Renamed.SetSimilarity(s.GetSimilarity()); return(out_Renamed); }
/// <summary> Given a Searcher, returns a new MultiSearcher wrapping the /// the original Searcher, /// as well as several "empty" IndexSearchers -- some of which will have /// deleted documents in them. This new MultiSearcher /// should behave exactly the same as the original Searcher. /// </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 hte middle, if positive s will be the last sub /// </param> public static MultiSearcher WrapSearcher(Searcher s, int edge) { // we can't put deleted docs before the nested reader, because // it will through off the docIds Searcher[] searchers = new Searcher[]{edge < 0?s:new IndexSearcher(MakeEmptyIndex(0)), new MultiSearcher(new Searcher[]{new IndexSearcher(MakeEmptyIndex(edge < 0?65:0)), new IndexSearcher(MakeEmptyIndex(0)), 0 == edge?s:new IndexSearcher(MakeEmptyIndex(0))}), new IndexSearcher(MakeEmptyIndex(0 < edge?0:3)), new IndexSearcher(MakeEmptyIndex(0)), new MultiSearcher(new Searcher[]{new IndexSearcher(MakeEmptyIndex(0 < edge?0:5)), new IndexSearcher(MakeEmptyIndex(0)), 0 < edge?s:new IndexSearcher(MakeEmptyIndex(0))})}; MultiSearcher out_Renamed = new MultiSearcher(searchers); out_Renamed.SetSimilarity(s.GetSimilarity()); return out_Renamed; }