2

My mapping is (part of it):

$index =  [
"mappings" => [
    "goods" => [
        "dynamic_templates"=> [
                        [
                            "iattribute_id"=> [
                                "match_mapping_type"=> "string",
                                "match"=>   "attribute_id",
                                "mapping"=> [
                                    "type"=> "integer"
                                ]
                            ]
                        ],
                        [
                            "iattribute_value"=> [
                                "match_mapping_type"=> "string",
                                "match"=>   "attribute_value",
                                "mapping"=> [
                                    "type"=> "string",
                                    "index" => "not_analyzed"
                                ]
                            ]
                        ]
                    ],
        "properties" => [
            ...
            "individual_attributes" => [
                            "type" => "nested",
                            "properties" => [
                                "template_id" => ["type" => "integer"],
                                "attributes_set" => [
                                    "type" => "nested",
                                    "properties" => [
                                        "attribute_id" => ["type" => "integer"],
                                        "attribute_value" => ["type" => "string", "index" => "not_analyzed"]
                                    ]
                                ]
                            ]
                        ]
            ...
        ]
    ]
]
];

How can I query attribute_id and attribute_value? They are nested inside nested. I can't understand how to specify path to fields. I've composed query but it doesn't work.

GET /index/type/_search
{
"query" : {
  "nested" : {
    "path" : "individual_attributes.attributes_set",
    "score_mode" : "none",
      "filter": {
        "bool": {
          "must": [
            {
              "term" : {
                "individual_attributes.attributes_set.attribute_id": "20"
              }
            },
            {
              "term" : {
                "individual_attributes.attributes_set.attribute_value": "commodi"
              }
            }
          ]
        }
      }
    }
  }

}
0

1 Answer 1

1

Try this:

{
  "query": {
    "nested": {
      "path": "individual_attributes",
      "score_mode": "none",
      "filter": {
        "nested": {
          "path": "individual_attributes.attributes_set",
          "query": {
            "bool": {
              "must": [
                {
                  "term": {
                    "individual_attributes.attributes_set.attribute_id": "20"
                  }
                },
                {
                  "term": {
                    "individual_attributes.attributes_set.attribute_value": "commodi"
                  }
                }
              ]
            }
          }
        }
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

6 Comments

It doesn't work...Maby something wrong with my mapping. GET /index/_mapping/type provides me the following - laravel.io/bin/8KW8v
If that's your mapping, you have a bit more serious issues with the index. The mapping doesn't contain nested fields, as per your mapping in php. So, either the index is not created the you think it is in your php code, or that mapping belongs to a different index.
I'm not able to view that mapping (laravel.io/bin/fork/yGvY6) unless I give read permissions to my private email address in github. And I'm not going to do that. But, as I said, the mapping I'm seeing in laravel.io/bin/8KW8v doesn't match the one you have in your php code. There's nothing more to add to that. You need to fix your mapping so that those fields are nested. And then the query I suggested will work.
It seems that your variant of code is correct. I haven't checked yet, but that's why I think so. I've created mapping via sense in my browser, and execution of GET /index/_mapping/type gives me correct mapping. But when I create the same mapping via php it gives me [] (emptyness). Something wrong with my php. I'll check what exactly and then I'll check your code. P.S. I haven't seen your comment when I've been writing mine. Yes, I'll check mapping.
I have json code for sense in browser - creation of mapping is succeed. I use the same code (json_decode($json_string,1)) in my php and something goes wrong.
|

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.