Java Code Examples for org.elasticsearch.index.query.QueryParseContext#parser()

The following examples show how to use org.elasticsearch.index.query.QueryParseContext#parser() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InnerHitsQueryParserHelper.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public InnerHitsSubSearchContext parse(QueryParseContext parserContext) throws IOException, QueryParsingException {
    String fieldName = null;
    XContentParser.Token token;
    String innerHitName = null;
    SubSearchContext subSearchContext = new SubSearchContext(SearchContext.current());
    try {
        XContentParser parser = parserContext.parser();
        while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
            if (token == XContentParser.Token.FIELD_NAME) {
                fieldName = parser.currentName();
            } else if (token.isValue()) {
                if ("name".equals(fieldName)) {
                    innerHitName = parser.textOrNull();
                } else {
                    parseCommonInnerHitOptions(parser, token, fieldName, subSearchContext, sortParseElement, sourceParseElement, highlighterParseElement, scriptFieldsParseElement, fieldDataFieldsParseElement);
                }
            } else {
                parseCommonInnerHitOptions(parser, token, fieldName, subSearchContext, sortParseElement, sourceParseElement, highlighterParseElement, scriptFieldsParseElement, fieldDataFieldsParseElement);
            }
        }
    } catch (Exception e) {
        throw new QueryParsingException(parserContext, "Failed to parse [_inner_hits]", e);
    }
    return new InnerHitsSubSearchContext(innerHitName, subSearchContext);
}
 
Example 2
Source File: MultiValuesSourceParser.java    From elasticsearch-linear-regression with Apache License 2.0 4 votes vote down vote up
@Override
public final MultiValuesSourceAggregationBuilder<VS, ?> parse(String aggregationName,
    QueryParseContext context)
    throws IOException {

  XContentParser parser = context.parser();
  List<String> fields = null;
  ValueType valueType = null;
  String format = null;
  Map<String, Object> missingMap = null;
  Map<ParseField, Object> otherOptions = new HashMap<>();
  XContentParser.Token token;
  String currentFieldName = null;
  while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
    if (token == XContentParser.Token.FIELD_NAME) {
      currentFieldName = parser.currentName();
    } else if (token == XContentParser.Token.VALUE_STRING) {
      if (CommonFields.FIELDS.match(currentFieldName)) {
        fields = Collections.singletonList(parser.text());
      } else if (formattable && CommonFields.FORMAT.match(currentFieldName)) {
        format = parser.text();
      } else if (CommonFields.VALUE_TYPE.match(currentFieldName)) {
        throw new ParsingException(parser.getTokenLocation(),
            "Unexpected token " + token + " [" + currentFieldName + "] in [" + aggregationName
                + "]. " +
                "Multi-field aggregations do not support scripts.");
      } else if (!token(aggregationName, currentFieldName, token, parser, otherOptions)) {
        throw new ParsingException(parser.getTokenLocation(),
            "Unexpected token " + token + " [" + currentFieldName + "] in [" + aggregationName
                + "].");
      }
    } else if (token == XContentParser.Token.START_OBJECT) {
      if (CommonFields.MISSING.match(currentFieldName)) {
        missingMap = new HashMap<>();
        while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
          parseMissingAndAdd(aggregationName, currentFieldName, parser, missingMap);
        }
      } else if (Script.SCRIPT_PARSE_FIELD.match(currentFieldName)) {
        throw new ParsingException(parser.getTokenLocation(),
            "Unexpected token " + token + " [" + currentFieldName + "] in [" + aggregationName
                + "]. " +
                "Multi-field aggregations do not support scripts.");

      } else if (!token(aggregationName, currentFieldName, token, parser, otherOptions)) {
        throw new ParsingException(parser.getTokenLocation(),
            "Unexpected token " + token + " [" + currentFieldName + "] in [" + aggregationName
                + "].");
      }
    } else if (token == XContentParser.Token.START_ARRAY) {
      if (Script.SCRIPT_PARSE_FIELD.match(currentFieldName)) {
        throw new ParsingException(parser.getTokenLocation(),
            "Unexpected token " + token + " [" + currentFieldName + "] in [" + aggregationName
                + "]. " +
                "Multi-field aggregations do not support scripts.");
      } else if (CommonFields.FIELDS.match(currentFieldName)) {
        fields = new ArrayList<>();
        while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
          if (token == XContentParser.Token.VALUE_STRING) {
            fields.add(parser.text());
          } else {
            throw new ParsingException(parser.getTokenLocation(),
                "Unexpected token " + token + " [" + currentFieldName + "] in [" + aggregationName
                    + "].");
          }
        }
      } else if (!token(aggregationName, currentFieldName, token, parser, otherOptions)) {
        throw new ParsingException(parser.getTokenLocation(),
            "Unexpected token " + token + " [" + currentFieldName + "] in [" + aggregationName
                + "].");
      }
    } else if (!token(aggregationName, currentFieldName, token, parser, otherOptions)) {
      throw new ParsingException(parser.getTokenLocation(),
          "Unexpected token " + token + " [" + currentFieldName + "] in [" + aggregationName
              + "].");
    }
  }

  MultiValuesSourceAggregationBuilder<VS, ?> factory = createFactory(aggregationName,
      this.valuesSourceType, this.targetValueType,
      otherOptions);
  if (fields != null) {
    factory.fields(fields);
  }
  if (valueType != null) {
    factory.valueType(valueType);
  }
  if (format != null) {
    factory.format(format);
  }
  if (missingMap != null) {
    factory.missingMap(missingMap);
  }
  return factory;
}
 
