3

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": {}
},
vivek jha
  • 344
  • 1
  • 11

1 Answers1

1

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
            }
        ]
Amit
  • 30,756
  • 6
  • 57
  • 88
  • I'm adding index/_doc with this kind of sample data. { "company_name":"name check", "company_email": "test@yopmail.com", "merchant": 12, "price": 98 } If I'm trying to search document data on kibana console for same index it's giving _source value but for the same document _source is empty while checking in postman. – radhika thakkar Jun 28 '22 at 16:33
  • @radhikathakkar, can you provide the complete URL of your request and response from postman – Amit Jun 29 '22 at 01:01
  • Postman API => http://localhost:3005/v1/elastic/search { "hits": [ { "_index": "index2", "_id": "Qj9Aq4EBm7k2dL95BZM6", "_score": 1, "_source": {} }, { "_index": "index2", "_id": "Qz9Fq4EBm7k2dL95hJNp", "_score": 1, "_source": {} } ] } – radhika thakkar Jun 29 '22 at 05:01
  • cloud url => https://{{cloudurl}}:9243/index2/_search?pretty=true ` { "hits": [ { "_index": "index2", "_id": "Qj9Aq4EBm7k2dL95BZM6", "_score": 1.0, "_source": { "company_email": "test@yopmail.com", "company_name": "name check", "price": 98 } } ] } ` – radhika thakkar Jun 29 '22 at 05:02
  • can you please check here? https://stackoverflow.com/questions/72790352/node-js-elasticsearch-responding-blank-source – radhika thakkar Jun 29 '22 at 05:05