Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch responding blank _source

I am using @elastic/elasticsearch library in my node project and i am trying to create an index like this

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

When i am trying to fetch a record

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

The response is

 {
    "_index": "myIndex",
    "_type": "_doc",
    "_id": "cjijWHEBHKa8WEr-JNYu",
    "_score": 1,
    "_source": {}
},
like image 393
vivek jha Avatar asked Dec 17 '25 09:12

vivek jha


1 Answers

You basically missed a small thing, while indexing you are sending empty data in both your category and something_else field and in _source field, ES stores what you send as part of your JSON payload. _id is auto-generated in your case, hence you see the data there, but it was not part of your body(JSON payload), which would form _source content, hence its empty.

If you just include some data in your fields, those documents will have the _source data.

Let me show you by an example.

Index Def

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

Index doc with empty data.

POST /{{your-index-name}}/_doc/1

{
   --> note empty data or payload
}

Search request

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

Search response which shows empty _source

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

Index doc with some sample data

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

Again match-all search query, gives below result

 "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
            }
        ]
like image 67
Amit Avatar answered Dec 20 '25 14:12

Amit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!