【问题标题】:Elasticsearch responding blank _sourceElasticsearch 响应空白_source
【发布时间】:2020-07-21 15:12:24
【问题描述】:

我在我的节点项目中使用@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": {}
},

【问题讨论】:

  • 已提供答案,请看一下,如果有其他问题,请告诉我
  • 只是我写了一个空白文本的例子..我实际上是在那里传递数据。
  • 你能分享一下你的ES映射吗?希望你没有在你的映射中禁用它 { "mappings": { "_source": { "enabled": false } } } 像这样?
  • {"myIndex":{"mappings":{}}}
  • 我也试过了,请问你用的是什么版本?

标签: node.js elasticsearch koa


【解决方案1】:

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

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

让我举个例子。

索引定义

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

索引文档为空数据。

POST /{{your-index-name}}/_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
            }
        ]

【讨论】:

    猜你喜欢
    • 2022-06-29
    • 2014-10-10
    • 1970-01-01
    • 1970-01-01
    • 2018-10-12
    • 1970-01-01
    • 2015-07-01
    • 2012-08-28
    • 1970-01-01
    相关资源
    最近更新 更多