public virtual Query VisitQuery(Query qry) { if (qry == null) return null; if (qry is TermQuery) { return VisitTermQuery((TermQuery)qry); } //else if (qry is MultiTermQuery) //{ // -> abstract //} else if (qry is BooleanQuery) { return VisitBooleanQuery((BooleanQuery)qry); } else if (qry is WildcardQuery) { return VisitWildcardQuery((WildcardQuery)qry); } else if (qry is PhraseQuery) { return VisitPhraseQuery((PhraseQuery)qry); } else if (qry is PrefixQuery) { return VisitPrefixQuery((PrefixQuery)qry); } else if (qry is MultiPhraseQuery) { return VisitMultiPhraseQuery((MultiPhraseQuery)qry); } else if (qry is FuzzyQuery) { return VisitFuzzyQuery((FuzzyQuery)qry); } else if (qry is TermRangeQuery) { return VisitTermRangeQuery((TermRangeQuery)qry); } else if (qry.GetType().IsGenericType && qry.GetType().GetGenericTypeDefinition() == typeof(NumericRangeQuery<>)) { return VisitNumericRangeQuery((MultiTermQuery)qry); } //else if (qry is SpanQuery) //{ // -> abstract //} Logging.Log.Warn(string.Format("Unknown query type: '{0}'", qry.GetType().FullName)); return qry; }
private static void Visit(Query query, AzureQueryLogger.IndentedTextWriter writer) { writer.WriteLine("Query Type: {0}", (object)query.GetType().FullName); ++writer.Indent; writer.WriteLine("Boost: {0}", (object)query.Boost); if (query is BooleanQuery) AzureQueryLogger.VisitQuery((BooleanQuery)query, writer); if (query is TermQuery) AzureQueryLogger.VisitQuery((TermQuery)query, writer); if (query is PhraseQuery) AzureQueryLogger.VisitQuery((PhraseQuery)query, writer); if (query is MultiTermQuery) AzureQueryLogger.VisitQuery((MultiTermQuery)query, writer); if (query is MultiPhraseQuery) AzureQueryLogger.VisitQuery((MultiPhraseQuery)query, writer); if (query is MatchAllDocsQuery) AzureQueryLogger.VisitQuery((MatchAllDocsQuery)query, writer); if (query is FieldScoreQuery) AzureQueryLogger.VisitQuery((FieldScoreQuery)query, writer); if (query is ValueSourceQuery) AzureQueryLogger.VisitQuery((ValueSourceQuery)query, writer); if (query is CustomScoreQuery) AzureQueryLogger.VisitQuery((CustomScoreQuery)query, writer); if (query is FilteredQuery) AzureQueryLogger.VisitQuery((FilteredQuery)query, writer); if (query is DisjunctionMaxQuery) AzureQueryLogger.VisitQuery((DisjunctionMaxQuery)query, writer); if (query is ConstantScoreQuery) AzureQueryLogger.VisitQuery((ConstantScoreQuery)query, writer); if (query is SpanQuery) AzureQueryLogger.VisitQuery((SpanQuery)query, writer); --writer.Indent; }
public virtual Query Visit(Query q) { var booleanq = q as BooleanQuery; if (booleanq != null) return VisitBooleanQuery(booleanq); //var boostingq = q as BoostingQuery; if (boostingq != null) return VisitBoostingQuery(boostingq); var constantScoreq = q as ConstantScoreQuery; if (constantScoreq != null) return VisitConstantScoreQuery(constantScoreq); var constantScoreRangeq = q as ConstantScoreRangeQuery; if (constantScoreRangeq != null) return VisitConstantScoreRangeQuery(constantScoreRangeq); var customScoreq = q as CustomScoreQuery; if (customScoreq != null) return VisitCustomScoreQuery(customScoreq); var disjunctionMaxq = q as DisjunctionMaxQuery; if (disjunctionMaxq != null) return VisitDisjunctionMaxQuery(disjunctionMaxq); var filteredq = q as FilteredQuery; if (filteredq != null) return VisitFilteredQuery(filteredq); //var fuzzyLikeThisq = q as FuzzyLikeThisQuery; if (fuzzyLikeThisq != null) return VisitFuzzyLikeThisQuery(fuzzyLikeThisq); var matchAllDocsq = q as MatchAllDocsQuery; if (matchAllDocsq != null) return VisitMatchAllDocsQuery(matchAllDocsq); //var moreLikeThisq = q as MoreLikeThisQuery; if (moreLikeThisq != null) return VisitMoreLikeThisQuery(moreLikeThisq); var multiPhraseq = q as MultiPhraseQuery; if (multiPhraseq != null) return VisitMultiPhraseQuery(multiPhraseq); var fuzzyq = q as FuzzyQuery; if (fuzzyq != null) return VisitFuzzyQuery(fuzzyq); var wildcardq = q as WildcardQuery; if (wildcardq != null) return VisitWildcardQuery(wildcardq); var phraseq = q as PhraseQuery; if (phraseq != null) return VisitPhraseQuery(phraseq); var prefixq = q as PrefixQuery; if (prefixq != null) return VisitPrefixQuery(prefixq); var rangeq = q as RangeQuery; if (rangeq != null) return VisitRangeQuery(rangeq); var spanFirstq = q as SpanFirstQuery; if (spanFirstq != null) return VisitSpanFirstQuery(spanFirstq); var spanNearq = q as SpanNearQuery; if (spanNearq != null) return VisitSpanNearQuery(spanNearq); var spanNotq = q as SpanNotQuery; if (spanNotq != null) return VisitSpanNotQuery(spanNotq); var spanOrq = q as SpanOrQuery; if (spanOrq != null) return VisitSpanOrQuery(spanOrq); var spanTermq = q as SpanTermQuery; if (spanTermq != null) return VisitSpanTermQuery(spanTermq); var termq = q as TermQuery; if (termq != null) return VisitTermQuery(termq); var valueSourceq = q as ValueSourceQuery; if (valueSourceq != null) return VisitValueSourceQuery(valueSourceq); var fieldScoreq = q as FieldScoreQuery; if (fieldScoreq != null) return VisitFieldScoreQuery(fieldScoreq); // <V2.9.2> var termRangeq = q as TermRangeQuery; if (termRangeq != null) return VisitTermRangeQuery(termRangeq); var numericRangeq = q as NumericRangeQuery; if (numericRangeq != null) return VisitNumericRangeQuery(numericRangeq); // </V2.9.2> throw new NotImplementedException("Unknown query type: " + q.GetType().FullName); }
private SectionSearchQueryPlan Translate(Query query) { if (query != null) { if (query is TermQuery) { return TranslateTermQuery((TermQuery)query); } else if (query is PhraseQuery) { return TranslatePhraseQuery((PhraseQuery)query); } else if (query is BooleanQuery) { return TranslateBooleanQuery((BooleanQuery)query); } else if (query is MetaDataQuery) { MetaDataQuery mquery = (MetaDataQuery)query; IMetaDataCache cache = (_cacheProvider != null ? _cacheProvider.Get(mquery.Term) : null); if (cache != null) { return ((MetaDataQuery)query).GetPlan(cache); } else { return ((MetaDataQuery)query).GetPlan(_reader); } } else { throw new TranslationException("unable to translate Query class: " + query.GetType().Name); } } return null; }
public virtual void Collect(int doc) { float score = sc.GetScore(); lastDoc[0] = doc; try { if (scorer == null) { Weight w = s.CreateNormalizedWeight(q); AtomicReaderContext context = readerContextArray[leafPtr]; scorer = w.GetScorer(context, (context.AtomicReader).LiveDocs); } int op = order[(opidx[0]++) % order.Length]; // System.out.println(op==skip_op ? // "skip("+(sdoc[0]+1)+")":"next()"); bool more = op == skip_op?scorer.Advance(scorer.DocID + 1) != DocIdSetIterator.NO_MORE_DOCS : scorer.NextDoc() != DocIdSetIterator.NO_MORE_DOCS; int scorerDoc = scorer.DocID; float scorerScore = scorer.GetScore(); float scorerScore2 = scorer.GetScore(); float scoreDiff = Math.Abs(score - scorerScore); float scorerDiff = Math.Abs(scorerScore2 - scorerScore); if (!more || doc != scorerDoc || scoreDiff > maxDiff || scorerDiff > maxDiff) { StringBuilder sbord = new StringBuilder(); for (int i = 0; i < order.Length; i++) { sbord.Append(order[i] == skip_op ? " skip()" : " next()"); } throw new Exception("ERROR matching docs:" + "\n\t" + (doc != scorerDoc ? "--> " : "") + "doc=" + doc + ", scorerDoc=" + scorerDoc + "\n\t" + (!more ? "--> " : "") + "tscorer.more=" + more + "\n\t" + (scoreDiff > maxDiff ? "--> " : "") + "scorerScore=" + scorerScore + " scoreDiff=" + scoreDiff + " maxDiff=" + maxDiff + "\n\t" + (scorerDiff > maxDiff ? "--> " : "") + "scorerScore2=" + scorerScore2 + " scorerDiff=" + scorerDiff + "\n\thitCollector.Doc=" + doc + " score=" + score + "\n\t Scorer=" + scorer + "\n\t Query=" + q + " " + q.GetType().Name + "\n\t Searcher=" + s + "\n\t Order=" + sbord + "\n\t Op=" + (op == skip_op ? " skip()" : " next()")); } } catch (IOException e) { throw new Exception(e.ToString(), e); } }
public virtual void TestParsingAndSearching() { string field = "content"; string[] docs = new string[] { "\\ abcdefg1", "\\x00079 hijklmn1", "\\\\ opqrstu1" }; // queries that should find all docs Query[] matchAll = new Query[] { new WildcardQuery(new Term(field, "*")), new WildcardQuery(new Term(field, "*1")), new WildcardQuery(new Term(field, "**1")), new WildcardQuery(new Term(field, "*?")), new WildcardQuery(new Term(field, "*?1")), new WildcardQuery(new Term(field, "?*1")), new WildcardQuery(new Term(field, "**")), new WildcardQuery(new Term(field, "***")), new WildcardQuery(new Term(field, "\\\\*")) }; // queries that should find no docs Query[] matchNone = new Query[] { new WildcardQuery(new Term(field, "a*h")), new WildcardQuery(new Term(field, "a?h")), new WildcardQuery(new Term(field, "*a*h")), new WildcardQuery(new Term(field, "?a")), new WildcardQuery(new Term(field, "a?")) }; PrefixQuery[][] matchOneDocPrefix = new PrefixQuery[][] { new PrefixQuery[] { new PrefixQuery(new Term(field, "a")), new PrefixQuery(new Term(field, "ab")), new PrefixQuery(new Term(field, "abc")) }, new PrefixQuery[] { new PrefixQuery(new Term(field, "h")), new PrefixQuery(new Term(field, "hi")), new PrefixQuery(new Term(field, "hij")), new PrefixQuery(new Term(field, "\\x0007")) }, new PrefixQuery[] { new PrefixQuery(new Term(field, "o")), new PrefixQuery(new Term(field, "op")), new PrefixQuery(new Term(field, "opq")), new PrefixQuery(new Term(field, "\\\\")) } }; WildcardQuery[][] matchOneDocWild = new WildcardQuery[][] { new WildcardQuery[] { new WildcardQuery(new Term(field, "*a*")), new WildcardQuery(new Term(field, "*ab*")), new WildcardQuery(new Term(field, "*abc**")), new WildcardQuery(new Term(field, "ab*e*")), new WildcardQuery(new Term(field, "*g?")), new WildcardQuery(new Term(field, "*f?1")) }, new WildcardQuery[] { new WildcardQuery(new Term(field, "*h*")), new WildcardQuery(new Term(field, "*hi*")), new WildcardQuery(new Term(field, "*hij**")), new WildcardQuery(new Term(field, "hi*k*")), new WildcardQuery(new Term(field, "*n?")), new WildcardQuery(new Term(field, "*m?1")), new WildcardQuery(new Term(field, "hij**")) }, new WildcardQuery[] { new WildcardQuery(new Term(field, "*o*")), new WildcardQuery(new Term(field, "*op*")), new WildcardQuery(new Term(field, "*opq**")), new WildcardQuery(new Term(field, "op*q*")), new WildcardQuery(new Term(field, "*u?")), new WildcardQuery(new Term(field, "*t?1")), new WildcardQuery(new Term(field, "opq**")) } }; // prepare the index Directory dir = NewDirectory(); RandomIndexWriter iw = new RandomIndexWriter(Random(), dir, NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random())).SetMergePolicy(NewLogMergePolicy())); for (int i = 0; i < docs.Length; i++) { Document doc = new Document(); doc.Add(NewTextField(field, docs[i], Field.Store.NO)); iw.AddDocument(doc); } iw.Dispose(); IndexReader reader = DirectoryReader.Open(dir); IndexSearcher searcher = NewSearcher(reader); // test queries that must find all foreach (Query q in matchAll) { if (VERBOSE) { Console.WriteLine("matchAll: q=" + q + " " + q.GetType().Name); } ScoreDoc[] hits = searcher.Search(q, null, 1000).ScoreDocs; Assert.AreEqual(docs.Length, hits.Length); } // test queries that must find none foreach (Query q in matchNone) { if (VERBOSE) { Console.WriteLine("matchNone: q=" + q + " " + q.GetType().Name); } ScoreDoc[] hits = searcher.Search(q, null, 1000).ScoreDocs; Assert.AreEqual(0, hits.Length); } // thest the prefi queries find only one doc for (int i = 0; i < matchOneDocPrefix.Length; i++) { for (int j = 0; j < matchOneDocPrefix[i].Length; j++) { Query q = matchOneDocPrefix[i][j]; if (VERBOSE) { Console.WriteLine("match 1 prefix: doc=" + docs[i] + " q=" + q + " " + q.GetType().Name); } ScoreDoc[] hits = searcher.Search(q, null, 1000).ScoreDocs; Assert.AreEqual(1, hits.Length); Assert.AreEqual(i, hits[0].Doc); } } // test the wildcard queries find only one doc for (int i = 0; i < matchOneDocWild.Length; i++) { for (int j = 0; j < matchOneDocWild[i].Length; j++) { Query q = matchOneDocWild[i][j]; if (VERBOSE) { Console.WriteLine("match 1 wild: doc=" + docs[i] + " q=" + q + " " + q.GetType().Name); } ScoreDoc[] hits = searcher.Search(q, null, 1000).ScoreDocs; Assert.AreEqual(1, hits.Length); Assert.AreEqual(i, hits[0].Doc); } } reader.Dispose(); dir.Dispose(); }
public virtual void TestParsingAndSearching() { System.String field = "content"; bool dbg = false; QueryParser qp = new QueryParser(field, new WhitespaceAnalyzer()); qp.SetAllowLeadingWildcard(true); System.String[] docs = new System.String[] { "\\ abcdefg1", "\\79 hijklmn1", "\\\\ opqrstu1" }; // queries that should find all docs System.String[] matchAll = new System.String[] { "*", "*1", "**1", "*?", "*?1", "?*1", "**", "***", "\\\\*" }; // queries that should find no docs System.String[] matchNone = new System.String[] { "a*h", "a?h", "*a*h", "?a", "a?" }; // queries that should be parsed to prefix queries System.String[][] matchOneDocPrefix = new System.String[][] { new System.String[] { "a*", "ab*", "abc*" }, new System.String[] { "h*", "hi*", "hij*", "\\\\7*" }, new System.String[] { "o*", "op*", "opq*", "\\\\\\\\*" } }; // queries that should be parsed to wildcard queries System.String[][] matchOneDocWild = new System.String[][] { new System.String[] { "*a*", "*ab*", "*abc**", "ab*e*", "*g?", "*f?1", "abc**" }, new System.String[] { "*h*", "*hi*", "*hij**", "hi*k*", "*n?", "*m?1", "hij**" }, new System.String[] { "*o*", "*op*", "*opq**", "op*q*", "*u?", "*t?1", "opq**" } }; // prepare the index RAMDirectory dir = new RAMDirectory(); IndexWriter iw = new IndexWriter(dir, new WhitespaceAnalyzer(), IndexWriter.MaxFieldLength.LIMITED); for (int i = 0; i < docs.Length; i++) { Document doc = new Document(); doc.Add(new Field(field, docs[i], Field.Store.NO, Field.Index.ANALYZED)); iw.AddDocument(doc); } iw.Close(); IndexSearcher searcher = new IndexSearcher(dir); // test queries that must find all for (int i = 0; i < matchAll.Length; i++) { System.String qtxt = matchAll[i]; Query q = qp.Parse(qtxt); if (dbg) { System.Console.Out.WriteLine("matchAll: qtxt=" + qtxt + " q=" + q + " " + q.GetType().FullName); } ScoreDoc[] hits = searcher.Search(q, null, 1000).scoreDocs; Assert.AreEqual(docs.Length, hits.Length); } // test queries that must find none for (int i = 0; i < matchNone.Length; i++) { System.String qtxt = matchNone[i]; Query q = qp.Parse(qtxt); if (dbg) { System.Console.Out.WriteLine("matchNone: qtxt=" + qtxt + " q=" + q + " " + q.GetType().FullName); } ScoreDoc[] hits = searcher.Search(q, null, 1000).scoreDocs; Assert.AreEqual(0, hits.Length); } // test queries that must be prefix queries and must find only one doc for (int i = 0; i < matchOneDocPrefix.Length; i++) { for (int j = 0; j < matchOneDocPrefix[i].Length; j++) { System.String qtxt = matchOneDocPrefix[i][j]; Query q = qp.Parse(qtxt); if (dbg) { System.Console.Out.WriteLine("match 1 prefix: doc=" + docs[i] + " qtxt=" + qtxt + " q=" + q + " " + q.GetType().FullName); } Assert.AreEqual(typeof(PrefixQuery), q.GetType()); ScoreDoc[] hits = searcher.Search(q, null, 1000).scoreDocs; Assert.AreEqual(1, hits.Length); Assert.AreEqual(i, hits[0].doc); } } // test queries that must be wildcard queries and must find only one doc for (int i = 0; i < matchOneDocPrefix.Length; i++) { for (int j = 0; j < matchOneDocWild[i].Length; j++) { System.String qtxt = matchOneDocWild[i][j]; Query q = qp.Parse(qtxt); if (dbg) { System.Console.Out.WriteLine("match 1 wild: doc=" + docs[i] + " qtxt=" + qtxt + " q=" + q + " " + q.GetType().FullName); } Assert.AreEqual(typeof(WildcardQuery), q.GetType()); ScoreDoc[] hits = searcher.Search(q, null, 1000).scoreDocs; Assert.AreEqual(1, hits.Length); Assert.AreEqual(i, hits[0].doc); } } searcher.Close(); }
private void AddInternal(Query query, Occur occur, OperatorType @operator, List <string> buildSteps) { if (query is RavenBooleanQuery booleanQuery) { if (booleanQuery._operator == @operator && booleanQuery.AnyBoost == false) { foreach (var booleanClause in booleanQuery.Clauses) { Add(booleanClause); } return; } else { buildSteps?.Add($"Cannot apply query optimization because operator is {@operator}, but we got {booleanQuery._operator} with boosting '{booleanQuery.AnyBoost}' ({booleanQuery.Boost} - {SingleToInt32Bits(booleanQuery.Boost)})"); } } buildSteps?.Add($"Cannot apply query optimization because query ({query}) is of type {query.GetType()}."); Add(query, occur);
private Lucene.Net.Search.Query ResolveQuery(Query query) { var type = query.GetType(); if (type == typeof(BooleanQuery)) return GetBooleanQuery((BooleanQuery)query); if (type == typeof(ContainsQuery)) return GetContainsQuery((ContainsQuery)query); if (type == typeof(NotQuery)) return GetNotQuery((NotQuery)query); if (type == typeof(TermQuery)) return GetTermQuery((TermQuery)query); if (type == typeof(RangeQuery)) return GetRangeQuery((RangeQuery)query); if (type == typeof(WildCardQuery)) return GetWildCardQuery((WildCardQuery)query); if (type == typeof(DateRangeQuery)) return GetDateRangeQuery((DateRangeQuery) query); throw new NotImplementedException("Query Type " + type.Name + " has not been translated in " + GetType().Name); }