0

I have the following structure in my index:

[
    {
         name:"project1",
         users: [
                   {"id":"1"},
                   {"id":"2"}
         ]
    },
    #... more projects
]

I would like to know how can I get all projects of a specific user (by his id), here is what I have tried :

q = Q("term", id="1") 
resp = projects_index.query("nested",path="users",query=q).execute()

but I get no result, what am I missing ?
Thank you
Edit : here is my index mapping :

   {
      "projects": {
        "mappings": {
          "doc": {
            "properties": {
              "created_at": {
                "type": "date"
              },
              "name": {
                "type": "text"
              },
              "users": {
                "type": "nested",
                "properties": {
                  "id": {
                    "type": "text"
                  }
                }
              }
            }
          }
        }
      }
    }
3
  • 1
    Add the mapping of index, so that people can help you better. Commented Jun 14, 2019 at 3:33
  • Thank you for your answer, but I'm not sure what you mean by the mapping index ? I have given an example of a document (or array of documents), each filed project (document) has a name and a list of users Commented Jun 14, 2019 at 3:36
  • 1
    Use this to get mapping: GET /<index>/_mapping. Replace <index> with your index name. Commented Jun 14, 2019 at 3:37

1 Answer 1

1

The reason you are not getting results is because while specifying a nested path you should provide the full name including parent field name i.e. you should use users.id instead of just id. The query thus will translate to as below:

{
  "query": {
    "bool": {
      "filter": [
        {
          "nested": {
            "path": "users",
            "query": {
              "term": {
                "users.id": "1"
              }
            }
          }
        }
      ]
    }
  }
}

Suggestion: Change the type of id field to keyword, to prevent id value getting tokenism into multiple terms.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you ! for python eleasticsearch dsl syntax it's : __ instead of . : Q("term", users__id="id_looking_for")

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.