예제 #1
0
    /**
     * Parse a set of {@link ContextQuery} according to a given mapping
     *
     * @param mappings List of mapping defined y the suggest field
     * @param parser parser holding the settings of the queries. The parsers current token is
     *     assumed hold an array. The number of elements in this array must match the number of
     *     elements in the mappings.
     * @return List of context queries
     * @throws IOException if something unexpected happened on the underlying stream
     * @throws ElasticsearchParseException if the list of queries could not be parsed
     */
    public static List<ContextQuery> parseQueries(
        Map<String, ContextMapping> mappings, XContentParser parser)
        throws IOException, ElasticsearchParseException {

      Map<String, ContextQuery> querySet = new HashMap<>();
      Token token = parser.currentToken();
      if (token == Token.START_OBJECT) {
        while ((token = parser.nextToken()) != Token.END_OBJECT) {
          String name = parser.text();
          ContextMapping mapping = mappings.get(name);
          if (mapping == null) {
            throw new ElasticsearchParseException("no mapping defined for [" + name + "]");
          }
          parser.nextToken();
          querySet.put(name, mapping.parseQuery(name, parser));
        }
      }

      List<ContextQuery> queries = Lists.newArrayListWithExpectedSize(mappings.size());
      for (ContextMapping mapping : mappings.values()) {
        queries.add(querySet.get(mapping.name));
      }
      return queries;
    }