Elasticsearch 响应空白_source

Elasticsearch responding blank _source

我在我的节点项目中使用 @elastic/elasticsearch 库,我正在尝试创建这样的索引

const { Client } = require('@elastic/elasticsearch')
const client = new Client({ node: 'http://localhost:9200' })
await client.index({
    index: 'myIndex',
    refresh: true,
    body: {
        category: '',
        something_else: ''
    }
})

当我尝试获取记录时

const { body } = await client.search({
    index: 'myIndex',
    body: {
        query: {
            "match_all": {}
        }
    }
})

响应是

 {
    "_index": "myIndex",
    "_type": "_doc",
    "_id": "cjijWHEBHKa8WEr-JNYu",
    "_score": 1,
    "_source": {}
},

你基本上错过了一件小事,在索引时你在你的 categorysomething_else 字段和 _source 字段中发送空数据,ES 存储你发送的部分您的 JSON 负载。 _id 在您的情况下是自动生成的,因此您会在那里看到数据,但它不是您的 body(JSON payload) 的一部分,这将形成 _source 内容,因此它是空的。

如果您只在字段中包含一些数据,这些文档将具有 _source 数据。

举个例子

索引定义

{
    "mappings": {
        "properties": {
            "category": {
                "type": "text"
            },
            "something_else": {
                "type": "text"
            }
        }
    }
}

包含空数据的索引文档。

POST /{{您的索引名称}}/_doc/1

{
   --> note empty data or payload
}

搜索请求

{
    "query": {
        "match_all": {}
    }
}

显示空的搜索响应_source

   "hits": [
           {
            "_index": "justno",
            "_type": "_doc",
            "_id": "3",
            "_score": 1.0,
            "_source": {} --> Output similar to yours
        }
        ]

带有一些示例数据的索引文档

{
    "category": "foo",
    "something_else": "bar"
}

再次匹配所有搜索查询,给出以下结果

 "hits": [
            {
                "_index": "justno",
                "_type": "_doc",
                "_id": "4",
                "_score": 1.0,
                "_source": { --> doc which had data, while indexing
                    "category": "foo",
                    "something_else": "bar"
                }
            },
            {
                "_index": "justno",
                "_type": "_doc",
                "_id": "1",
                "_score": 1.0,
                "_source": {} --> note first doc response
            }
        ]