コード例 #1
0
  @Override
  public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
    XContentParser parser = parseContext.parser();

    boolean disableCoord = false;
    float boost = 1.0f;
    int minimumNumberShouldMatch = -1;

    List<BooleanClause> clauses = newArrayList();

    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 ("must".equals(currentFieldName)) {
          clauses.add(new BooleanClause(parseContext.parseInnerQuery(), BooleanClause.Occur.MUST));
        } else if ("must_not".equals(currentFieldName) || "mustNot".equals(currentFieldName)) {
          clauses.add(
              new BooleanClause(parseContext.parseInnerQuery(), BooleanClause.Occur.MUST_NOT));
        } else if ("should".equals(currentFieldName)) {
          clauses.add(
              new BooleanClause(parseContext.parseInnerQuery(), BooleanClause.Occur.SHOULD));
        }
      } else if (token == XContentParser.Token.START_ARRAY) {
        if ("must".equals(currentFieldName)) {
          while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
            clauses.add(
                new BooleanClause(parseContext.parseInnerQuery(), BooleanClause.Occur.MUST));
          }
        } else if ("must_not".equals(currentFieldName) || "mustNot".equals(currentFieldName)) {
          while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
            clauses.add(
                new BooleanClause(parseContext.parseInnerQuery(), BooleanClause.Occur.MUST_NOT));
          }
        } else if ("should".equals(currentFieldName)) {
          while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
            clauses.add(
                new BooleanClause(parseContext.parseInnerQuery(), BooleanClause.Occur.SHOULD));
          }
        }
      } else if (token.isValue()) {
        if ("disable_coord".equals(currentFieldName) || "disableCoord".equals(currentFieldName)) {
          disableCoord = parser.booleanValue();
        } else if ("minimum_number_should_match".equals(currentFieldName)
            || "minimumNumberShouldMatch".equals(currentFieldName)) {
          minimumNumberShouldMatch = parser.intValue();
        } else if ("boost".equals(currentFieldName)) {
          boost = parser.floatValue();
        }
      }
    }

    BooleanQuery query = new BooleanQuery(disableCoord);
    for (BooleanClause clause : clauses) {
      query.add(clause);
    }
    query.setBoost(boost);
    if (minimumNumberShouldMatch != -1) {
      query.setMinimumNumberShouldMatch(minimumNumberShouldMatch);
    }
    return optimizeQuery(fixNegativeQueryIfNeeded(query));
  }