gpt4 book ai didi

java - Amazon CloudSearch结构化查询解析器

转载 作者:行者123 更新时间:2023-12-01 21:53:03 24 4
gpt4 key购买 nike

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

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

最佳答案

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();

关于java - Amazon CloudSearch结构化查询解析器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34858189/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com