Java Code Examples for org.elasticsearch.common.xcontent.XContentParser#binaryValue()

The following examples show how to use org.elasticsearch.common.xcontent.XContentParser#binaryValue() . 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: WrapperQueryParser.java    From Elasticsearch with Apache License 2.0 6 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, "[wrapper] query malformed");
    }
    String fieldName = parser.currentName();
    if (!fieldName.equals("query")) {
        throw new QueryParsingException(parseContext, "[wrapper] query malformed");
    }
    parser.nextToken();

    byte[] querySource = parser.binaryValue();
    try (XContentParser qSourceParser = XContentFactory.xContent(querySource).createParser(querySource)) {
        final QueryParseContext context = new QueryParseContext(parseContext.index(), parseContext.indexQueryParserService());
        context.reset(qSourceParser);
        Query result = context.parseInnerQuery();
        parser.nextToken();
        parseContext.combineNamedQueries(context);
        return result;
    }
}
 
Example 2
Source File: AbstractXContentParser.java    From crate with Apache License 2.0 6 votes vote down vote up
static Object readValue(XContentParser parser, MapFactory mapFactory, XContentParser.Token token) throws IOException {
    if (token == XContentParser.Token.VALUE_NULL) {
        return null;
    } else if (token == XContentParser.Token.VALUE_STRING) {
        return parser.text();
    } else if (token == XContentParser.Token.VALUE_NUMBER) {
        return parser.numberValue();
    } else if (token == XContentParser.Token.VALUE_BOOLEAN) {
        return parser.booleanValue();
    } else if (token == XContentParser.Token.START_OBJECT) {
        return readMap(parser, mapFactory);
    } else if (token == XContentParser.Token.START_ARRAY) {
        return readList(parser, mapFactory);
    } else if (token == XContentParser.Token.VALUE_EMBEDDED_OBJECT) {
        return parser.binaryValue();
    }
    return null;
}
 
Example 3
Source File: AggregationBinaryParseElement.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void parse(XContentParser parser, SearchContext context) throws Exception {
    byte[] aggSource = parser.binaryValue();
    try (XContentParser aSourceParser = XContentFactory.xContent(aggSource).createParser(aggSource)) {
        aSourceParser.nextToken(); // move past the first START_OBJECT
        super.parse(aSourceParser, context);
    }
}
 
Example 4
Source File: FilterBinaryParseElement.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void parse(XContentParser parser, SearchContext context) throws Exception {
    byte[] filterSource = parser.binaryValue();
    try (XContentParser fSourceParser = XContentFactory.xContent(filterSource).createParser(filterSource)) {
        ParsedQuery filter = context.queryParserService().parseInnerFilter(fSourceParser);
        if (filter != null) {
            context.parsedPostFilter(filter);
        }
    }
}
 
Example 5
Source File: QueryBinaryParseElement.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void parse(XContentParser parser, SearchContext context) throws Exception {
    byte[] querySource = parser.binaryValue();
    try (XContentParser qSourceParser = XContentFactory.xContent(querySource).createParser(querySource)) {
        context.parsedQuery(context.queryParserService().parse(qSourceParser));
    }
}
 
Example 6
Source File: AbstractXContentParser.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
static Object readValue(XContentParser parser, MapFactory mapFactory, XContentParser.Token token) throws IOException {
    if (token == XContentParser.Token.VALUE_NULL) {
        return null;
    } else if (token == XContentParser.Token.VALUE_STRING) {
        return parser.text();
    } else if (token == XContentParser.Token.VALUE_NUMBER) {
        XContentParser.NumberType numberType = parser.numberType();
        if (numberType == XContentParser.NumberType.INT) {
            return parser.intValue();
        } else if (numberType == XContentParser.NumberType.LONG) {
            return parser.longValue();
        } else if (numberType == XContentParser.NumberType.FLOAT) {
            return parser.floatValue();
        } else if (numberType == XContentParser.NumberType.DOUBLE) {
            return parser.doubleValue();
        }
    } else if (token == XContentParser.Token.VALUE_BOOLEAN) {
        return parser.booleanValue();
    } else if (token == XContentParser.Token.START_OBJECT) {
        return readMap(parser, mapFactory);
    } else if (token == XContentParser.Token.START_ARRAY) {
        return readList(parser, mapFactory);
    } else if (token == XContentParser.Token.VALUE_EMBEDDED_OBJECT) {
        return parser.binaryValue();
    }
    return null;
}
 
Example 7
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 8
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 9
Source File: SecureHash.java    From crate with Apache License 2.0 4 votes vote down vote up
public static SecureHash fromXContent(XContentParser parser) throws IOException {
    XContentParser.Token currentToken;
    int iterations = 0;
    byte[] hash = new byte[0];
    byte[] salt = new byte[0];
    boolean hasPassword = false;

    while ((currentToken = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (currentToken == XContentParser.Token.FIELD_NAME) {
            while ((currentToken = parser.nextToken()) != XContentParser.Token.END_OBJECT) { // secure_hash
                hasPassword = true;
                if (currentToken == XContentParser.Token.FIELD_NAME) {
                    String currentFieldName = parser.currentName();
                    currentToken = parser.nextToken();
                    switch (currentFieldName) {
                        case X_CONTENT_KEY_ITERATIONS:
                            if (currentToken != XContentParser.Token.VALUE_NUMBER) {
                                throw new ElasticsearchParseException(
                                    "failed to parse SecureHash, 'iterations' value is not a number [{}]", currentToken);
                            }
                            iterations = parser.intValue();
                            break;
                        case X_CONTENT_KEY_HASH:
                            if (currentToken.isValue() == false) {
                                throw new ElasticsearchParseException(
                                    "failed to parse SecureHash, 'hash' does not contain any value [{}]", currentToken);
                            }
                            hash = parser.binaryValue();
                            break;
                        case X_CONTENT_KEY_SALT:
                            if (currentToken.isValue() == false) {
                                throw new ElasticsearchParseException(
                                    "failed to parse SecureHash, 'salt' does not contain any value [{}]", currentToken);
                            }
                            salt = parser.binaryValue();
                            break;
                        default:
                            throw new ElasticsearchParseException("failed to parse secure_hash");
                    }
                }
            }
        }
    }

    if (hasPassword) {
        return SecureHash.of(iterations, salt, hash);
    }
    return null;
}