3

Here is a sample product(2) data which I've indexed in my elasticsearch instance.

[{
    "type": "mobile",
    "name": "iPhone 7s",
    "price": 70000,
    "brand_id": "100",
    "spec": [{
        "id": 200,          // brand id
        "value": "apple"
    }, {
        "id": 201,          // color id
        "value": "black"
    }, {
        "id": 202,          // battery size id
        "value": "2200"
    }]
}, {
    "type": "mobile",
    "name": "Samsung Galaxy 5S",
    "price": 50000,
    "brand_id": "101",
    "spec": [{
        "id": 200,
        "value": "samsung"
    }, {
        "id": 201,
        "value": "silver"
    }, {
        "id": 202,
        "value": "3200"
    }]
}]

What I want to do is, fetching all mobile phones data where 'brand' will be 'samsung' & 'color' is equal to 'silver'.

I'm using PHP for communicating with Elasticsearch. Here is a sample PHP script which is returning all 'samsung' phones from elastic.

$params = [
            'index' => 'index name',
            'type' => 'products',
            'from' => $start,
            'size' => $limit,
            'body' => [
                'query' => [
                    'bool' => [
                        'must' => [
                            'match' => [
                                'type' => 'mobile'
                            ]
                        ],
                        [
                            'nested' => [
                                'path' => "spec",
                                "score_mode" => "max",
                                'query' => [
                                    'bool' => [
                                        'must' => [
                                            [
                                                'match' => [
                                                    "spec.id"  => 200
                                                ]
                                            ],
                                            [
                                                'match' => [
                                                    "spec.value"  => 'samsung'
                                                ]
                                            ]
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
            ]
        ];

$result = $client->search($params);

But, I can't able to understand how to include condition for 'color' field also. So that, I can get all 'samsung' mobiles having 'black' color only.

2
  • First you need to make sure that your spec field is of nested type. Is that the case? Commented Dec 13, 2016 at 13:18
  • That's I've already done at the time of mapping of the index. Commented Dec 13, 2016 at 13:19

2 Answers 2

1

Since your spec field is correctly mapped as a nested field, you simply need to modify your query like this (i.e. move the nested query inside the bool/must one and create another one for the color constraint):

$params = [
            'index' => 'index name',
            'type' => 'products',
            'from' => $start,
            'size' => $limit,
            'body' => [
                'query' => [
                    'bool' => [
                        'must' => [
                          [
                            'match' => [
                                'type' => 'mobile'
                            ]
                          ],
                          [
                            'nested' => [
                                'path' => "spec",
                                "score_mode" => "max",
                                'query' => [
                                    'bool' => [
                                        'must' => [
                                            [
                                                'match' => [
                                                    "spec.id"  => 200
                                                ]
                                            ],
                                            [
                                                'match' => [
                                                    "spec.value"  => 'samsung'
                                                ]
                                            ]
                                        ]
                                    ]
                                ]
                            ]
                          ],
                          [
                            'nested' => [
                                'path' => "spec",
                                "score_mode" => "max",
                                'query' => [
                                    'bool' => [
                                        'must' => [
                                            [
                                                'match' => [
                                                    "spec.id"  => 201
                                                ]
                                            ],
                                            [
                                                'match' => [
                                                    "spec.value"  => 'black'
                                                ]
                                            ]
                                        ]
                                    ]
                                ]
                            ]
                          ],
                       ]
                    ]
                ]
            ]
        ];

$result = $client->search($params);
Sign up to request clarification or add additional context in comments.

1 Comment

Wow!!! It's working. Thanks a lot again for helping me out. Now I can take it this from here.
0
You can give the multiple condition   
 $ownerid = 1068;
    $arrMain = array();
    array_push($arrMain, ['term' => ['endownerid' => $ownerid]]);
    array_push($arrMain, ['range' => [
        'dtype' => [
            'gte' => 5,
            'lte'=> 6
        ]
    ]]);
    
    $params = [
        "index" => "mongoindex1",
        "type" => "u_endata",
        "body" => [
           // "_source" => ["sno", "app_title", "title", "ddate", "decdata", "dtype", "url"],
            "size" => 500,
            "sort" => [
                "ddate" => [
                    "order" => "desc"
                ]
            ],
            "query" => [
                "bool" => [
                    "must" => $arrMain
                ]
            ],
        ]
    ];
    $cc_response = $client->search($params);

Comments

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.