Lucene - Core
  1. Lucene - Core
  2. LUCENE-1245

MultiFieldQueryParser is not friendly for overriding getFieldQuery(String,String,int)

    Details

    • Type: Improvement Improvement
    • Status: Closed
    • Priority: Major Major
    • Resolution: Not A Problem
    • Affects Version/s: 2.3.2
    • Fix Version/s: None
    • Component/s: core/queryparser
    • Labels:
      None
    • Lucene Fields:
      New, Patch Available

      Description

      LUCENE-1213 fixed an issue in MultiFieldQueryParser where the slop parameter wasn't being properly applied. Problem is, the fix which eventually got committed is calling super.getFieldQuery(String,String), bypassing any possibility of customising the query behaviour.

      This should be relatively simply fixable by modifying getFieldQuery(String,String,int) to, if field is null, recursively call getFieldQuery(String,String,int) instead of setting the slop itself. This gives subclasses which override either getFieldQuery method a chance to do something different.

        Activity

        Hide
        Trejkaz added a comment -

        (Updating title to be more specific about what wasn't friendly.)

        Show
        Trejkaz added a comment - (Updating title to be more specific about what wasn't friendly.)
        Hide
        Trejkaz added a comment -

        Fix makes getFieldQuery(String,String) and getFieldQuery(String,String,int) work more or less the same. Neither calls methods on super and thus overriding the methods will work (and does. Although I have no unit test for this yet.)

        Common boosting logic is extracted to an applyBoost method. Also the check for the clauses being empty, I have removed... as getBooleanQuery appears to be doing that already.

        Show
        Trejkaz added a comment - Fix makes getFieldQuery(String,String) and getFieldQuery(String,String,int) work more or less the same. Neither calls methods on super and thus overriding the methods will work (and does. Although I have no unit test for this yet.) Common boosting logic is extracted to an applyBoost method. Also the check for the clauses being empty, I have removed... as getBooleanQuery appears to be doing that already.
        Hide
        Trejkaz added a comment - - edited

        Here's an example illustrating the way we were using it, although instead of changing the query text we're actually returning a different query class – that class isn't in Lucene Core and also it's easier to build up an expected query if it's just a TermQuery.

            public void testOverrideGetFieldQuery() throws Exception {
                String[] fields = { "a", "b" };
                QueryParser parser = new MultiFieldQueryParser(fields, new StandardAnalyzer()) {
                    protected Query getFieldQuery(String field, String queryText, int slop) throws ParseException {
                        if (field != null && slop == 1) {
                            queryText = "z" + queryText;
                        }
                        return super.getFieldQuery(field, queryText, slop);
                    }
                };
        
                BooleanQuery expected = new BooleanQuery();
                expected.add(new TermQuery(new Term("a", "zabc")), BooleanClause.Occur.SHOULD);
                expected.add(new TermQuery(new Term("b", "zabc")), BooleanClause.Occur.SHOULD);
                assertEquals("Expected a mangled query", expected, parser.parse("\"abc\"~1"));
            }
        
        Show
        Trejkaz added a comment - - edited Here's an example illustrating the way we were using it, although instead of changing the query text we're actually returning a different query class – that class isn't in Lucene Core and also it's easier to build up an expected query if it's just a TermQuery. public void testOverrideGetFieldQuery() throws Exception { String[] fields = { "a", "b" }; QueryParser parser = new MultiFieldQueryParser(fields, new StandardAnalyzer()) { protected Query getFieldQuery(String field, String queryText, int slop) throws ParseException { if (field != null && slop == 1) { queryText = "z" + queryText; } return super.getFieldQuery(field, queryText, slop); } }; BooleanQuery expected = new BooleanQuery(); expected.add(new TermQuery(new Term("a", "zabc")), BooleanClause.Occur.SHOULD); expected.add(new TermQuery(new Term("b", "zabc")), BooleanClause.Occur.SHOULD); assertEquals("Expected a mangled query", expected, parser.parse("\"abc\"~1")); }
        Hide
        Jan Høydahl added a comment -

        This issue has been inactive for more than 4 years. Please close if it's no longer relevant/needed, or bring it up to date if you intend to work on it. SPRING_CLEANING_2013

        Show
        Jan Høydahl added a comment - This issue has been inactive for more than 4 years. Please close if it's no longer relevant/needed, or bring it up to date if you intend to work on it. SPRING_CLEANING_2013
        Hide
        Trejkaz added a comment -

        I guess this resolution is correct. We have stopped using the basic query parser and now use the flexible one precisely because of this sort of issue, so the issue itself is no longer an issue for us.

        Show
        Trejkaz added a comment - I guess this resolution is correct. We have stopped using the basic query parser and now use the flexible one precisely because of this sort of issue, so the issue itself is no longer an issue for us.

          People

          • Assignee:
            Unassigned
            Reporter:
            Trejkaz
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development