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

从多个条件的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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题