Get different and specific results from Index

{
      "_index" : "test_index",
      "_type" : "test_type",
      "_id" : "test_id",
      "_version" : 7,
      "_seq_no" : 7,
      "_primary_term" : 4,
      "found" : true,
      "_source" : {
        "attachments" : [
          {
            "filename" : "test_1.txt",
            "data" : "VGhlIGluZ2VzdCBwbHVnaW5zIGV4dGVuZCBFbGFzdGljc2VhcmNoIGJ5IHByb3ZpZGluZyBhZGRpdGlvbmFsIGluZ2VzdCBub2RlIGNhcGFiaWxpdGllcy4=",
            "attachment" : {
              "content_type" : "text/plain; charset=ISO-8859-1",
              "language" : "en",
              "content" : "The ingest plugins extend Elasticsearch by providing additional ingest node capabilities.",
              "content_length" : 90
            }
          },
          {
            "filename" : "test_2.txt",
            "data" : "VGhlIGluZ2VzdCBhdHRhY2htZW50IHBsdWdpbiBsZXRzIEVsYXN0aWNzZWFyY2ggZXh0cmFjdCBmaWxlIGF0dGFjaG1lbnRzLg==",
            "attachment" : {
              "content_type" : "text/plain; charset=ISO-8859-1",
              "language" : "en",
              "content" : "The ingest attachment plugin lets Elasticsearch extract file attachments.",
              "content_length" : 74
            }
          }
        ]
      }
    }
    indent preformatted text by 4 spaces

I want to only extract test_2.txt data from this. For same, I wrote below query:
curl -X GET "localhost:9200/_search" -H 'Content-Type: application/json' -d'
{
"_source": false,
"query" : {
"terms" : {"attachments" : {["filename" : "test_2.txt"]}}
}
}
'

But I am getting blank results in hits key of the returned results.

Can anyone please help me in how to get those using the "terms" query?

Please format your code, logs or configuration files using </> icon as explained in this guide and not the citation button. It will make your post more readable.

Or use markdown style like:

```
CODE
```

This is the icon to use if you are not using markdown format:

There's a live preview panel for exactly this reasons.

Lots of people read these forums, and many of them will simply skip over a post that is difficult to read, because it's just too large an investment of their time to try and follow a wall of badly formatted text.
If your goal is to get an answer to your questions, it's in your interest to make it as easy to read and understand as possible.
Please update your post.

Here you should probably run something like:

GET _search
{
  "query" : {
    "terms" : {"attachments.filename" : "test_2.txt" }
  }
}

But it depends on what the mapping is as well.

Note that if you need to get only one of the attachments, then don't index both attachments within one single doc but prefer doing that individually.

Thank you for your valuable suggestion for posting on ES forum. Tried running above query, it throws below error:

{"error":{"root_cause":[{"type":"parsing_exception","reason":"[terms] query does not support [attachments.filename]","line":4,"col":41}],"type":"parsing_exception","reason":"[terms] query does not support [attachments.filename]","line":4,"col":41},"status":400}

Right. It should have been:

GET _search
{
  "query" : {
    "term" : {"attachments.filename" : "test_2.txt" }
  }
}

Query didn't throw an error, but gave blank output in "hits". As previously suggested by you, attach 1 attachment per doc if need to fetch 1 attachment per request.

Could you provide a full recreation script as described in About the Elasticsearch category. It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

Probably here you did not use a keyword data type for this field.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.