dparivln22034
2016-12-13 13:16
浏览 81
已采纳

从多个条件的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.

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • drvvvuyia15070493 2016-12-13 13:23
    已采纳

    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);
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题