Uploaded image for project: 'Lucene - Core'
  1. Lucene - Core
  2. LUCENE-5205

SpanQueryParser with recursion, analysis and syntax very similar to classic QueryParser

    Details

    • Type: Improvement
    • Status: Resolved
    • Priority: Major
    • Resolution: Later
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: core/queryparser
    • Labels:

      Description

      This parser extends QueryParserBase and includes functionality from:

      • Classic QueryParser: most of its syntax
      • SurroundQueryParser: recursive parsing for "near" and "not" clauses.
      • ComplexPhraseQueryParser: can handle "near" queries that include multiterms (wildcard, fuzzy, regex, prefix),
      • AnalyzingQueryParser: has an option to analyze multiterms.

      At a high level, there's a first pass BooleanQuery/field parser and then a span query parser handles all terminal nodes and phrases.

      Same as classic syntax:

      • term: test
      • fuzzy: roam~0.8, roam~2
      • wildcard: te?t, test*, t*st
      • regex: /[mb]oat/
      • phrase: "jakarta apache"
      • phrase with slop: "jakarta apache"~3
      • default "or" clause: jakarta apache
      • grouping "or" clause: (jakarta apache)
      • boolean and +/-: (lucene OR apache) NOT jakarta; +lucene +apache -jakarta
      • multiple fields: title:lucene author:hatcher

      Main additions in SpanQueryParser syntax vs. classic syntax:

      • Can require "in order" for phrases with slop with the ~> operator: "jakarta apache"~>3
      • Can specify "not near": "fever bieber"!~3,10 ::
        find "fever" but not if "bieber" appears within 3 words before or 10 words after it.
      • Fully recursive phrasal queries with [ and ]; as in: [[jakarta apache]~3 lucene]~>4 ::
        find "jakarta" within 3 words of "apache", and that hit has to be within four words before "lucene"
      • Can also use [] for single level phrasal queries instead of " as in: [jakarta apache]
      • Can use "or grouping" clauses in phrasal queries: "apache (lucene solr)"~3 :: find "apache" and then either "lucene" or "solr" within three words.
      • Can use multiterms in phrasal queries: "jakarta~1 ap*che"~2
      • Did I mention full recursion: [[jakarta~1 ap*che]~2 (solr~ /l[ou]+[cs][en]+/)]~10 :: Find something like "jakarta" within two words of "ap*che" and that hit has to be within ten words of something like "solr" or that "lucene" regex.
      • Can require at least x number of hits at boolean level: "apache AND (lucene solr tika)~2
      • Can use negative only query: -jakarta :: Find all docs that don't contain "jakarta"
      • Can use an edit distance > 2 for fuzzy query via SlowFuzzyQuery (beware of potential performance issues!).

      Trivial additions:

      • Can specify prefix length in fuzzy queries: jakarta~1,2 (edit distance =1, prefix =2)
      • Can specifiy Optimal String Alignment (OSA) vs Levenshtein for distance <=2: (jakarta~1 (OSA) vs jakarta~>1(Levenshtein)

      This parser can be very useful for concordance tasks (see also LUCENE-5317 and LUCENE-5318) and for analytical search.

      Until LUCENE-2878 is closed, this might have a use for fans of SpanQuery.

      Most of the documentation is in the javadoc for SpanQueryParser.

      Any and all feedback is welcome. Thank you.

      Until this is added to the Lucene project, I've added a standalone lucene-addons repo (with jars compiled for the latest stable build of Lucene) on github.

        Attachments

        1. SpanQueryParser_v1.patch.gz
          20 kB
          Tim Allison
        2. patch.txt
          114 kB
          Tim Allison
        3. LUCENE_5205.patch
          260 kB
          Tim Allison
        4. LUCENE-5205.patch.gz
          52 kB
          Tim Allison
        5. LUCENE-5205.patch.gz
          53 kB
          Tim Allison
        6. LUCENE-5205_smallTestMods.patch
          3 kB
          Tim Allison
        7. LUCENE-5205-date-pkg-prvt.patch
          4 kB
          Tim Allison
        8. LUCENE-5205_dateTestReInitPkgPrvt.patch
          10 kB
          Tim Allison
        9. LUCENE-5205-cleanup-tests.patch
          122 kB
          Tim Allison
        10. LUCENE-5205_improve_stop_word_handling.patch
          19 kB
          Tim Allison

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                tallison@apache.org Tim Allison
              • Votes:
                11 Vote for this issue
                Watchers:
                26 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: