Examples of XContentParser


Examples of org.elasticsearch.common.xcontent.XContentParser

    builder.field(fieldName, new String(data, charset));
  }

  public static void addComplexField(XContentBuilder builder, String fieldName,
      XContentType contentType, byte[] data) throws IOException {
    XContentParser parser = null;
    try {
      XContentBuilder tmp = jsonBuilder();
      parser = XContentFactory.xContent(contentType).createParser(data);
      parser.nextToken();
      tmp.copyCurrentStructure(parser);
      builder.field(fieldName, tmp.string());
    } catch (JsonParseException ex) {
      // If we get an exception here the most likely cause is nested JSON that
      // can't be figured out in the body. At this point just push it through
      // as is, we have already added the field so don't do it again
      addSimpleField(builder, fieldName, data);
    } finally {
      if (parser != null) {
        parser.close();
      }
    }
  }
View Full Code Here

Examples of org.elasticsearch.common.xcontent.XContentParser

   * @return parsed response (May be Map, or List, or simple value)
   * @throws UnsupportedEncodingException
   * @throws IOException
   */
  protected Object parseJSONResponse(byte[] responseData) throws UnsupportedEncodingException, IOException {
    XContentParser parser = null;
    // wrap response so we are able to process JSON array of values on root level of response, which is not supported
    // by XContentFactory
    try {
      StringBuilder sb = new StringBuilder();
      sb.append("{ \"wrapit\" : ").append(new String(responseData, "UTF-8")).append("}");
      responseData = sb.toString().getBytes("UTF-8");
      parser = XContentFactory.xContent(XContentType.JSON).createParser(responseData);
      Map<String, Object> wrappedResponseParsed = parser.mapAndClose();
      return wrappedResponseParsed.get("wrapit");
    } finally {
      if (parser != null)
        parser.close();
    }
  }
View Full Code Here

Examples of org.elasticsearch.common.xcontent.XContentParser

   * @param filePath path inside jar/classpath pointing to JSON file to read
   * @return parsed JSON file
   * @throws SettingsException
   */
  public static Map<String, Object> loadJSONFromJarPackagedFile(String filePath) throws SettingsException {
    XContentParser parser = null;
    try {
      parser = XContentFactory.xContent(XContentType.JSON).createParser(Utils.class.getResourceAsStream(filePath));
      Map<String, Object> ret = parser.mapAndClose();
      if (logger.isDebugEnabled())
        logger.debug("jar packaged JSON file {} content is: {}", filePath, ret);
      return ret;
    } catch (IOException e) {
      throw new SettingsException(e.getMessage(), e);
    } finally {
      if (parser != null)
        parser.close();
    }
  }
View Full Code Here

Examples of org.elasticsearch.common.xcontent.XContentParser

   * @throws Exception
   */
  @Override
  @SuppressWarnings("unchecked")
  public List<String> getAllJIRAProjects() throws Exception {
    XContentParser parser = null;
    try {
      byte[] responseData = performJIRAGetRESTCall("project", null);
      logger.debug("JIRA REST response data: {}", new String(responseData));

      StringBuilder sb = new StringBuilder();
      sb.append("{ \"projects\" : ").append(new String(responseData, "UTF-8")).append("}");
      responseData = sb.toString().getBytes("UTF-8");
      parser = XContentFactory.xContent(XContentType.JSON).createParser(responseData);
      Map<String, Object> responseParsed = parser.mapAndClose();

      List<String> ret = new ArrayList<String>();
      for (Map<String, Object> mk : (List<Map<String, Object>>) responseParsed.get("projects")) {
        ret.add((String) mk.get("key"));
      }

      return ret;
    } finally {
      if (parser != null)
        parser.close();
    }
  }
View Full Code Here

Examples of org.elasticsearch.common.xcontent.XContentParser

  public ChangedIssuesResults getJIRAChangedIssues(String projectKey, int startAt, Date updatedAfter, Date updatedBefore)
      throws Exception {
    byte[] responseData = performJIRAChangedIssuesREST(projectKey, startAt, updatedAfter, updatedBefore);
    logger.debug("JIRA REST response data: {}", new String(responseData));

    XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(responseData);
    Map<String, Object> responseParsed = parser.mapAndClose();
    Integer startAtRet = Utils.nodeIntegerValue(responseParsed.get("startAt"));
    Integer maxResults = Utils.nodeIntegerValue(responseParsed.get("maxResults"));
    Integer total = Utils.nodeIntegerValue(responseParsed.get("total"));
    List<Map<String, Object>> issues = (List<Map<String, Object>>) responseParsed.get("issues");
    if (startAtRet == null || maxResults == null || total == null) {
View Full Code Here

Examples of org.elasticsearch.common.xcontent.XContentParser

   * @param filePath path inside jar/classpath pointing to JSON file to read
   * @return parsed JSON file
   * @throws SettingsException
   */
  public static Map<String, Object> loadJSONFromJarPackagedFile(String filePath) throws SettingsException {
    XContentParser parser = null;
    try {
      parser = XContentFactory.xContent(XContentType.JSON).createParser(Utils.class.getResourceAsStream(filePath));
      Map<String, Object> ret = parser.mapAndClose();
      if (logger.isDebugEnabled())
        logger.debug("jar packaged JSON file {} content is: {}", filePath, ret);
      return ret;
    } catch (IOException e) {
      throw new SettingsException(e.getMessage(), e);
    } finally {
      if (parser != null)
        parser.close();
    }
  }
View Full Code Here

Examples of org.elasticsearch.common.xcontent.XContentParser

    @Override public String[] names() {
        return new String[]{NAME, Strings.toCamelCase(NAME)};
    }

    @Override public Filter parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
        XContentParser parser = parseContext.parser();

        Query query = null;
        Filter filter = null;
        float boost = 1.0f;
        String scope = null;
        String path = null;
        boolean cache = false;
        CacheKeyFilter.Key cacheKey = null;
        String filterName = null;

        // we need a late binding filter so we can inject a parent nested filter inner nested queries
        NestedQueryParser.LateBindingParentFilter currentParentFilterContext = NestedQueryParser.parentFilterContext.get();

        NestedQueryParser.LateBindingParentFilter usAsParentFilter = new NestedQueryParser.LateBindingParentFilter();
        NestedQueryParser.parentFilterContext.set(usAsParentFilter);

        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 (token == XContentParser.Token.START_OBJECT) {
                if ("query".equals(currentFieldName)) {
                    query = parseContext.parseInnerQuery();
                } else if ("filter".equals(currentFieldName)) {
                    filter = parseContext.parseInnerFilter();
                }
            } else if (token.isValue()) {
                if ("path".equals(currentFieldName)) {
                    path = parser.text();
                } else if ("boost".equals(currentFieldName)) {
                    boost = parser.floatValue();
                } else if ("_scope".equals(currentFieldName)) {
                    scope = parser.text();
                } else if ("_name".equals(currentFieldName)) {
                    filterName = parser.text();
                } else if ("_cache".equals(currentFieldName)) {
                    cache = parser.booleanValue();
                } else if ("_cache_key".equals(currentFieldName) || "_cacheKey".equals(currentFieldName)) {
                    cacheKey = new CacheKeyFilter.Key(parser.text());
                }
            }
        }
        if (query == null && filter == null) {
            throw new QueryParsingException(parseContext.index(), "[nested] requires either 'query' or 'filter' field");
View Full Code Here

Examples of org.elasticsearch.common.xcontent.XContentParser

    @Override public String[] names() {
        return new String[]{NAME, "more_like_this", "moreLikeThis"};
    }

    @Override public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
        XContentParser parser = parseContext.parser();

        MoreLikeThisQuery mltQuery = new MoreLikeThisQuery();
        mltQuery.setMoreLikeFields(new String[]{AllFieldMapper.NAME});
        mltQuery.setSimilarity(parseContext.searchSimilarity());
        Analyzer analyzer = null;

        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.isValue()) {
                if ("like_text".equals(currentFieldName) || "likeText".equals(currentFieldName)) {
                    mltQuery.setLikeText(parser.text());
                } else if ("min_term_freq".equals(currentFieldName) || "minTermFreq".equals(currentFieldName)) {
                    mltQuery.setMinTermFrequency(parser.intValue());
                } else if ("max_query_terms".equals(currentFieldName) || "maxQueryTerms".equals(currentFieldName)) {
                    mltQuery.setMaxQueryTerms(parser.intValue());
                } else if ("min_doc_freq".equals(currentFieldName) || "minDocFreq".equals(currentFieldName)) {
                    mltQuery.setMinDocFreq(parser.intValue());
                } else if ("max_doc_freq".equals(currentFieldName) || "maxDocFreq".equals(currentFieldName)) {
                    mltQuery.setMaxDocFreq(parser.intValue());
                } else if ("min_word_len".equals(currentFieldName) || "minWordLen".equals(currentFieldName)) {
                    mltQuery.setMinWordLen(parser.intValue());
                } else if ("max_word_len".equals(currentFieldName) || "maxWordLen".equals(currentFieldName)) {
                    mltQuery.setMaxWordLen(parser.intValue());
                } else if ("boost_terms".equals(currentFieldName) || "boostTerms".equals(currentFieldName)) {
                    mltQuery.setBoostTerms(true);
                    mltQuery.setBoostTermsFactor(parser.floatValue());
                } else if ("percent_terms_to_match".equals(currentFieldName) || "percentTermsToMatch".equals(currentFieldName)) {
                    mltQuery.setPercentTermsToMatch(parser.floatValue());
                } else if ("analyzer".equals(currentFieldName)) {
                    analyzer = parseContext.analysisService().analyzer(parser.text());
                }
            } else if (token == XContentParser.Token.START_ARRAY) {
                if ("stop_words".equals(currentFieldName) || "stopWords".equals(currentFieldName)) {
                    Set<String> stopWords = Sets.newHashSet();
                    while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                        stopWords.add(parser.text());
                    }
                    mltQuery.setStopWords(stopWords);
                } else if ("fields".equals(currentFieldName)) {
                    List<String> fields = Lists.newArrayList();
                    while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                        fields.add(parseContext.indexName(parser.text()));
                    }
                    mltQuery.setMoreLikeFields(fields.toArray(new String[fields.size()]));
                }
            }
        }
View Full Code Here

Examples of org.elasticsearch.common.xcontent.XContentParser

    @Override public String[] names() {
        return new String[]{NAME};
    }

    @Override public Filter parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
        XContentParser parser = parseContext.parser();

        ArrayList<Filter> filters = newArrayList();

        boolean cache = false;
        CacheKeyFilter.Key cacheKey = null;

        String filterName = null;
        String currentFieldName = null;
        XContentParser.Token token = parser.currentToken();
        if (token == XContentParser.Token.START_ARRAY) {
            while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                filters.add(parseContext.parseInnerFilter());
            }
        } else {
            while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                if (token == XContentParser.Token.FIELD_NAME) {
                    currentFieldName = parser.currentName();
                } else if (token == XContentParser.Token.START_ARRAY) {
                    if ("filters".equals(currentFieldName)) {
                        while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                            filters.add(parseContext.parseInnerFilter());
                        }
                    } else {
                        while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                            filters.add(parseContext.parseInnerFilter());
                        }
                    }
                } else if (token.isValue()) {
                    if ("_cache".equals(currentFieldName)) {
                        cache = parser.booleanValue();
                    } else if ("_name".equals(currentFieldName)) {
                        filterName = parser.text();
                    } else if ("_cache_key".equals(currentFieldName) || "_cacheKey".equals(currentFieldName)) {
                        cacheKey = new CacheKeyFilter.Key(parser.text());
                    }
                }
            }
        }
View Full Code Here

Examples of org.elasticsearch.common.xcontent.XContentParser

    @Override public String[] names() {
        return new String[]{NAME};
    }

    @Override public Filter parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
        XContentParser parser = parseContext.parser();

        boolean cache = true; // since usually term filter is on repeating terms, cache it by default
        CacheKeyFilter.Key cacheKey = null;
        String fieldName = null;
        String value = null;

        String filterName = 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 (token.isValue()) {
                if ("_name".equals(currentFieldName)) {
                    filterName = parser.text();
                } else if ("_cache".equals(currentFieldName)) {
                    cache = parser.booleanValue();
                } else if ("_cache_key".equals(currentFieldName) || "_cacheKey".equals(currentFieldName)) {
                    cacheKey = new CacheKeyFilter.Key(parser.text());
                } else {
                    fieldName = currentFieldName;
                    value = parser.text();
                }
            }
        }

        if (fieldName == null) {
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.