【问题标题】:Elasticsearch named queries inside hasParentQuery does not work?hasParentQuery 中的 Elasticsearch 命名查询不起作用?
【发布时间】:2017-01-03 16:15:31
【问题描述】:

我做了一个测试来测试我在 hasParentQuery 中得到了正确的命名查询。但是好像不行。

运行 elasticsearch 2.4,我找不到任何有关它的信息。或者它可能是一个错误? https://www.elastic.co/guide/en/elasticsearch/reference/2.4/search-request-named-queries-and-filters.html

输出

parent
outerBool
// "innerBool" and "term" should also be here?

JSON 搜索查询

{
  "query": {
    "bool": {
      "must": {
        "has_parent": {
          "query": {
            "bool": {
              "should": {
                "term": {
                  "value": {
                    "value": "1",
                    "_name": "term"
                  }
                }
              },
              "_name": "innerBool"
            }
          },
          "parent_type": "branch",
          "_name": "parent"
        }
      },
      "_name": "outerBool"
    }
  }
}

还有测试:

client.admin().indices().create(new CreateIndexRequest("my_index")).actionGet();

String employee =
    IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream("employeeMapping.json"));
client.admin().indices().preparePutMapping("my_index")
    .setType("employee").setSource(employee).execute().actionGet();
String branch = IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream("branchMapping.json"));
client.admin().indices().preparePutMapping("my_index").setType("branch").setSource(branch).execute()
    .actionGet();

String parentId = "1";
client.prepareIndex("my_index", "branch", parentId)
    .setSource(new HashMap<String, Object>() {{
        put("value", "1");
        put("value2", "2");
    }}).execute().actionGet();
client.prepareIndex("my_index", "employee", parentId)
    .setSource(new HashMap<String, Object>() {{
        put("id", "1");
    }}).setParent(parentId).execute().actionGet();

Thread.sleep(1000);

SearchRequestBuilder searchRequestBuilder = client.prepareSearch("my_index").setTypes("employee").setQuery(
    QueryBuilders.boolQuery().must(
        QueryBuilders.hasParentQuery("branch",
            QueryBuilders.boolQuery()
                .should(QueryBuilders.termQuery("value", "1").queryName("term")).queryName("innerBool")

        ).queryName("parent")
    ).queryName("outerBool")
);
SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();

Arrays.stream(searchResponse.getHits().getHits()[0].getMatchedQueries()).forEach(q ->
    System.out.println(q)
);

branchMapping.json

{
  "properties": {}
}

employeeMapping.json

{
  "_parent": {
    "type": "branch"
  }
}

【问题讨论】:

    标签: java elasticsearch


    【解决方案1】:

    我必须将 inner_hits 添加到父查询中,以便添加响应中的 innerHits 元素,并且我可以从那里获取匹配的查询。

    QueryBuilders.hasParentQuery("branch",
       QueryBuilders.boolQuery()
          .should(QueryBuilders.termQuery("value", "1").queryName("term")).queryName("innerBool"))
       .queryName("parent")
       .innerHit(new QueryInnerHitBuilder())
    

    【讨论】:

      猜你喜欢
      • 2018-08-28
      • 2017-02-11
      • 2017-07-08
      • 2018-04-10
      • 2015-01-05
      • 2018-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多