我有一个索引,其中对象内部有对象数组:
{
"id": "someID",
"tags": [],
"people": [
{
"id": "userID",
"role": "roleTARGET1",
"firstName": "name",
"lastName": "lastname",
"fullName": "fullname"
},
{
"id": "userID",
"role": "role",
"firstName": "name",
"lastName": "lastname",
"fullName": "fullname"
},
{
"id": "userID",
"role": "roleTARGET2",
"firstName": "name",
"lastName": "lastname",
"fullName": "fullname"
}
],
"importantPerson": {
"id": "userID",
"role": "role",
"firstName": "name",
"lastName": "lastname",
"fullName": "fullname"
},
"highlightedFields": {}
}
我的查询需要的工作方式是:
- 如果重要人物对象不为空,则匹配(字符串匹配)重要人物的全名
- 如果重要人物是空的;人;具有角色的数组=";roleTARGET1";或";roleTARGET2">
我在想某种XOR结构可以在这种情况下工作。
我解析和生成查询的方式是通过Java中的RestHighLevelClient,这里有一个片段:
private void addSearchQuery(BoolQueryBuilder query, List<Filter> filters) {
String value = filters.get(0).getValues().get(0).getValue();
String[] fields = new String[filters.size()];
int i = 0;
for (Filter filter : filters) {
fields[i] = filter.getPath();
i++;
}
addMultiMatchQuery(query, fields, value);
}
private void addMultiMatchQuery(BoolQueryBuilder query, String[] fields, String value) {
query.filter(QueryBuilders.multiMatchQuery(value, fields).operator(Operator.AND).type(MultiMatchQueryBuilder.Type.BOOL_PREFIX));
}
你知道我该怎么做吗?
无论是java代码还是查询都将不胜感激。
谢谢!
{
"query": {
"bool": {
"should": [
{
"bool": {
"must": {
"match": {"importantPerson.fullName": "fullname"}
}
}
},
{
"bool": {
"must": [
{"term": {"importantPerson.fullName": ""}},
{
"bool":{
"should":[
{
"term":{
"people.role":"roleTARGET1"
}
},
{
"term":{
"people.role":"roleTARGET2"
}
}
]
}
}
]
}
}
]
}
}
}
它执行OR
=should
(importantPerson's full name
匹配
OR
{object empty
AND
`OR = role a or b`
}}