And on elasticsearch

1

I started recently to experiment with elasticsearch and I can not do an AND.

I have two objects like the following:

"objeto1": [{"id": 1,"tipo": [{"id": 1,"valor": 5},{"id": 2,"valor": 4},{"id": 3,"valor": 2}]}]

"objeto2": [{"id": 2,"tipo": [{"id": 1,"valor": 2},{"id": 2,"valor": 1},{"id": 3,"valor": 6}]}]

I want to filter the objects that within the "type" array have an object with "id" 3 and "value" 2. I have tried to use the MUST expression of elasticsearch to do the AND.

{
    "query": {
        "bool":{
            "must":[
                {
                    "term": {"tipo.id":3}
                },
                {
                    "term": {"tipo.valor":2}
                }
            ]
        }
    }
}

But it returns both objects, since within object1 and object 2 there are fields with "id" 3 and "value" 2. My intention would be to return only the object1, since it is the only one that has "type" an object with "id" 3 and "value" 2.

{
 "id": 3,
 "valor": 2
}

How could an AND be made to filter through two fields?

    
asked by kg994 11.06.2018 в 10:32
source

1 answer

0

First of all it would be to check how you have configured the mappings of your indexes. You should have it like this:

{
"mappings": {
    "tipo": {
      "type": "nested",
      "properties": {
        "valor": {
          "type": "integer"
        },
        "id": {
          "type": "integer"
        }
      }
    }
  }
}

On the other hand, the query would be as follows:

{
"query": {
"nested": {
  "path": "tipo",
  "query": {
    "bool": {
      "must": [
        { "match": {"tipo.valor": 2}},
        { "match": {"tipo.id": 3}}
      ]
    }
  }
}
}
}
    
answered by 14.08.2018 в 00:30