当field是一个对象时,elasticsearch不会返回所有匹配

MrE*_*MrE 1 javascript elasticsearch

我对Elasticsearch给出的结果感到困惑

我用一个简单的查询进行测试,

body: {
   size: 100,
   from: 0,
   query: { match_all: {} },
   fields: ["object"]    <--- this is an object

}
Run Code Online (Sandbox Code Playgroud)

挺直的

我得到了点击. hits.total = 141380

但是 hits.hits.length = 49

如果我增加size1000,我得到hits.hits.length = 129

而hits.total仍然是 hits.total = 141380

如果我不使用fields,我得到所有的文档,并以可读的格式,但如果我指定字段我得到一个关键数组对象数组的数组(是搜索结果的复杂格式!)

有人可以解释为什么使用字段时会有所不同吗?我希望对象只有我要求的字段.

Vin*_*nic 5

您需要使用源过滤来获取_source仅包含请求字段的后端.

只需将fields查询替换_source为:

body: {
   size: 100,
   from: 0,
   query: { match_all: {} },
   _source: ["object"]    <--- this is an object

}
Run Code Online (Sandbox Code Playgroud)

这就是使用fields选项时结果(命中计数)不同的原因:

fields参数是关于显式标记为存储在映射中的字段,默认情况下是关闭的,通常不建议使用.为了向后兼容,如果fields参数指定了未存储的字段(store映射设置为false),它将加载_source并从中提取它.

此外,只能通过字段选项返回叶字段.因此无法返回对象字段,此类请求将失败.

但是,如果您使用选项中的object字段搜索多个索引,fields并且如果此字段不存在或映射到除object(string或者long)所有或部分索引之外的其他数据类型,则此类请求将不会失败并且将是在hits数组中返回.

这就是为什么你会得到不同的价值观hits.totalhits.hits.length

典型此类查询的输出如下所示:

{
  "took": 91,
  "timed_out": false,
  "_shards": {
    "total": 10,
    "successful": 9,
    "failed": 1,
    "failures": [
      {
        "shard": 1,
        "index": "test_index1",
        "node": "GQN77mbqTSmmmwQlmjSBEg",
        "reason": {
          "type": "illegal_argument_exception",
          "reason": "field [object] isn't a leaf field"
        }
      }
    ]
  },
  "hits": {
    "total": 25,
    "max_score": 1,
    "hits": [
      {
        "_index": "test_index2",
        "_type": "test_type1",
        "_id": "1",
        "_score": 1
      },
      {
        "_index": "test_index2",
        "_type": "test_type2",
        "_id": "1",
        "_score": 1
      },
      {
        "_index": "test_index2",
        "_type": "test_type3",
        "_id": "1",
        "_score": 1,
        "fields": {
          "object": [
            "simple text"    <-- here the field 'object' is a leaf field
          ]
        }
      }
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

以下hits.total是搜索所有索引的文档总数:因为它匹配所有查询.

并且hits.hits.length是请求没有失败的文档的no:.