- 
                Notifications
    You must be signed in to change notification settings 
- Fork 25.6k
Group field caps shard requests per node #77047
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
          
     Merged
      
      
            jtibshirani
  merged 10 commits into
  elastic:group-field-caps
from
jtibshirani:field-caps
  
      
      
   
  Sep 27, 2021 
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            10 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      97522d9
              
                Pull out shard operation into its own class
              
              
                jtibshirani 999b7a5
              
                Add request + response classes, plus tests
              
              
                jtibshirani 48cb0af
              
                Group index requests into node requests
              
              
                jtibshirani ae9db42
              
                Refactor collection to handle multiple responses per index
              
              
                jtibshirani 2ce1d68
              
                Also support grouping by node with index_filter
              
              
                jtibshirani 8b6b977
              
                Fix test failures
              
              
                jtibshirani d681ae4
              
                Merge remote-tracking branch 'upstream/7.x' into field-caps
              
              
                jtibshirani 5d95c5c
              
                Return error when node is no longer available
              
              
                jtibshirani 4efd602
              
                Skip over shard if we already found a match for that index
              
              
                jtibshirani d34f016
              
                Merge remote-tracking branch 'upstream/7.x' into field-caps
              
              
                jtibshirani File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
        
          
          
            117 changes: 117 additions & 0 deletions
          
          117 
        
  server/src/main/java/org/elasticsearch/action/fieldcaps/FieldCapabilitiesFetcher.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
