cakephp / elastic-search

Elastic search datasource for CakePHP
Other
88 stars 53 forks source link

How to query nested tags #118

Closed netstyler closed 7 years ago

netstyler commented 7 years ago

I try to get this query:

GET my_index/_search/
{
  "query": {
    "bool": {
        "should": [
           {
               "nested": {
                  "path":"tags",
                  "query": {
                    "bool": {
                      "must": [
                        {"match": {"tags.name": "Abu Dhabi"}}
                      ]
                    }
                  }
                }
           },
           {
               "nested": {
                  "path":"tags",
                  "query": {
                    "bool": {
                      "must": [
                        {"match": {"tags.name": "Zürich"}}
                      ]
                    }
                  }
                }
           }
        ]
    }
  }
}

I did spend now hours, days to get the query but without success. All my queries are always must queries not should queries:

[
    'query' => [
        'bool' => [
            'filter' => [
                (int) 0 => [
                    'bool' => [
                        'must' => [
                            (int) 0 => [
                                'nested' => [
                                    'path' => 'tags',
                                    'query' => [
                                        'term' => [
                                            'tags.name' => 'New York'
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
            ]
        ]
    ]
]

How to turn this in a should query? Or how to build the query to get articles where at least one tag does match?

Thanks for any help or hints.

Here how my document looks like:

{
   "_index":"my_index",
   "_type":"daily_news",
   "_id":"2",
   "_version":1,
   "_score":1,
   "_source":{
      "title":"Second record",
      "publish_date":"2016-01-01 12:00:00",
      "tags":[
         {
            "name":"New York",
            "identifier":null,
            "keyname":"newyork"
         },
         {
            "name":"Abu Dhabi",
            "identifier":null,
            "keyname":"abudhabi"
         }
      ]
   }
}

Mapping:

"mappings":{
   "daily_news":{
      "properties":{
         "title":{
            "type":"string"
         },
         "publish_date":{
            "type":"string"
         },
         "tags":{
            "include_in_root":true,
            "type":"nested",
            "properties":{
               "identifier":{
                  "type":"string"
               },
               "name":{
                  "type":"string"
               },
               "keyname":{
                  "type":"string"
               }
            }
         }
      }
   }
},