Esempio n. 1
0
        public void OverlappingSpans_Part4()
        {
            var lm = MakeSpan(0, cc, dd);
            var rm = MakeSpan(0, dd, cc);

            // Rewriting with SpanNotQueries works!
            Console.WriteLine("((cc dd) - (dd cc)) ** (dd cc)");
            var not = new SpanNotQuery(lm, rm);
            var q4  = MakeSpan(16, not, rm);

            TestIndex.AssertQuery(q4, 3);

            // This is now implemented int the parser
            TestIndex.AssertQuery(Content("cc dd ** dd cc"), 3);
        }
Esempio n. 2
0
        public void OverlappingSpans_Part6()
        {
            // But the reverse ways of excluding spans works
            // (dd ee * ee) => (dd ee) * (ee -(dd ee))
            var span = MakeSpan(0, dd, ee);
            var not  = new SpanNotQuery(ee, span);
            var q6   = MakeSpan(1, span, not);

            TestIndex.AssertQuery(q6, 3);

            // The parser now implements this strategy
            var q = Content("dd ee * ee");

            TestIndex.AssertQuery(Content("dd ee * ee"), 3);
        }
Esempio n. 3
0
        public override SpanQuery GetSpanQuery(XmlElement e)
        {
            XmlElement includeElem = DOMUtils.GetChildByTagOrFail(e, "Include");

            includeElem = DOMUtils.GetFirstChildOrFail(includeElem);

            XmlElement excludeElem = DOMUtils.GetChildByTagOrFail(e, "Exclude");

            excludeElem = DOMUtils.GetFirstChildOrFail(excludeElem);

            SpanQuery include = factory.GetSpanQuery(includeElem);
            SpanQuery exclude = factory.GetSpanQuery(excludeElem);

            SpanNotQuery snq = new SpanNotQuery(include, exclude);

            snq.Boost = DOMUtils.GetAttribute(e, "boost", 1.0f);
            return(snq);
        }
        public void TestSpanNot()
        {
            Directory dir = NewDirectory();
            // use simpleanalyzer for more natural tokenization (else "test." is a token)
            Analyzer          analyzer = new MockAnalyzer(Random, MockTokenizer.SIMPLE, true);
            IndexWriterConfig iwc      = NewIndexWriterConfig(TEST_VERSION_CURRENT, analyzer);

            iwc.SetMergePolicy(NewLogMergePolicy());
            RandomIndexWriter iw = new RandomIndexWriter(Random, dir, iwc);

            FieldType offsetsType = new FieldType(TextField.TYPE_STORED);

            offsetsType.IndexOptions = (IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
            Field    body = new Field("body", "", offsetsType);
            Document doc  = new Document();

            doc.Add(body);

            body.SetStringValue("This is a test.");
            iw.AddDocument(doc);
            body.SetStringValue("Test a one sentence document.");
            iw.AddDocument(doc);

            IndexReader ir = iw.GetReader();

            iw.Dispose();

            IndexSearcher          searcher    = NewSearcher(ir);
            ICUPostingsHighlighter highlighter = new PostingsHighlighterAnalyzerHelper(analyzer);
            SpanQuery include = new SpanMultiTermQueryWrapper <WildcardQuery>(new WildcardQuery(new Term("body", "te*")));
            SpanQuery exclude = new SpanTermQuery(new Term("body", "bogus"));
            Query     query   = new SpanNotQuery(include, exclude);
            TopDocs   topDocs = searcher.Search(query, null, 10, Sort.INDEXORDER);

            assertEquals(2, topDocs.TotalHits);
            String[] snippets = highlighter.Highlight("body", query, searcher, topDocs);
            assertEquals(2, snippets.Length);
            assertEquals("This is a <b>test</b>.", snippets[0]);
            assertEquals("<b>Test</b> a one sentence document.", snippets[1]);

            ir.Dispose();
            dir.Dispose();
        }
Esempio n. 5
0
            public override Query Rewrite(IndexReader reader)
            {
                // ArrayList spanClauses = new ArrayList();
                if (contents is TermQuery)
                {
                    return(contents);
                }
                // Build a sequence of Span clauses arranged in a SpanNear - child
                // clauses can be complex
                // Booleans e.g. nots and ors etc
                int numNegatives = 0;

                if (!(contents is BooleanQuery))
                {
                    throw new ArgumentException("Unknown query type \""
                                                + contents.GetType().Name
                                                + "\" found in phrase query string \"" + phrasedQueryStringContents
                                                + "\"");
                }
                BooleanQuery bq = (BooleanQuery)contents;

                BooleanClause[] bclauses       = bq.GetClauses();
                SpanQuery[]     allSpanClauses = new SpanQuery[bclauses.Length];
                // For all clauses e.g. one* two~
                for (int i = 0; i < bclauses.Length; i++)
                {
                    // HashSet bclauseterms=new HashSet();
                    Query qc = bclauses[i].Query;
                    // Rewrite this clause e.g one* becomes (one OR onerous)
                    qc = qc.Rewrite(reader);
                    if (bclauses[i].Occur.Equals(Occur.MUST_NOT))
                    {
                        numNegatives++;
                    }

                    if (qc is BooleanQuery booleanQuery)
                    {
                        IList <SpanQuery> sc = new JCG.List <SpanQuery>();
                        AddComplexPhraseClause(sc, booleanQuery);
                        if (sc.Count > 0)
                        {
                            allSpanClauses[i] = sc[0];
                        }
                        else
                        {
                            // Insert fake term e.g. phrase query was for "Fred Smithe*" and
                            // there were no "Smithe*" terms - need to
                            // prevent match on just "Fred".
                            allSpanClauses[i] = new SpanTermQuery(new Term(field,
                                                                           "Dummy clause because no terms found - must match nothing"));
                        }
                    }
                    else
                    {
                        if (qc is TermQuery tq)
                        {
                            allSpanClauses[i] = new SpanTermQuery(tq.Term);
                        }
                        else
                        {
                            throw new ArgumentException("Unknown query type \""
                                                        + qc.GetType().Name
                                                        + "\" found in phrase query string \""
                                                        + phrasedQueryStringContents + "\"");
                        }
                    }
                }
                if (numNegatives == 0)
                {
                    // The simple case - no negative elements in phrase
                    return(new SpanNearQuery(allSpanClauses, slopFactor, inOrder));
                }
                // Complex case - we have mixed positives and negatives in the
                // sequence.
                // Need to return a SpanNotQuery
                JCG.List <SpanQuery> positiveClauses = new JCG.List <SpanQuery>();
                for (int j = 0; j < allSpanClauses.Length; j++)
                {
                    if (!bclauses[j].Occur.Equals(Occur.MUST_NOT))
                    {
                        positiveClauses.Add(allSpanClauses[j]);
                    }
                }

                SpanQuery[] includeClauses = positiveClauses
                                             .ToArray();

                SpanQuery include; // LUCENENET: IDE0059: Remove unnecessary value assignment

                if (includeClauses.Length == 1)
                {
                    include = includeClauses[0]; // only one positive clause
                }
                else
                {
                    // need to increase slop factor based on gaps introduced by
                    // negatives
                    include = new SpanNearQuery(includeClauses, slopFactor + numNegatives,
                                                inOrder);
                }
                // Use sequence of positive and negative values as the exclude.
                SpanNearQuery exclude = new SpanNearQuery(allSpanClauses, slopFactor,
                                                          inOrder);
                SpanNotQuery snot = new SpanNotQuery(include, exclude);

                return(snot);
            }
Esempio n. 6
0
 public virtual Query VisitSpanNotQuery(SpanNotQuery spanNotq)
 {
     throw new NotImplementedException();
 }
Esempio n. 7
0
 public virtual Query VisitSpanNotQuery(SpanNotQuery spanNotq)
 {
     throw new SnNotSupportedException();
 }