我在一个列表中有多个弹性查询,并希望并行执行,并且我希望该列表中所有查询的一个响应

时间:2019-04-29 07:28:13

标签: elasticsearch

与弹性搜索有关。我在列表中有多个弹性搜索查询,并希望并行执行所有查询,而我的目标是获取列表中并行执行的所有查询的单个响应。 列表就像:

[{
  "bool" : {
    "filter" : [ {
      "bool" : {
        "filter" : [ {
          "term" : {
            "scan.access_level" : "public"
          }
        }, {
          "bool" : {
            "should" : [ {
              "term" : {
                "container.id" : "qa-develop-3c8ffb06-387d-44fd-806b-b54b544a1946"
              }
            }, {
              "term" : {
                "container.container_id" : "3c8ffb06-387d-44fd-806b-b54b544a1946"
              }
            }, {
              "term" : {
                "container.uuid" : "3c8ffb06-387d-44fd-806b-b54b544a1946"
              }
            } ],
            "minimum_should_match" : "1"
          }
        } ],
        "must_not" : [ {
          "bool" : {
            "should" : {
              "terms" : {
                "plugin.id" : [ 3, 7, 15 ]
              }
            },
            "minimum_should_match" : "1"
          }
        }, {
          "term" : {
            "host.tracked" : "false"
          }
        }, {
          "term" : {
            "tracking.state" : "FIXED"
          }
        }, {
          "term" : {
            "modification" : "accepted"
          }
        }, {
          "terms" : {
            "severity" : [ 0 ]
          }
        } ]
      }
    }, {
      "bool" : {
        "should" : [ {
          "range" : {
            "tracking.last_found" : {
              "from" : "now-10d/d",
              "to" : "now-1d/d",
              "include_lower" : true,
              "include_upper" : true
            }
          }
        }, {
          "range" : {
            "tracking.last_fixed" : {
              "from" : "now-10d/d",
              "to" : "now-1d/d",
              "include_lower" : true,
              "include_upper" : true
            }
          }
        }, {
          "bool" : {
            "filter" : {
              "range" : {
                "scan.started_at" : {
                  "from" : "now-10d/d",
                  "to" : "now-1d/d",
                  "include_lower" : true,
                  "include_upper" : true
                }
              }
            },
            "must_not" : {
              "exists" : {
                "field" : "tracking"
              }
            }
          }
        } ],
        "minimum_should_match" : "1"
      }
    } ]
  }
}, {
  "bool" : {
    "filter" : [ {
      "bool" : {
        "filter" : [ {
          "term" : {
            "scan.access_level" : "public"
          }
        }, {
          "bool" : {
            "should" : [ {
              "term" : {
                "container.id" : "qa-develop-3c8ffb06-387d-44fd-806b-b54b544a1946"
              }
            }, {
              "term" : {
                "container.container_id" : "3c8ffb06-387d-44fd-806b-b54b544a1946"
              }
            }, {
              "term" : {
                "container.uuid" : "3c8ffb06-387d-44fd-806b-b54b544a1946"
              }
            } ],
            "minimum_should_match" : "1"
          }
        } ],
        "must_not" : [ {
          "bool" : {
            "should" : {
              "terms" : {
                "plugin.id" : [ 3, 7, 15 ]
              }
            },
            "minimum_should_match" : "1"
          }
        }, {
          "term" : {
            "host.tracked" : "false"
          }
        }, {
          "term" : {
            "tracking.state" : "FIXED"
          }
        }, {
          "term" : {
            "modification" : "accepted"
          }
        }, {
          "terms" : {
            "severity" : [ 0 ]
          }
        } ]
      }
    }, {
      "bool" : {
        "should" : [ {
          "range" : {
            "tracking.last_found" : {
              "from" : "now-30d/d",
              "to" : "now-21d/d",
              "include_lower" : true,
              "include_upper" : true
            }
          }
        }, {
          "range" : {
            "tracking.last_fixed" : {
              "from" : "now-30d/d",
              "to" : "now-21d/d",
              "include_lower" : true,
              "include_upper" : true
            }
          }
        }, {
          "bool" : {
            "filter" : {
              "range" : {
                "scan.started_at" : {
                  "from" : "now-30d/d",
                  "to" : "now-21d/d",
                  "include_lower" : true,
                  "include_upper" : true
                }
              }
            },
            "must_not" : {
              "exists" : {
                "field" : "tracking"
              }
            }
          }
        } ],
        "minimum_should_match" : "1"
      }
    } ]
  }
}, {
  "bool" : {
    "filter" : [ {
      "bool" : {
        "filter" : [ {
          "term" : {
            "scan.access_level" : "public"
          }
        }, {
          "bool" : {
            "should" : [ {
              "term" : {
                "container.id" : "qa-develop-3c8ffb06-387d-44fd-806b-b54b544a1946"
              }
            }, {
              "term" : {
                "container.container_id" : "3c8ffb06-387d-44fd-806b-b54b544a1946"
              }
            }, {
              "term" : {
                "container.uuid" : "3c8ffb06-387d-44fd-806b-b54b544a1946"
              }
            } ],
            "minimum_should_match" : "1"
          }
        } ],
        "must_not" : [ {
          "bool" : {
            "should" : {
              "terms" : {
                "plugin.id" : [ 3, 7, 15 ]
              }
            },
            "minimum_should_match" : "1"
          }
        }, {
          "term" : {
            "host.tracked" : "false"
          }
        }, {
          "term" : {
            "tracking.state" : "FIXED"
          }
        }, {
          "term" : {
            "modification" : "accepted"
          }
        }, {
          "terms" : {
            "severity" : [ 0 ]
          }
        } ]
      }
    }, {
      "bool" : {
        "should" : [ {
          "range" : {
            "tracking.last_found" : {
              "from" : "now-20d/d",
              "to" : "now-11d/d",
              "include_lower" : true,
              "include_upper" : true
            }
          }
        }, {
          "range" : {
            "tracking.last_fixed" : {
              "from" : "now-20d/d",
              "to" : "now-11d/d",
              "include_lower" : true,
              "include_upper" : true
            }
          }
        }, {
          "bool" : {
            "filter" : {
              "range" : {
                "scan.started_at" : {
                  "from" : "now-20d/d",
                  "to" : "now-11d/d",
                  "include_lower" : true,
                  "include_upper" : true
                }
              }
            },
            "must_not" : {
              "exists" : {
                "field" : "tracking"
              }
            }
          }
        } ],
        "minimum_should_match" : "1"
      }
    } ]
  }
}]

我在这里使用的弹性搜索端点是_search 并且我希望此列表中出现的所有这三个查询有一个汇总响应。

我用来获取响应的代码是:

static ElasticsearchQueryResponse<Map<String, Object>> queryEs(ElasticsearchClient esClient, String queryPath, QueryBuilder query,
                                                                   List<AbstractAggregationBuilder> aggregations, String metricName, boolean addPrefix = true,
                                                                   int numFilteredTGMembers = 0) {
                String aggString = aggregations.collect { XContentHelper.toString(it)[1..-2] }
                .join(", ")

        SearchSourceBuilder search = new SearchSourceBuilder()
                .size(0)
                .query(query)
                .aggregations("{$aggString}".bytes)
        log.debug("elasticsearch_search ${queryPath}=${search}")
        ElasticsearchQueryResponse res = esClient.performQuery(queryPath, search, mapTypeReference, addPrefix, metricName)
        log.info("es query filtering on ${numFilteredTGMembers} target group members took ${res.took} ms ")

        return res
    }

这将返回单个查询的单个响应,我需要所有查询的汇总响应。请帮助我。

0 个答案:

没有答案