1919
2020package org .elasticsearch .index .query ;
2121
22- import org .apache .lucene .search .Query ;
23- import org .elasticsearch .action .support .IndicesOptions ;
24- import org .elasticsearch .cluster .ClusterService ;
25- import org .elasticsearch .cluster .metadata .IndexNameExpressionResolver ;
26- import org .elasticsearch .common .Nullable ;
2722import org .elasticsearch .common .ParseField ;
2823import org .elasticsearch .common .inject .Inject ;
29- import org .elasticsearch .common .lucene .search .Queries ;
30- import org .elasticsearch .common .regex .Regex ;
3124import org .elasticsearch .common .xcontent .XContentParser ;
32- import org .elasticsearch .index .query .support .XContentStructure ;
3325
3426import java .io .IOException ;
3527import java .util .ArrayList ;
3628import java .util .Collection ;
3729
3830/**
31+ * Parser for {@link IndicesQueryBuilder}.
3932 */
40- public class IndicesQueryParser extends BaseQueryParserTemp {
33+ public class IndicesQueryParser extends BaseQueryParser {
4134
4235 private static final ParseField QUERY_FIELD = new ParseField ("query" , "filter" );
4336 private static final ParseField NO_MATCH_QUERY = new ParseField ("no_match_query" , "no_match_filter" );
4437
45- @ Nullable
46- private final ClusterService clusterService ;
47- private final IndexNameExpressionResolver indexNameExpressionResolver ;
48-
4938 @ Inject
50- public IndicesQueryParser (@ Nullable ClusterService clusterService , IndexNameExpressionResolver indexNameExpressionResolver ) {
51- this .clusterService = clusterService ;
52- this .indexNameExpressionResolver = indexNameExpressionResolver ;
39+ public IndicesQueryParser () {
5340 }
5441
5542 @ Override
@@ -58,65 +45,52 @@ public String[] names() {
5845 }
5946
6047 @ Override
61- public Query parse (QueryShardContext context ) throws IOException , QueryParsingException {
62- QueryParseContext parseContext = context .parseContext ();
48+ public QueryBuilder fromXContent (QueryParseContext parseContext ) throws IOException , QueryParsingException {
6349 XContentParser parser = parseContext .parser ();
6450
65- Query noMatchQuery = null ;
66- boolean queryFound = false ;
67- boolean indicesFound = false ;
68- boolean currentIndexMatchesIndices = false ;
51+ QueryBuilder innerQuery = null ;
52+ Collection < String > indices = new ArrayList <>() ;
53+ QueryBuilder noMatchQuery = IndicesQueryBuilder . defaultNoMatchQuery () ;
54+
6955 String queryName = null ;
7056 float boost = AbstractQueryBuilder .DEFAULT_BOOST ;
7157
7258 String currentFieldName = null ;
7359 XContentParser .Token token ;
74- XContentStructure .InnerQuery innerQuery = null ;
75- XContentStructure .InnerQuery innerNoMatchQuery = null ;
7660 while ((token = parser .nextToken ()) != XContentParser .Token .END_OBJECT ) {
7761 if (token == XContentParser .Token .FIELD_NAME ) {
7862 currentFieldName = parser .currentName ();
7963 } else if (token == XContentParser .Token .START_OBJECT ) {
8064 if (parseContext .parseFieldMatcher ().match (currentFieldName , QUERY_FIELD )) {
81- innerQuery = new XContentStructure .InnerQuery (parseContext , null );
82- queryFound = true ;
65+ innerQuery = parseContext .parseInnerQueryBuilder ();
8366 } else if (parseContext .parseFieldMatcher ().match (currentFieldName , NO_MATCH_QUERY )) {
84- innerNoMatchQuery = new XContentStructure . InnerQuery ( parseContext , null );
67+ noMatchQuery = parseContext . parseInnerQueryBuilder ( );
8568 } else {
8669 throw new QueryParsingException (parseContext , "[indices] query does not support [" + currentFieldName + "]" );
8770 }
8871 } else if (token == XContentParser .Token .START_ARRAY ) {
8972 if ("indices" .equals (currentFieldName )) {
90- if (indicesFound ) {
73+ if (indices . isEmpty () == false ) {
9174 throw new QueryParsingException (parseContext , "[indices] indices or index already specified" );
9275 }
93- indicesFound = true ;
94- Collection <String > indices = new ArrayList <>();
9576 while (parser .nextToken () != XContentParser .Token .END_ARRAY ) {
9677 String value = parser .textOrNull ();
9778 if (value == null ) {
9879 throw new QueryParsingException (parseContext , "[indices] no value specified for 'indices' entry" );
9980 }
10081 indices .add (value );
10182 }
102- currentIndexMatchesIndices = matchesIndices (parseContext .index ().name (), indices .toArray (new String [indices .size ()]));
10383 } else {
10484 throw new QueryParsingException (parseContext , "[indices] query does not support [" + currentFieldName + "]" );
10585 }
10686 } else if (token .isValue ()) {
10787 if ("index" .equals (currentFieldName )) {
108- if (indicesFound ) {
88+ if (indices . isEmpty () == false ) {
10989 throw new QueryParsingException (parseContext , "[indices] indices or index already specified" );
11090 }
111- indicesFound = true ;
112- currentIndexMatchesIndices = matchesIndices (parseContext .index ().name (), parser .text ());
91+ indices .add (parser .text ());
11392 } else if (parseContext .parseFieldMatcher ().match (currentFieldName , NO_MATCH_QUERY )) {
114- String type = parser .text ();
115- if ("all" .equals (type )) {
116- noMatchQuery = Queries .newMatchAllQuery ();
117- } else if ("none" .equals (type )) {
118- noMatchQuery = Queries .newMatchNoDocsQuery ();
119- }
93+ noMatchQuery = parseNoMatchQuery (parser .text ());
12094 } else if ("_name" .equals (currentFieldName )) {
12195 queryName = parser .text ();
12296 } else if ("boost" .equals (currentFieldName )) {
@@ -126,44 +100,26 @@ public Query parse(QueryShardContext context) throws IOException, QueryParsingEx
126100 }
127101 }
128102 }
129- if (!queryFound ) {
103+
104+ if (innerQuery == null ) {
130105 throw new QueryParsingException (parseContext , "[indices] requires 'query' element" );
131106 }
132- if (! indicesFound ) {
107+ if (indices . isEmpty () ) {
133108 throw new QueryParsingException (parseContext , "[indices] requires 'indices' or 'index' element" );
134109 }
135-
136- Query chosenQuery ;
137- if (currentIndexMatchesIndices ) {
138- chosenQuery = innerQuery .asQuery ();
139- } else {
140- // If noMatchQuery is set, it means "no_match_query" was "all" or "none"
141- if (noMatchQuery != null ) {
142- chosenQuery = noMatchQuery ;
143- } else {
144- // There might be no "no_match_query" set, so default to the match_all if not set
145- if (innerNoMatchQuery == null ) {
146- chosenQuery = Queries .newMatchAllQuery ();
147- } else {
148- chosenQuery = innerNoMatchQuery .asQuery ();
149- }
150- }
151- }
152- if (queryName != null ) {
153- context .addNamedQuery (queryName , chosenQuery );
154- }
155- chosenQuery .setBoost (boost );
156- return chosenQuery ;
110+ return new IndicesQueryBuilder (innerQuery , indices .toArray (new String [indices .size ()]))
111+ .noMatchQuery (noMatchQuery )
112+ .boost (boost )
113+ .queryName (queryName );
157114 }
158115
159- protected boolean matchesIndices (String currentIndex , String ... indices ) {
160- final String [] concreteIndices = indexNameExpressionResolver .concreteIndices (clusterService .state (), IndicesOptions .lenientExpandOpen (), indices );
161- for (String index : concreteIndices ) {
162- if (Regex .simpleMatch (index , currentIndex )) {
163- return true ;
164- }
116+ static QueryBuilder parseNoMatchQuery (String type ) {
117+ if ("all" .equals (type )) {
118+ return QueryBuilders .matchAllQuery ();
119+ } else if ("none" .equals (type )) {
120+ return new MatchNoneQueryBuilder ();
165121 }
166- return false ;
122+ throw new IllegalArgumentException ( "query type can only be [all] or [none] but not " + "[" + type + "]" ) ;
167123 }
168124
169125 @ Override
0 commit comments