- 
                Notifications
    
You must be signed in to change notification settings  - Fork 25.6k
 
ESQL: Use a must boolean statement when pushing down to Lucene when scoring is also needed #124001
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
0c107c5
              a6544f8
              c88e288
              24bcede
              46d37ad
              ac2c900
              1940e40
              ee18dee
              File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| 
          
            
          
           | 
    @@ -101,7 +101,13 @@ private static PhysicalPlan rewrite( | |
| if (newPushable.size() > 0) { // update the executable with pushable conditions | ||
| Query queryDSL = TRANSLATOR_HANDLER.asQuery(Predicates.combineAnd(newPushable)); | ||
| QueryBuilder planQuery = queryDSL.asBuilder(); | ||
| var query = Queries.combine(Queries.Clause.FILTER, asList(queryExec.query(), planQuery)); | ||
| Queries.Clause combiningQueryClauseType; | ||
| if (queryExec.hasScoring()) { | ||
                
       | 
||
| combiningQueryClauseType = Queries.Clause.MUST; | ||
| } else { | ||
| combiningQueryClauseType = Queries.Clause.FILTER; | ||
| } | ||
| var query = Queries.combine(combiningQueryClauseType, asList(queryExec.query(), planQuery)); | ||
| queryExec = new EsQueryExec( | ||
| queryExec.source(), | ||
| queryExec.indexPattern(), | ||
| 
          
            
          
           | 
    ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| 
          
            
          
           | 
    @@ -32,27 +32,38 @@ protected PhysicalPlan rule(EsSourceExec plan) { | |
| var docId = new FieldAttribute(plan.source(), EsQueryExec.DOC_ID_FIELD.getName(), EsQueryExec.DOC_ID_FIELD); | ||
| final List<Attribute> attributes = new ArrayList<>(); | ||
| attributes.add(docId); | ||
| if (plan.indexMode() == IndexMode.TIME_SERIES) { | ||
| Attribute tsid = null, timestamp = null; | ||
| for (Attribute attr : plan.output()) { | ||
| String name = attr.name(); | ||
| if (name.equals(MetadataAttribute.TSID_FIELD)) { | ||
| tsid = attr; | ||
| } else if (name.equals(MetadataAttribute.TIMESTAMP_FIELD)) { | ||
| timestamp = attr; | ||
| 
     | 
||
| 
         There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just refactoring to eliminate double iteration over   | 
||
| var outputIterator = plan.output().iterator(); | ||
| var isTimeSeries = plan.indexMode() == IndexMode.TIME_SERIES; | ||
| var keepIterating = true; | ||
| Attribute tsid = null, timestamp = null, score = null; | ||
| 
     | 
||
| while (keepIterating && outputIterator.hasNext()) { | ||
| Attribute attr = outputIterator.next(); | ||
| if (attr instanceof MetadataAttribute ma) { | ||
| if (ma.name().equals(MetadataAttribute.SCORE)) { | ||
| score = attr; | ||
| } else if (isTimeSeries) { | ||
| if (ma.name().equals(MetadataAttribute.TSID_FIELD)) { | ||
| tsid = attr; | ||
| } else if (ma.name().equals(MetadataAttribute.TIMESTAMP_FIELD)) { | ||
| timestamp = attr; | ||
| } | ||
| } | ||
| } | ||
| keepIterating = score == null || (isTimeSeries && (tsid == null || timestamp == null)); | ||
| } | ||
| if (isTimeSeries) { | ||
| if (tsid == null || timestamp == null) { | ||
| throw new IllegalStateException("_tsid or @timestamp are missing from the time-series source"); | ||
| } | ||
| attributes.add(tsid); | ||
| attributes.add(timestamp); | ||
| } | ||
| plan.output().forEach(attr -> { | ||
| if (attr instanceof MetadataAttribute ma && ma.name().equals(MetadataAttribute.SCORE)) { | ||
| attributes.add(ma); | ||
| } | ||
| }); | ||
| if (score != null) { | ||
| attributes.add(score); | ||
| } | ||
| 
     | 
||
| return new EsQueryExec(plan.source(), plan.indexPattern(), plan.indexMode(), plan.indexNameWithModes(), attributes, plan.query()); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| 
          
            
          
           | 
    @@ -21,6 +21,7 @@ | |
| import org.elasticsearch.xpack.esql.core.expression.Attribute; | ||
| import org.elasticsearch.xpack.esql.core.expression.Expression; | ||
| import org.elasticsearch.xpack.esql.core.expression.FieldAttribute; | ||
| import org.elasticsearch.xpack.esql.core.expression.MetadataAttribute; | ||
| import org.elasticsearch.xpack.esql.core.tree.NodeInfo; | ||
| import org.elasticsearch.xpack.esql.core.tree.NodeUtils; | ||
| import org.elasticsearch.xpack.esql.core.tree.Source; | ||
| 
          
            
          
           | 
    @@ -204,6 +205,10 @@ public static boolean isSourceAttribute(Attribute attr) { | |
| return DOC_ID_FIELD.getName().equals(attr.name()); | ||
| } | ||
| 
     | 
||
| public boolean hasScoring() { | ||
| return attrs().stream().anyMatch(a -> a instanceof MetadataAttribute && a.name().equals(MetadataAttribute.SCORE)); | ||
                
       | 
||
| } | ||
| 
     | 
||
| @Override | ||
| protected NodeInfo<EsQueryExec> info() { | ||
| return NodeInfo.create( | ||
| 
          
            
          
           | 
    ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| 
          
            
          
           | 
    @@ -49,7 +49,6 @@ | |
| import org.elasticsearch.xpack.esql.core.expression.Expression; | ||
| import org.elasticsearch.xpack.esql.core.expression.FieldAttribute; | ||
| import org.elasticsearch.xpack.esql.core.expression.FoldContext; | ||
| import org.elasticsearch.xpack.esql.core.expression.MetadataAttribute; | ||
| import org.elasticsearch.xpack.esql.core.type.DataType; | ||
| import org.elasticsearch.xpack.esql.core.type.KeywordEsField; | ||
| import org.elasticsearch.xpack.esql.core.type.MultiTypeEsField; | ||
| 
          
            
          
           | 
    @@ -186,9 +185,7 @@ public final PhysicalOperation sourcePhysicalOperation(EsQueryExec esQueryExec, | |
| assert esQueryExec.estimatedRowSize() != null : "estimated row size not initialized"; | ||
| int rowEstimatedSize = esQueryExec.estimatedRowSize(); | ||
| int limit = esQueryExec.limit() != null ? (Integer) esQueryExec.limit().fold(context.foldCtx()) : NO_LIMIT; | ||
| boolean scoring = esQueryExec.attrs() | ||
| 
         There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Small refactoring.  | 
||
| .stream() | ||
| .anyMatch(a -> a instanceof MetadataAttribute && a.name().equals(MetadataAttribute.SCORE)); | ||
| boolean scoring = esQueryExec.hasScoring(); | ||
| if ((sorts != null && sorts.isEmpty() == false)) { | ||
| List<SortBuilder<?>> sortBuilders = new ArrayList<>(sorts.size()); | ||
| for (Sort sort : sorts) { | ||
| 
          
            
          
           | 
    ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the actual fix for the bug. Need to add tests.