ElasticSearch查询多维数组值

kzmpq1sx  于 2023-03-12  发布在  ElasticSearch
关注(0)|答案(1)|浏览(126)

我有这些Map:

'indices' => [
    'mappings' => [
        'default' => [
            'properties' => [
                'id' => [
                    'type' => 'keyword',
                ],
                'name' => [
                    'type' => 'text',
                    'analyzer' => 'english',
                ],
            ],
        ],
    ],

我需要存储字段stocks arr[obj] stock看起来像:

{stock_id:uint, qty:uint, hidden: bool}

然后,我需要使用搜索namewhere stocks.stock_id IN [1,2,3] and qty>0 and hidden != true查询此索引
如何实现这一点-我是否需要更改Map设置中的某些内容?
顺便说一句,我使用拉拉威尔侦察机引擎盖下的弹性引擎。但你的答案会有帮助,如果你给我看的只是原始查询

xzabzqsa

xzabzqsa1#

试试这个

$query = [
    'bool' => [
        'must' => [
            ['term' => ['id' => $id]],
            ['term' => ['status' => $status]],
            ['range' => ['quantity' => ['gte' => $min_quantity, 'lte' => $max_quantity]]],
        ],
    ],
];

$params = [
    'index' => 'my_model_index',
    'body' => [
        'query' => $query,
    ],
];

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

对于多维数组,请使用嵌套

$query = [
    'nested' => [
        'path' => 'my_array',
        'query' => [
            'bool' => [
                'must' => [
                    ['term' => ['my_array.id' => $id]],
                    ['term' => ['my_array.status' => $status]],
                    ['range' => ['my_array.quantity' => ['gte' => $min_quantity, 'lte' => $max_quantity]]],
                ],
            ],
        ],
    ],
];

$params = [
    'index' => 'my_model_index',
    'body' => [
        'query' => $query,
    ],
];

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

相关问题