从多个条件的Elasticsearch获取数据

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.

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);