Can anybody help me with a difficult query?


(magorbe) #1

Hello !! I have this mapping :

"c_index": {
    "aliases": {},
    "mappings": {
        "an": {
            "properties": {
                "id": {
                    "type": "string"
                },
                "sm": {
                    "type": "nested",
                    "properties": {
                        "cr": {
                            "type": "nested",
                            "properties": {
                                "c": {
                                    "type": "string"
                                },
                                "e": {
                                    "type": "long"
                                },
                                "id": {
                                    "type": "string"
                                },
                                "s": {
                                    "type": "long"
                                }
                            }
                        },
                        "id": {
                            "type": "string"
                        }
                    }
                }
            }
        }
    }

And I need a query than gives me all the cr's when:

an.id == x and sm.id == y

I tried with :

{ 
  "query": {
    "bool": {
      "filter": [
        { "term": { "_id": "x" } },
        { "term": { "sm.id": "y" } }
      ]
    }
  }
}

And there is no error, but returns zero elements, and I am sure that there is one element for the query... If I do :

{ "query": { "bool": { "filter": [ { "term": { "_id": "x" } } ] } } }

Everything is OK, but the problem is when i tried to add the second term... Any idea ? Thanks!


(Rahul Desai) #2

You need to use bool query with a must block:

Something like this

{
  "size" : 0,
  "query" : {
    "bool": {
      "must": [
        {
          "match": {
            "id": "X"
          }
        },
        {
          "nested": {
            "path": "sm",
            "query": {
              "match": {
                "sm.id": "Y"
              }
            }
          }
        }
      ]
    }
  }
}

Hopefully that helps.


(magorbe) #3

thanks!!! It works fine but I have a problem... the query returns the content of all the 'an's' with id=X that has a 'sm' wit id = Y, but I don't need all the content of the "an", I need only the info of the sm's with id=Y, it's possible?? Thanks again!


(system) #4

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