英文:
Elasticsearch query multidimensional array values
问题
我有以下这些映射:
'indices' => [
'mappings' => [
'default' => [
'properties' => [
'id' => [
'type' => 'keyword',
],
'name' => [
'type' => 'text',
'analyzer' => 'english',
],
],
],
],
],
我需要存储字段 stocks
数组对象
stock
的结构如下:
{stock_id: uint, qty: uint, hidden: bool}
然后,我需要使用搜索条件查询这个索引,条件是 name
包含在 [1, 2, 3] 中,并且 qty
大于 0,hidden
不等于 true。
我该如何实现这个功能?是否需要更改映射设置?
顺便提一下,我正在使用 Laravel Scout,底层使用 Elastic Engine。但如果您能展示原始查询,对我也会很有帮助。
英文:
I have these mappings:
'indices' => [
'mappings' => [
'default' => [
'properties' => [
'id' => [
'type' => 'keyword',
],
'name' => [
'type' => 'text',
'analyzer' => 'english',
],
],
],
],
I need to store field stocks
arr[obj]
stock looks like:
{stock_id:uint, qty:uint, hidden: bool}
Then I need to query this index with search of name
where stocks.stock_id IN [1,2,3] and qty>0 and hidden != true
How I can implement this - does I need to change something in mapping settings?
Btw im using laravel scout with elastic engine under the hood. But your answer will helpful if you show me just raw query
答案1
得分: 2
尝试这个:
$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);
英文:
Try this
$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);
For multidimensional array use nested
$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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论