Elasticsearch查询多维数组数值

huangapple go评论126阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年3月9日 18:34:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75683395.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定