0

I have a field with mapping:

   "my_field_name": {
            "type": "nested",
            "properties": {
              "labels": {
                "type": "text"
              },
              "source": {
                "type": "text"
              }
            }
          }

and when I put data, format is like:

"my_field_name": {
            "url_of_an_image": [
              "tag1",
              "tag2",
              "tag3",
              "tag4",
              "tag5"
            ],
            "url_of_an_image2": [
              "tag4",
              "tag5",
              "tag6",
              "tag7",
              "tag8"
            ]
}

I want to write an elasticsearch query to gather all the documents that contains given tag.

How can I search a tag in all documents and select the ones that contains given tag?

Thank you

2 Answers 2

1

(EDITED AFTER COMMENT)

As a first consideration, the mapping doesn't match the structure of your document, e.g., my_field_name has neither labels nor source fields. Which is the one that better represents your data?

As a second consideration, in your comment you said that

while querying I don't know "url_of_an_image" since it is different for each document

However, from your example, it looks like url_of_an_image is a property of the items of my_field_name rather than of the document. Therefore, I'd suggest doing something like:

"my_field_name": [
  {
    "image_url": "url_of_an_image",
    "tags": [
       "tag1",
       "tag2",
       "tag3",
       "tag4",
       "tag5"
    ]
  },
  {
    "image_url": "url_of_an_image2",
    "tags": [
       "tag4",
       "tag5",
       "tag6",
       "tag7",
       "tag8"
    ]
  }
]

And this would be the mapping:

"my_field_name": {
  "type": "nested",
  "properties": {
     "image_url": {
       "type": "text"
     },
     "tags": {
       "type": "text"
    }
  }
}

This way, you can run queries like:

{
  "query": {
    "nested" : {
      "path" : "my_field_name",
      "query" : {
        "bool" : {
          "must" : [
            { "match" : {"my_field_name.image_url" : "url_of_an_image2"} },
            { "match" : {"my_field_name.tags" : "tag3"} }
          ]
        }
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hello @glenacota, unfortunately it did not worked. And moreover, while querying I don't know "url_of_an_image" since it is different for each document (some of documents may have the same url_of_an_image but it is not important). I am trying to find a way to query without specifying a "url_of_an_image". I have different documents with different "url_of_an_image"s and each of them has a list of tags. I want to gather all the documents that contain given tag
Hello @glenacota, I will update here after trying your suggestion. Thank you
0

You can achieve this by using the Nested Query: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html

1 Comment

Actually, it does not provide me a solution. I tried it already

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.