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

[PATCH] Introduction of QueryFactory interface for Query construction

Details

    • Improvement
    • Status: Closed
    • Minor
    • Resolution: Not A Problem
    • None
    • None
    • core/queryparser
    • None
    • Operating System: Linux
      Platform: Other

    • 33449

    Description

      To override the default behaviour of the QueryParser you must currently extend
      it and override the appropriate methods. This seemed to be a little awkward.

      To enable a (hopefully) more flexible means of creating Query instances it
      would seem better to introduce a QueryFactory interface and pass an instance of
      an implementation of this interface to the QueryParser, having the QueryParser
      callback to it to construct the query as it parses it. With this design you
      could write something like:

      QueryParser parser = new QueryParser("defaultField", new StandardAnalyzer(),
      new QueryFactoryImpl());

      Where 'QueryFactoryImpl' is an implementation of the QueryFactory interface.
      If you wanted to add the ability to lower case all of your queries you could
      then write:

      QueryParser parser = new QueryParser("defaultField", new StandardAnalyzer(),
      new LowerCaseQueryFactory(new QueryFactoryImpl()));

      Where 'LowerCaseQueryFactory' is a decorator around another QueryFactory
      instance and it simply lowercases all the terms passed to it before delegating
      the actual query construction. This is a simple example, but more powerful
      functionality could be added not by changing the QueryParser, but by creating a
      new QueryFactory implementation.

      I have a patch for this which will be forthcoming in a moment.

      Attachments

        Issue Links

          Activity

            matt@codemonkeyconsultancy.net Matthew Denner added a comment -

            Created an attachment (id=14212)
            Adds a QueryFactory interface and refactors code to use it

            This patch adds a org.apache.lucene.search.QueryFactory interface and refactors
            the Lucene code to use it. It has 3 implementations included:

            • org.apache.lucene.search.queryfactory.QueryFactoryImpl is an implementation
              created by extracting the QueryParser code for Query construction
            • org.apache.lucene.search.queryfactory.LowerCaseQueryFactory is a decorator
              implementation that will convert all terms to lowercase
            • org.apache.lucene.search.queryfactory.MultiFieldQueryFactory is an
              implementation created by extracting the MultiFieldQueryParser code for Query
              construction

            Note that for some reason that I can't quite fathom at the moment this patch
            seems to be rejected for the SearchTestForDuplicates code. To fix this you'll
            need to manually edit this file and change the construction of the QueryParser
            implementations to take a QueryFactoryImpl instance, i.e.

            new QueryParser(PRIORITY_FIELD, analyzer, new QueryFactoryImpl())

            I have added unit-tests for the QueryFactory implementations as best I could
            and changed one or two tests in the TestQueryParser unit-test because of the
            introduction of the LowerCaseQueryFactory. Because I have added these
            unit-tests they rely on the MockObjects (http://www.mockobjects.com/) JAR file
            (specifically I have used mockobjects-core-0.09.jar) which I have not included
            in the patch but will attach here for download if so required.

            Please also note that some of the API in QueryParser has moved into the
            QueryFactoryImpl class as it seemed to be needed there more than in the
            QueryParser itself, although that is not what I'm necessarily suggesting as a
            change. So you might find your code breaks which I apologise for!

            Hopefully that's everything!

            matt@codemonkeyconsultancy.net Matthew Denner added a comment - Created an attachment (id=14212) Adds a QueryFactory interface and refactors code to use it This patch adds a org.apache.lucene.search.QueryFactory interface and refactors the Lucene code to use it. It has 3 implementations included: org.apache.lucene.search.queryfactory.QueryFactoryImpl is an implementation created by extracting the QueryParser code for Query construction org.apache.lucene.search.queryfactory.LowerCaseQueryFactory is a decorator implementation that will convert all terms to lowercase org.apache.lucene.search.queryfactory.MultiFieldQueryFactory is an implementation created by extracting the MultiFieldQueryParser code for Query construction Note that for some reason that I can't quite fathom at the moment this patch seems to be rejected for the SearchTestForDuplicates code. To fix this you'll need to manually edit this file and change the construction of the QueryParser implementations to take a QueryFactoryImpl instance, i.e. new QueryParser(PRIORITY_FIELD, analyzer, new QueryFactoryImpl()) I have added unit-tests for the QueryFactory implementations as best I could and changed one or two tests in the TestQueryParser unit-test because of the introduction of the LowerCaseQueryFactory. Because I have added these unit-tests they rely on the MockObjects ( http://www.mockobjects.com/ ) JAR file (specifically I have used mockobjects-core-0.09.jar) which I have not included in the patch but will attach here for download if so required. Please also note that some of the API in QueryParser has moved into the QueryFactoryImpl class as it seemed to be needed there more than in the QueryParser itself, although that is not what I'm necessarily suggesting as a change. So you might find your code breaks which I apologise for! Hopefully that's everything!
            shaie Shai Erera added a comment -

            Not sure this is a problem anymore since QP now allows extending and overriding getXYZ() methods for creating specific Query instances.

            shaie Shai Erera added a comment - Not sure this is a problem anymore since QP now allows extending and overriding getXYZ() methods for creating specific Query instances.
            tomoko Tomoko Uchida added a comment -

            This issue was moved to GitHub issue: #1422.

            tomoko Tomoko Uchida added a comment - This issue was moved to GitHub issue: #1422 .

            People

              Unassigned Unassigned
              matt@codemonkeyconsultancy.net Matthew Denner
              Votes:
              3 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: