QueryParser which permits complex phrase query syntax eg "(john jon jonathan~) peters*".

Performs potentially multiple passes over Query text to parse any nested logic in PhraseQueries. - First pass takes any PhraseQuery content between quotes and stores for subsequent pass. All other query content is parsed as normal - Second pass parses any stored PhraseQuery content, checking all embedded clauses are referring to the same field and therefore can be rewritten as Span queries. All PhraseQuery clauses are expressed as ComplexPhraseQuery objects

This could arguably be done in one pass using a new QueryParser but here I am working within the constraints of the existing parser as a base class. This currently simply feeds all phrase content through an analyzer to select phrase terms - any "special" syntax such as * ~ * etc are not given special status

Наследование: Classic.QueryParser
Пример #1
0
        public virtual void TestHashcodeEquals()
        {
            ComplexPhraseQueryParser qp = new ComplexPhraseQueryParser(TEST_VERSION_CURRENT, defaultFieldName, analyzer);

            qp.InOrder           = true;
            qp.FuzzyPrefixLength = 1;

            String qString = "\"aaa* bbb*\"";

            Query q  = qp.Parse(qString);
            Query q2 = qp.Parse(qString);

            assertEquals(q.GetHashCode(), q2.GetHashCode());
            assertEquals(q, q2);

            qp.InOrder = (false); // SOLR-6011

            q2 = qp.Parse(qString);

            // although the general contract of hashCode can't guarantee different values, if we only change one thing
            // about a single query, it normally should result in a different value (and will with the current
            // implementation in ComplexPhraseQuery)
            assertTrue(q.GetHashCode() != q2.GetHashCode());
            assertTrue(!q.equals(q2));
            assertTrue(!q2.equals(q));
        }
Пример #2
0
        private void CheckMatches(string qString, string expectedVals)
        {
            ComplexPhraseQueryParser qp = new ComplexPhraseQueryParser(TEST_VERSION_CURRENT, defaultFieldName, analyzer);

            qp.InOrder           = inOrder;
            qp.FuzzyPrefixLength = 1; // usually a good idea

            Query q = qp.Parse(qString);

            HashSet <string> expecteds = new HashSet <string>();

            string[] vals = expectedVals.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < vals.Length; i++)
            {
                if (vals[i].Length > 0)
                {
                    expecteds.Add(vals[i]);
                }
            }

            TopDocs td = searcher.Search(q, 10);

            ScoreDoc[] sd = td.ScoreDocs;
            for (int i = 0; i < sd.Length; i++)
            {
                Document doc = searcher.Doc(sd[i].Doc);
                string   id  = doc.Get("id");
                assertTrue(qString + "matched doc#" + id + " not expected", expecteds
                           .Contains(id));
                expecteds.Remove(id);
            }

            assertEquals(qString + " missing some matches ", 0, expecteds.Count);
        }
 private void CheckBadQuery(String qString)
 {
     ComplexPhraseQueryParser qp = new ComplexPhraseQueryParser(TEST_VERSION_CURRENT, defaultFieldName, analyzer);
     qp.InOrder = inOrder;
     Exception expected = null;
     try
     {
         qp.Parse(qString);
     }
     catch (Exception e)
     {
         expected = e;
     }
     assertNotNull("Expected parse error in " + qString, expected);
 }
Пример #4
0
        private void CheckBadQuery(String qString)
        {
            ComplexPhraseQueryParser qp = new ComplexPhraseQueryParser(TEST_VERSION_CURRENT, defaultFieldName, analyzer);

            qp.InOrder = inOrder;
            Exception expected = null;

            try
            {
                qp.Parse(qString);
            }
            catch (Exception e)
            {
                expected = e;
            }
            assertNotNull("Expected parse error in " + qString, expected);
        }
Пример #5
0
            // Called by ComplexPhraseQueryParser for each phrase after the main
            // parse
            // thread is through
            protected internal void ParsePhraseElements(ComplexPhraseQueryParser qp)
            {
                // TODO ensure that field-sensitivity is preserved ie the query
                // string below is parsed as
                // field+":("+phrasedQueryStringContents+")"
                // but this will need code in rewrite to unwrap the first layer of
                // boolean query

                string oldDefaultParserField = qp.Field;

                try
                {
                    //temporarily set the QueryParser to be parsing the default field for this phrase e.g author:"fred* smith"
                    qp.m_field = this.field;
                    contents   = qp.Parse(phrasedQueryStringContents);
                }
                finally
                {
                    qp.m_field = oldDefaultParserField;
                }
            }
Пример #6
0
            // Called by ComplexPhraseQueryParser for each phrase after the main
            // parse
            // thread is through
            protected internal void ParsePhraseElements(ComplexPhraseQueryParser qp)
            {
                // TODO ensure that field-sensitivity is preserved ie the query
                // string below is parsed as
                // field+":("+phrasedQueryStringContents+")"
                // but this will need code in rewrite to unwrap the first layer of
                // boolean query

                string oldDefaultParserField = qp.Field;
                try
                {
                    //temporarily set the QueryParser to be parsing the default field for this phrase e.g author:"fred* smith"
                    qp.field = this.field;
                    contents = qp.Parse(phrasedQueryStringContents);
                }
                finally
                {
                    qp.field = oldDefaultParserField;
                }
            }
        private void CheckMatches(string qString, string expectedVals)
        {
            ComplexPhraseQueryParser qp = new ComplexPhraseQueryParser(TEST_VERSION_CURRENT, defaultFieldName, analyzer);
            qp.InOrder = inOrder;
            qp.FuzzyPrefixLength = 1; // usually a good idea

            Query q = qp.Parse(qString);

            HashSet<string> expecteds = new HashSet<string>();
            string[] vals = expectedVals.Split(new char[] {','}, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < vals.Length; i++)
            {
                if (vals[i].Length > 0)
                    expecteds.Add(vals[i]);
            }

            TopDocs td = searcher.Search(q, 10);
            ScoreDoc[] sd = td.ScoreDocs;
            for (int i = 0; i < sd.Length; i++)
            {
                Document doc = searcher.Doc(sd[i].Doc);
                string id = doc.Get("id");
                assertTrue(qString + "matched doc#" + id + " not expected", expecteds
                    .Contains(id));
                expecteds.Remove(id);
            }

            assertEquals(qString + " missing some matches ", 0, expecteds.Count);
        }
        public virtual void TestHashcodeEquals()
        {
            ComplexPhraseQueryParser qp = new ComplexPhraseQueryParser(TEST_VERSION_CURRENT, defaultFieldName, analyzer);
            qp.InOrder = true;
            qp.FuzzyPrefixLength = 1;

            String qString = "\"aaa* bbb*\"";

            Query q = qp.Parse(qString);
            Query q2 = qp.Parse(qString);

            assertEquals(q.GetHashCode(), q2.GetHashCode());
            assertEquals(q, q2);

            qp.InOrder = (false); // SOLR-6011

            q2 = qp.Parse(qString);

            // although the general contract of hashCode can't guarantee different values, if we only change one thing
            // about a single query, it normally should result in a different value (and will with the current
            // implementation in ComplexPhraseQuery)
            assertTrue(q.GetHashCode() != q2.GetHashCode());
            assertTrue(!q.equals(q2));
            assertTrue(!q2.equals(q));
        }