Nested Filter Problem


(Pedro Lugo) #1

Hi everyone, i am facing a problem implementing nested query on my elasticsearch 5.6 cluster.

I've this mapping:

    {
       "my_test": {
         "mappings": {
           "co_product": {
             "properties": {
               "photos": {
                 "type": "keyword"
               },
               "specs": {
                 "type": "nested",
                 "properties": {
                   "value": {
                     "type": "keyword"
                   }
                 }
               }
             }
           }
         }
       }
    }

And this data:

[
    {
         "photos": [
        "http://foto2.png",
      ],
      "specs": [
        {
          "value": "Tornillo"
        },
        {
          "value": "Dorado"
        }
      ]
    },{
         "photos": [
        "http://foto2.png",
      ],
      "specs": [
        {
          "value": "Tornillo"
        },
        {
          "value": "Plata"
        }
      ]
    }
]

I need a query to get a exactly the items that match with all the filters that i send, i tried like this:

"query": {
    "nested": {
      "path": "specs",
      "query": {
        "bool": {
          "must": [
            {"match": {"specs.value": "Tornillo"}},
            {"match": {"specs.value": "Dorado"}}
          ]
        }
      }
    }
  }

But that brings me back an empty response.

I tried this way too:

"query": {
    "nested": {
      "path": "specs",
      "query": {
        "bool": {
          "should": [
            {"match": {"specs.value": "Tornillo"}},
            {"match": {"specs.value": "Dorado"}}
          ]
        }
      }
    }
  }

But that brings me back all the items, and i just want the item that match exaclty.

Please i've headache trying....


Querying Nested Datatype
(Arvind Rao) #2

See my response to a similar question

In the context of queries on nested structures, an element in the "specs" array can either match "Tornillo" or "Dorado" but not both.

Try what I suggested in the other question, i.e. use the inner_hits object to determine if the count of matched elements is 2. Only the records where it is 2 are valid results.

{
	"query": {
		"nested": {
			"path": "specs",
			"query": {
				"bool": {
					"filter": [{
							"terms": {
								"specs.value": [
									"Tornillo", "Dorado"
								]
							}
						}
					]
				}
			},
			"inner_hits": {}
		}
	}
}

(Pedro Lugo) #3

That do not work for me, because i need to retorn the exactly number of items that match with the filter, i need to response to a web page that will paginate the results.

Its there a some way to do that? Changing the mapping too i don't know, im noob in this. (sorry my bad english)


(Animageofmine) #4

Can you share the expected answer? Have you looked at the object type if that fits your requirement?


(Pedro Lugo) #5

Thanks a lot for your answers, i appreciate it. @animageofmine & @Arvind_Rao

I need a response like that:

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "my_test",
        "_type": "co_product",
        "_id": "AWEU4KdEcjKtthiRBLYL",
        "_score": 1,
        "_source": {
          "photos": [
            "http://foto2.png",
            "http://foto1.png",
            "http://foto.png"
          ],
          "specs": [
            {
              "value": "Tornillo"
            },
            {
              "value": "Dorado"
            }
          ]
        }
      }
    ]
  }
}

Because i need to sent a equivalente query like that:

WHERE specs.value= "Tornillo" 
AND specs.value = "Dorado"

(Arvind Rao) #6

Just responded on the other thread. Found solution on StackOverflow

{
	"query": {
		"bool": {
			"must": [{
					"nested": {
						"path": "specs",
						"query": {
							"bool": {
								"must": [{
										"term": {
											"specs.value": "Tornillo"
										}

									}
								]
							}
						}
					}
				},
				{
					"nested": {
						"path": "specs",
						"query": {
							"bool": {
								"must": [{
										"term": {
											"specs.value": "Dorado"
										}

									}
								]
							}
						}
					}
				}
			]
		}
	}
}

(Pedro Lugo) #7

OMG, i was searching all night with no solution. Thank u very much. Solved!


(system) #8

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