1

I am saving multiple file data to Elasticsearch using /cairn/_doc/id where id is unique id of file. The sample data is like below.

var file = 1;
var meraData = '{"data":[{"page":"1","lineid":"00f6","text":"OIL AND NATURAL ","fileId":"175"},{"page":"1","lineid":"4411","text":"Source Rock Geochemistry","fileId":"175"},{"page":"1","lineid":"c267","text":"CHENNAI -600034","fileId":"175"}]}';
function sendElastic(meraData, file){
    $.ajax({
        type:'POST',
        url: 'https://search-cairn-o3fpbj34hxgttac6b3dnuc37ie.us-east-1.es.amazonaws.com/cairn/_doc/'+file,
        data:JSON.stringify(meraData),
        contentType: "application/json",
        dataType: "json",
        success:function(data){
            console.log(data);
        },
        error: function(data){
            console.log(data.responseJSON);
        }
    });
}

After making a GET request to cairn/_search?q=OIL reponse is like below.

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 0.57148063,
    "hits": [
      {
        "_index": "cairn",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.57148063,
        "_source": {
          "data": [
            {
              "page": "1",
              "lineid": "00f6",
              "text": "OIL AND NATURAL ",
              "fileId": "175"
            },
            {
              "page": "1",
              "lineid": "4411",
              "text": "Source Rock Geochemistry",
              "fileId": "175"
            },
            {
              "page": "1",
              "lineid": "c267",
              "text": "CHENNAI -600034",
              "fileId": "175"
            }
          ]
        }
      }
    ]
  }
}

How can I change the response format to show only exact matched array value in _source.data array? For example, for query "OIL"

{
  "page": "1",
  "lineid": "00f6",
  "text": "OIL AND NATURAL ",
  "fileId": "175"
}

should return in _source.data

Or is it possible to save it like /cairn/_doc/id/lineid ?

3
  • You need to store source.data as nested type and use nested query to get exact match. Refer link elastic.co/guide/en/elasticsearch/reference/current/… for further details Commented Oct 1, 2019 at 11:04
  • @jaspreetchahal I used nested query too. Even if I get the exact match, the response includes all source.data array instead of matched array key. Commented Oct 1, 2019 at 11:16
  • this answer may help: stackoverflow.com/a/32774267/4604579 (hint: use nested inner_hits) Commented Oct 1, 2019 at 11:21

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.