Amazon CloudSearch结构化查询解析器

时间:2016-01-18 15:20:54

标签: java amazon amazon-cloudsearch

我需要解析Amazon CloudSearch"结构化"查询,但找不到任何相关的来源。有没有人知道解析这些查询的解析器?

相关文档:http://docs.aws.amazon.com/cloudsearch/latest/developerguide/searching-compound-queries.html

1 个答案:

答案 0 :(得分:3)

Awsome有一个非常方便的结构化查询查询构建器。我们的想法是将结构化查询字符串构建为

(and ('title:'star' (or actors:'Harrison Ford' actors:'William Shatner')

使用实用程序构建器,它使用字段,值和运算符{和,或者,eq,前缀等}并构造查询。查询的简单示例实现看起来像

class QueryBuilder {
    String field;
    String value;
    String operator;
    Set<QueryBuilder> queries;

    public QueryBuilder(String field, String value) {
         this.field = field;
         this.value = value;
    }

    public QueryBuilder(String field, String value, String operator, 
             QueryBuilder... queries) {
         this.field = field;
         this.value = value;
         this.operator = operator;
         this.queries = queries;
    }

    public static QueryBuilder and(QueryBuilder.. expr) {
         this("and", queries);
    }

    public static QueryBuilder or(QueryBuilder.. expr) {
         this("or", queries);
    }

    public String toString() {
        if(queries == null || queries.isEmpty() {
            ImmutableSet<String> queryParts = ImmutableSet.of("(",
                    operator, "field=", field, value, ")");
            return Joiner.on(' ').join(queryParts);
        } else {
            //nested queries for an operator
            String nestedQuery = Joiner.on(' ').join(queries);
            ImmutableSet<String> queryParts = ImmutableSet.of("(",
                     operator.toString(), nestedQuery, ")");
            return Joiner.on(' ').join(queryParts);
        }
    }
}

在您的代码中,只需致电

即可
QueryBuilder builder = QueryBuidler.and(new QueryBuilder("title", "star"), 
        QueryBuilder.or(new QueryBuilder("actors", "Harrison Ford"), 
            new QueryBuilder("actors", "William Shatner")));
String query = builder.toString();