0

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

rst630
  • 59
  • 2
  • 14

1 Answers1

2

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);
Mayuresh
  • 81
  • 5