ElasticSearch嵌套查询不返回数据

时间:2019-02-22 02:06:22

标签: java elasticsearch nested

我在ElasticSearch(客户)中有一个具有以下部分映射的索引:

      "addresses": {
          "type": "nested",
          "properties": {
              "address": {
                  "type": "keyword"
              },
              "phone": {
                  "type": "keyword"
              }
          }
      },

我正在尝试搜索特定的地址(我们称其为“ foo street”)。我正在使用ElasticSearch 6.3。和RestHighLevelClient(6.3)。

就我而言,以下变量具有以下值:

docName = "addresses"
name = "address"
value = "foo street"

我知道客户索引中的至少一个文档的地址为“ foo street”

我创建NestedQueryBuilder的代码是这样的:

QueryBuilder innerQuery = QueryBuilders.matchQuery(name, value);
NestedQueryBuilder nestedQueryBuilder = QueryBuilders.nestedQuery(
    docName, innerQuery, ScoreNode.None);

SearchRequest request = new SearcRequest("customers");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(nestedQueryBuilder);
searchSourceBuilder.from(from);
searchSourceBuilder.size(size);
request.source(searchSourceBuilder);

如果我记录我的NestedQueryBuilder对象(调用toString(),这就是我得到的:

{
"nested" : {
  "query" : {
    "match" : {
      "ip_address" : {
        "query" : "10.214.159.193",
        "operator" : "OR",
        "prefix_length" : 0,
        "max_expansions" : 50,
        "fuzzy_transpositions" : true,
        "lenient" : false,
        "zero_terms_query" : "NONE",
        "auto_generate_synonyms_phrase_query" : true,
        "boost" : 1.0
      }
    }
  },
  "path" : "links",
  "ignore_unmapped" : false,
  "score_mode" : "none",
  "boost" : 1.0
}
}

为什么我没有收到任何点击?

以下是SearchResponse中的值:

Status: OK
Took: 0s
terminatedEarly: null
timedOut: false
total hits: 0
maxScore: NaN

1 个答案:

答案 0 :(得分:1)

您应该对嵌套字段使用完全限定的名称。使用links.ip_address代替ip_address

您的查询应如下:

{
  "nested": {
    "query": {
      "match": {
        "links.ip_address": {          <--------------------- change here
          "query": "10.214.159.193",
          "operator": "OR",
          "prefix_length": 0,
          "max_expansions": 50,
          "fuzzy_transpositions": true,
          "lenient": false,
          "zero_terms_query": "NONE",
          "auto_generate_synonyms_phrase_query": true,
          "boost": 1
        }
      }
    },
    "path": "links",
    "ignore_unmapped": false,
    "score_mode": "none",
    "boost": 1
  }
}

在代码中:

String fullName = docName + "." + name;
QueryBuilder innerQuery = QueryBuilders.matchQuery(fullName, value);