Example 3
Source File: FieldDataTermsQueryParser.java    From siren-join with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
  XContentParser parser = parseContext.parser();

  XContentParser.Token token = parser.nextToken();
  if (token != XContentParser.Token.FIELD_NAME) {
      throw new QueryParsingException(parseContext, "[fielddata_terms] a field name is required");
  }
  String fieldName = parser.currentName();

  String queryName = null;
  byte[] value = null;
  Long cacheKey = null;

  token = parser.nextToken();
  if (token == XContentParser.Token.START_OBJECT) {
    String currentFieldName = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
      if (token == XContentParser.Token.FIELD_NAME) {
        currentFieldName = parser.currentName();
      } else {
        if ("value".equals(currentFieldName)) {
          value = parser.binaryValue();
        } else if ("_name".equals(currentFieldName)) {
          queryName = parser.text();
        } else if ("_cache_key".equals(currentFieldName) || "_cacheKey".equals(currentFieldName)) {
          cacheKey = parser.longValue();
        } else {
          throw new QueryParsingException(parseContext, "[fielddata_terms] filter does not support [" + currentFieldName + "]");
        }
      }
    }
    parser.nextToken();
  } else {
    value = parser.binaryValue();
    // move to the next token
    parser.nextToken();
  }

  if (value == null) {
    throw new QueryParsingException(parseContext, "[fielddata_terms] a binary value is required");
  }
  if (cacheKey == null) { // cache key is mandatory - see #170
    throw new QueryParsingException(parseContext, "[fielddata_terms] a cache key is required");
  }

  if (fieldName == null) {
    throw new QueryParsingException(parseContext, "[fielddata_terms] a field name is required");
  }

  MappedFieldType fieldType = parseContext.fieldMapper(fieldName);
  if (fieldType == null) {
    return new MatchNoDocsQuery();
  }

  IndexFieldData fieldData = parseContext.getForField(fieldType);
  Query query = this.toFieldDataTermsQuery(fieldType, fieldData, value, cacheKey);

  if (queryName != null) {
    parseContext.addNamedQuery(queryName, query);
  }

  return query;
}
 