| * in compliance with, at your election, the Elastic License 2.0 or the Server | ||
| * Side Public License, v 1. | ||
| */ | ||
|  | ||
| package org.elasticsearch.action.fieldcaps; | ||
|  | ||
| import org.elasticsearch.index.IndexService; | ||
| import org.elasticsearch.index.engine.Engine; | ||
| import org.elasticsearch.index.mapper.MappedFieldType; | ||
| import org.elasticsearch.index.mapper.ObjectMapper; | ||
| import org.elasticsearch.index.mapper.RuntimeField; | ||
| import org.elasticsearch.index.query.MatchAllQueryBuilder; | ||
| import org.elasticsearch.index.query.SearchExecutionContext; | ||
| import org.elasticsearch.index.shard.IndexShard; | ||
| import org.elasticsearch.index.shard.ShardId; | ||
| import org.elasticsearch.indices.IndicesService; | ||
| import org.elasticsearch.search.SearchService; | ||
| import org.elasticsearch.search.builder.SearchSourceBuilder; | ||
| import org.elasticsearch.search.internal.AliasFilter; | ||
| import org.elasticsearch.search.internal.ShardSearchRequest; | ||
|  | ||
| import java.io.IOException; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.function.Predicate; | ||
|  | ||
| /** | ||
| * Loads the mappings for an index and computes all {@link IndexFieldCapabilities}. This | ||
| * helper class performs the core shard operation for the field capabilities action. | ||
| */ | ||
| class FieldCapabilitiesFetcher { | ||
| private final IndicesService indicesService; | ||
|  | ||
| FieldCapabilitiesFetcher(IndicesService indicesService) { | ||
| this.indicesService = indicesService; | ||
| } | ||
|  | ||
| public FieldCapabilitiesIndexResponse fetch(final FieldCapabilitiesIndexRequest request) throws IOException { | ||
| final ShardId shardId = request.shardId(); | ||
| final IndexService indexService = indicesService.indexServiceSafe(shardId.getIndex()); | ||
| final IndexShard indexShard = indexService.getShard(request.shardId().getId()); | ||
| try (Engine.Searcher searcher = indexShard.acquireSearcher(Engine.CAN_MATCH_SEARCH_SOURCE)) { | ||
|  | ||
| final SearchExecutionContext searchExecutionContext = indexService.newSearchExecutionContext(shardId.id(), 0, | ||
| searcher, request::nowInMillis, null, request.runtimeFields()); | ||
|  | ||
| if (canMatchShard(request, searchExecutionContext) == false) { | ||
| return new FieldCapabilitiesIndexResponse(request.index(), Collections.emptyMap(), false); | ||
| } | ||
|  | ||
| Set<String> fieldNames = new HashSet<>(); | ||
| for (String pattern : request.fields()) { | ||
| fieldNames.addAll(searchExecutionContext.getMatchingFieldNames(pattern)); | ||
| } | ||
|  | ||
| Predicate<String> fieldPredicate = indicesService.getFieldFilter().apply(shardId.getIndexName()); | ||
| Map<String, IndexFieldCapabilities> responseMap = new HashMap<>(); | ||
| for (String field : fieldNames) { | ||
| MappedFieldType ft = searchExecutionContext.getFieldType(field); | ||
| boolean isMetadataField = searchExecutionContext.isMetadataField(field); | ||
| if (isMetadataField || fieldPredicate.test(ft.name())) { | ||
| IndexFieldCapabilities fieldCap = new IndexFieldCapabilities(field, | ||
| ft.familyTypeName(), isMetadataField, ft.isSearchable(), ft.isAggregatable(), ft.meta()); | ||
| responseMap.put(field, fieldCap); | ||
| } else { | ||
| continue; | ||
| } | ||
|  | ||
| // Check the ancestor of the field to find nested and object fields. | ||
| // Runtime fields are excluded since they can override any path. | ||
| //TODO find a way to do this that does not require an instanceof check | ||
| if (ft instanceof RuntimeField == false) { | ||
| int dotIndex = ft.name().lastIndexOf('.'); | ||
| while (dotIndex > -1) { | ||
| String parentField = ft.name().substring(0, dotIndex); | ||
| if (responseMap.containsKey(parentField)) { | ||
| // we added this path on another field already | ||
| break; | ||
| } | ||
| // checks if the parent field contains sub-fields | ||
| if (searchExecutionContext.getFieldType(parentField) == null) { | ||
| // no field type, it must be an object field | ||
| ObjectMapper mapper = searchExecutionContext.getObjectMapper(parentField); | ||
| // Composite runtime fields do not have a mapped type for the root - check for null | ||
| if (mapper != null) { | ||
| String type = mapper.isNested() ? "nested" : "object"; | ||
| IndexFieldCapabilities fieldCap = new IndexFieldCapabilities(parentField, type, | ||
| false, false, false, Collections.emptyMap()); | ||
| responseMap.put(parentField, fieldCap); | ||
| } | ||
| } | ||
| dotIndex = parentField.lastIndexOf('.'); | ||
| } | ||
| } | ||
| } | ||
| return new FieldCapabilitiesIndexResponse(request.index(), responseMap, true); | ||
| } | ||
| } | ||
|  | ||
| private boolean canMatchShard(FieldCapabilitiesIndexRequest req, SearchExecutionContext searchExecutionContext) throws IOException { | ||
| if (req.indexFilter() == null || req.indexFilter() instanceof MatchAllQueryBuilder) { | ||
| return true; | ||
| } | ||
| assert req.nowInMillis() != 0L; | ||
| ShardSearchRequest searchRequest = new ShardSearchRequest(req.shardId(), null, req.nowInMillis(), AliasFilter.EMPTY); | ||
| searchRequest.source(new SearchSourceBuilder().query(req.indexFilter())); | ||
| return SearchService.queryStillMatchesAfterRewrite(searchRequest, searchExecutionContext); | ||
| } | ||
|  | ||
| } | ||
        
          
          
            126 changes: 126 additions & 0 deletions
          
          126 
        
  server/src/main/java/org/elasticsearch/action/fieldcaps/FieldCapabilitiesNodeRequest.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
