public Explain ( |
||
weight | ||
doc | int | |
return |
public AutoCompletionResult Autocomplete(string text, bool includeExplanation = false) { if (string.IsNullOrWhiteSpace(text)) return AutoCompletionResult.NoResult(text); var searchers = _directoryFactory.GetAllDirectories().Select(d => { try { return new IndexSearcher(d, true); } catch (Exception e) { _log.Error(e, "While searching directory {0}", d); return null; } }) .Where(s => s != null) .ToArray(); using (var searcher = new MultiSearcher(searchers)) { try { BooleanQuery query = GetQueryForText(text); var results = searcher.Search(query, 10); var commands = results.ScoreDocs .Select(d => { var document = searcher.Doc(d.Doc); try { Explanation explanation = null; if (includeExplanation) { explanation = searcher.Explain(query, d.Doc); } var coreDoc = CoreDocument.Rehydrate(document); var command = _converterRepository.FromDocumentToItem(coreDoc); return new AutoCompletionResult.CommandResult(command, coreDoc.GetDocumentId(), explanation); } catch (Exception e) { _log.Error(e, "Error getting command result for document {0}:{1}", document.GetField(SpecialFields.ConverterId).StringValue, document.GetField(SpecialFields.Id).StringValue); return null; } }) .Where(r => r != null); return AutoCompletionResult.OrderedResult(text, commands); } catch (ParseException e) { _log.Error(e, "Error parsing '{0}'", text); return AutoCompletionResult.NoResult(text); } } }
public virtual void TestTermQueryMultiSearcherExplain() { // creating two directories for indices Directory indexStoreA = new MockRAMDirectory(); Directory indexStoreB = new MockRAMDirectory(); Document lDoc = new Document(); lDoc.Add(new Field("handle", "1 2", Field.Store.YES, Field.Index.ANALYZED)); Document lDoc2 = new Document(); lDoc2.Add(new Field("handle", "1 2", Field.Store.YES, Field.Index.ANALYZED)); Document lDoc3 = new Document(); lDoc3.Add(new Field("handle", "1 2", Field.Store.YES, Field.Index.ANALYZED)); IndexWriter writerA = new IndexWriter(indexStoreA, new StandardAnalyzer(Util.Version.LUCENE_CURRENT), true, IndexWriter.MaxFieldLength.LIMITED); IndexWriter writerB = new IndexWriter(indexStoreB, new StandardAnalyzer(Util.Version.LUCENE_CURRENT), true, IndexWriter.MaxFieldLength.LIMITED); writerA.AddDocument(lDoc); writerA.AddDocument(lDoc2); writerA.Optimize(); writerA.Close(); writerB.AddDocument(lDoc3); writerB.Close(); QueryParser parser = new QueryParser(Util.Version.LUCENE_CURRENT, "fulltext", new StandardAnalyzer(Util.Version.LUCENE_CURRENT)); Query query = parser.Parse("handle:1"); Searcher[] searchers = new Searcher[2]; searchers[0] = new IndexSearcher(indexStoreB, true); searchers[1] = new IndexSearcher(indexStoreA, true); Searcher mSearcher = new MultiSearcher(searchers); ScoreDoc[] hits = mSearcher.Search(query, null, 1000).ScoreDocs; Assert.AreEqual(3, hits.Length); Explanation explain = mSearcher.Explain(query, hits[0].Doc); System.String exp = explain.ToString(0); Assert.IsTrue(exp.IndexOf("maxDocs=3") > - 1, exp); Assert.IsTrue(exp.IndexOf("docFreq=3") > - 1, exp); query = parser.Parse("handle:\"1 2\""); hits = mSearcher.Search(query, null, 1000).ScoreDocs; Assert.AreEqual(3, hits.Length); explain = mSearcher.Explain(query, hits[0].Doc); exp = explain.ToString(0); Assert.IsTrue(exp.IndexOf("1=3") > - 1, exp); Assert.IsTrue(exp.IndexOf("2=3") > - 1, exp); query = new SpanNearQuery(new SpanQuery[]{new SpanTermQuery(new Term("handle", "1")), new SpanTermQuery(new Term("handle", "2"))}, 0, true); hits = mSearcher.Search(query, null, 1000).ScoreDocs; Assert.AreEqual(3, hits.Length); explain = mSearcher.Explain(query, hits[0].Doc); exp = explain.ToString(0); Assert.IsTrue(exp.IndexOf("1=3") > - 1, exp); Assert.IsTrue(exp.IndexOf("2=3") > - 1, exp); mSearcher.Close(); }
public virtual void TestTermQueryMultiSearcherExplain() { // creating two directories for indices Directory indexStoreA = new MockRAMDirectory(); Directory indexStoreB = new MockRAMDirectory(); Document lDoc = new Document(); lDoc.Add(new Field("handle", "1 2", Field.Store.YES, Field.Index.ANALYZED)); Document lDoc2 = new Document(); lDoc2.Add(new Field("handle", "1 2", Field.Store.YES, Field.Index.ANALYZED)); Document lDoc3 = new Document(); lDoc3.Add(new Field("handle", "1 2", Field.Store.YES, Field.Index.ANALYZED)); IndexWriter writerA = new IndexWriter(indexStoreA, new StandardAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED); IndexWriter writerB = new IndexWriter(indexStoreB, new StandardAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED); writerA.AddDocument(lDoc); writerA.AddDocument(lDoc2); writerA.Optimize(); writerA.Close(); writerB.AddDocument(lDoc3); writerB.Close(); QueryParser parser = new QueryParser("fulltext", new StandardAnalyzer()); Query query = parser.Parse("handle:1"); Searcher[] searchers = new Searcher[2]; searchers[0] = new IndexSearcher(indexStoreB); searchers[1] = new IndexSearcher(indexStoreA); Searcher mSearcher = new MultiSearcher(searchers); ScoreDoc[] hits = mSearcher.Search(query, null, 1000).scoreDocs; Assert.AreEqual(3, hits.Length); Explanation explain = mSearcher.Explain(query, hits[0].doc); System.String exp = explain.ToString(0); Assert.IsTrue(exp.IndexOf("maxDocs=3") > -1, exp); Assert.IsTrue(exp.IndexOf("docFreq=3") > -1, exp); query = parser.Parse("handle:\"1 2\""); hits = mSearcher.Search(query, null, 1000).scoreDocs; Assert.AreEqual(3, hits.Length); explain = mSearcher.Explain(query, hits[0].doc); exp = explain.ToString(0); Assert.IsTrue(exp.IndexOf("1=3") > -1, exp); Assert.IsTrue(exp.IndexOf("2=3") > -1, exp); query = new SpanNearQuery(new SpanQuery[] { new SpanTermQuery(new Term("handle", "1")), new SpanTermQuery(new Term("handle", "2")) }, 0, true); hits = mSearcher.Search(query, null, 1000).scoreDocs; Assert.AreEqual(3, hits.Length); explain = mSearcher.Explain(query, hits[0].doc); exp = explain.ToString(0); Assert.IsTrue(exp.IndexOf("1=3") > -1, exp); Assert.IsTrue(exp.IndexOf("2=3") > -1, exp); mSearcher.Close(); }