Example 4
Source File: TermsEnumTermsQueryParser.java    From siren-join with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
  XContentParser parser = parseContext.parser();

  XContentParser.Token token = parser.nextToken();
  if (token != XContentParser.Token.FIELD_NAME) {
      throw new QueryParsingException(parseContext, "[termsenum_terms] a field name is required");
  }
  String fieldName = parser.currentName();

  String queryName = null;
  byte[] value = null;
  Long cacheKey = null;

  token = parser.nextToken();
  if (token == XContentParser.Token.START_OBJECT) {
    String currentFieldName = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
      if (token == XContentParser.Token.FIELD_NAME) {
        currentFieldName = parser.currentName();
      } else {
        if ("value".equals(currentFieldName)) {
            value = parser.binaryValue();
        } else if ("_name".equals(currentFieldName)) {
            queryName = parser.text();
        } else if ("_cache_key".equals(currentFieldName) || "_cacheKey".equals(currentFieldName)) {
            cacheKey = parser.longValue();
        } else {
          throw new QueryParsingException(parseContext, "[termsenum_terms] filter does not support [" + currentFieldName + "]");
        }
      }
    }
    parser.nextToken();
  } else {
    value = parser.binaryValue();
    // move to the next token
    parser.nextToken();
  }

  if (value == null) {
    throw new QueryParsingException(parseContext, "[termsenum_terms] a binary value is required");
  }
  if (cacheKey == null) { // cache key is mandatory - see #170
    throw new QueryParsingException(parseContext, "[termsenum_terms] a cache key is required");
  }

  if (fieldName == null) {
    throw new QueryParsingException(parseContext, "[termsenum_terms] a field name is required");
  }

  MappedFieldType fieldType = parseContext.fieldMapper(fieldName);
  if (fieldType == null) {
    return new MatchNoDocsQuery();
  }

  Query query = new TermsEnumTermsQuery(value, fieldName, cacheKey);

  if (queryName != null) {
    parseContext.addNamedQuery(queryName, query);
  }

  return query;
}
 
Example 5
Source File: PhraseCountQueryBuilder.java    From pyramid with Apache License 2.0 4 votes vote down vote up
public static Optional<PhraseCountQueryBuilder> fromXContent(QueryParseContext parseContext) throws IOException {
    XContentParser parser = parseContext.parser();
    String fieldName = null;
    Object value = null;
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    String analyzer = null;
    int slop = MatchQuery.DEFAULT_PHRASE_SLOP;
    boolean inOrder = false;
    boolean weightedCount = false;
    String queryName = null;
    String currentFieldName = null;
    XContentParser.Token token;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (parseContext.isDeprecatedSetting(currentFieldName)) {
            // skip
        } else if (token == XContentParser.Token.START_OBJECT) {
            throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, currentFieldName);
            fieldName = currentFieldName;
            while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                if (token == XContentParser.Token.FIELD_NAME) {
                    currentFieldName = parser.currentName();
                } else if (token.isValue()) {
                    if (PhraseCountQueryBuilder.QUERY_FIELD.match(currentFieldName)) {
                        value = parser.objectText();
                    } else if (ANALYZER_FIELD.match(currentFieldName)) {
                        analyzer = parser.text();
                    } else if(IN_ORDER_FIELD.match(currentFieldName)) {
                        inOrder = parser.booleanValue();
                    } else if (WEIGHTED_COUNT_FIELD.match(currentFieldName)) {
                        weightedCount = parser.booleanValue();
                    } else if (BOOST_FIELD.match(currentFieldName)) {
                        boost = parser.floatValue();
                    } else if (SLOP_FIELD.match(currentFieldName)) {
                        slop = parser.intValue();
                    } else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName)) {
                        queryName = parser.text();
                    } else {
                        throw new ParsingException(parser.getTokenLocation(),
                            "[" + NAME + "] query does not support [" + currentFieldName + "]");
                    }
                } else {
                    throw new ParsingException(parser.getTokenLocation(),
                        "[" + NAME + "] unknown token [" + token + "] after [" + currentFieldName + "]");
                }
            }
        } else {
            throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, parser.currentName());
            fieldName = parser.currentName();
            value = parser.objectText();
        }
    }

    PhraseCountQueryBuilder phraseCountQuery = new PhraseCountQueryBuilder(fieldName, value);
    phraseCountQuery.analyzer(analyzer);
    phraseCountQuery.slop(slop);
    phraseCountQuery.inOrder(inOrder);
    phraseCountQuery.weightedCount(weightedCount);
    phraseCountQuery.queryName(queryName);
    phraseCountQuery.boost(boost);
    return Optional.of(phraseCountQuery);
}