| * in compliance with, at your election, the Elastic License 2.0 or the Server | ||
| * Side Public License, v 1. | ||
| */ | ||
|  | ||
| package org.elasticsearch.action.fieldcaps; | ||
|  | ||
| import org.elasticsearch.action.ActionRequest; | ||
| import org.elasticsearch.action.ActionRequestValidationException; | ||
| import org.elasticsearch.action.IndicesRequest; | ||
| import org.elasticsearch.action.OriginalIndices; | ||
| import org.elasticsearch.action.support.IndicesOptions; | ||
| import org.elasticsearch.common.io.stream.StreamInput; | ||
| import org.elasticsearch.common.io.stream.StreamOutput; | ||
| import org.elasticsearch.index.query.QueryBuilder; | ||
| import org.elasticsearch.index.shard.ShardId; | ||
|  | ||
| import java.io.IOException; | ||
| import java.util.Arrays; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
|  | ||
| class FieldCapabilitiesNodeRequest extends ActionRequest implements IndicesRequest { | ||
|  | ||
| private final ShardId[] shardIds; | ||
| private final String[] fields; | ||
| private final OriginalIndices originalIndices; | ||
| private final QueryBuilder indexFilter; | ||
| private final long nowInMillis; | ||
| private final Map<String, Object> runtimeFields; | ||
|  | ||
| FieldCapabilitiesNodeRequest(StreamInput in) throws IOException { | ||
| super(in); | ||
| shardIds = in.readArray(ShardId::new, ShardId[]::new); | ||
| fields = in.readStringArray(); | ||
| originalIndices = OriginalIndices.readOriginalIndices(in); | ||
| indexFilter = in.readOptionalNamedWriteable(QueryBuilder.class); | ||
| nowInMillis = in.readLong(); | ||
| runtimeFields = in.readMap(); | ||
| } | ||
|  | ||
| FieldCapabilitiesNodeRequest(ShardId[] shardIds, | ||
| String[] fields, | ||
| OriginalIndices originalIndices, | ||
| QueryBuilder indexFilter, | ||
| long nowInMillis, | ||
| Map<String, Object> runtimeFields) { | ||
| this.fields = fields; | ||
| this.shardIds = shardIds; | ||
| this.originalIndices = originalIndices; | ||
| this.indexFilter = indexFilter; | ||
| this.nowInMillis = nowInMillis; | ||
| this.runtimeFields = runtimeFields; | ||
| } | ||
|  | ||
| public String[] fields() { | ||
| return fields; | ||
| } | ||
|  | ||
| public OriginalIndices originalIndices() { | ||
| return originalIndices; | ||
| } | ||
|  | ||
| @Override | ||
| public String[] indices() { | ||
| return originalIndices.indices(); | ||
| } | ||
|  | ||
| @Override | ||
| public IndicesOptions indicesOptions() { | ||
| return originalIndices.indicesOptions(); | ||
| } | ||
|  | ||
| public QueryBuilder indexFilter() { | ||
| return indexFilter; | ||
| } | ||
|  | ||
| public Map<String, Object> runtimeFields() { | ||
| return runtimeFields; | ||
| } | ||
|  | ||
| public ShardId[] shardIds() { | ||
| return shardIds; | ||
| } | ||
|  | ||
| public long nowInMillis() { | ||
| return nowInMillis; | ||
| } | ||
|  | ||
| @Override | ||
| public void writeTo(StreamOutput out) throws IOException { | ||
| super.writeTo(out); | ||
| out.writeArray(shardIds); | ||
| out.writeStringArray(fields); | ||
| OriginalIndices.writeOriginalIndices(originalIndices, out); | ||
| out.writeOptionalNamedWriteable(indexFilter); | ||
| out.writeLong(nowInMillis); | ||
| out.writeMap(runtimeFields); | ||
| } | ||
|  | ||
| @Override | ||
| public ActionRequestValidationException validate() { | ||
| return null; | ||
| } | ||
|  | ||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
| FieldCapabilitiesNodeRequest that = (FieldCapabilitiesNodeRequest) o; | ||
| return nowInMillis == that.nowInMillis && Arrays.equals(shardIds, that.shardIds) | ||
| && Arrays.equals(fields, that.fields) && Objects.equals(originalIndices, that.originalIndices) | ||
| && Objects.equals(indexFilter, that.indexFilter) && Objects.equals(runtimeFields, that.runtimeFields); | ||
| } | ||
|  | ||
| @Override | ||
| public int hashCode() { | ||
| int result = Objects.hash(originalIndices, indexFilter, nowInMillis, runtimeFields); | ||
| result = 31 * result + Arrays.hashCode(shardIds); | ||
| result = 31 * result + Arrays.hashCode(fields); | ||
| return result; | ||
| } | ||
| } | 
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
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 class lets us use the same per-shard logic for both the new and old execution strategies. It's not completely necessary to add it -- I could have shuffled some inner classes around to let us share this logic. However I found this to be a nice abstraction. It helps breaks up
TransportFieldCapabilitiesAction, which is complex, and opens the door to adding unit tests for field caps (which I hope to in a follow-up).