0

I have an index with two different types :

    [hits] => Array
        (
            [total] => 408863
            [max_score] => 1
            [hits] => Array
                (
                    [0] => Array
                        (
                            [_index] => myindex
                            [_type] => merch
                            [_id] => 379919
                            [_score] => 1
                            [_source] => Array
                                (
                                    [id] => 379919
                                    [field1] =>  Lorem ipsum
                                    [field2] => ERKDK56
                                    [field3] => 1256
...


    [hits] => Array
        (
            [total] => 4172386
            [max_score] => 1
            [hits] => Array
                (
                    [0] => Array
                        (
                            [_index] => myindex
                            [_type] => merchSeller
                            [_id] => 2599218
                            [_score] => 1
                            [_source] => Array
                                (
                                    [id] => 2599218
                                    [field4] => 1
                                    [field5] => 1
                                    [merch] => Array
                                        (
                                            [id] => 132539
                                            [field6] => 132539
                                        )
                                    [seller] => Array
                                        (
                                            [id] => 689
                                            [field7] => 1
...

How can I create a query that can check the fields of both types?

I created a query by adding both type in the type field hoping I wil have access to there fields but it does not return good result.

$params = [            
    'index' => 'myindex',
    'type' => 'merch,merchSeller',
    'body' => [
    'query' => [
        'bool' => [
            'must'=> [
                'match' => ['field1' => 'Lorem'] ,
            ],
            'must'=> [
                'match' => ['field4' => '23'],
            ]
        ]
    ]]
];

I'm using laravel with "elasticsearch/elasticsearch": "~7.1".

Thank you

  • Does this answer your question? [elasticsearch bool query combine must with OR](https://stackoverflow.com/questions/28538760/elasticsearch-bool-query-combine-must-with-or) – FredvN Dec 01 '22 at 19:14
  • Thanks but no. I'm looking for a way to get data from an index that has two type each with different fields. – ventomachine45 Dec 02 '22 at 12:52

1 Answers1

0

Selecting records with _type equals to merc and match on field1 or records with _type equals to merchSeller and match on field4

  "query": {
        "bool" :  {
          "should": [ 
            {
              "bool": {
                "must": [ {
                  "term":{"_type":"merch"}
                },
                {
                  "term":{"field1" : "Lorem ipsum"}
                }
                  ]
              }
            },
            {
              "bool": {
                "must": [ {
                  "term":{"_type":"merchSeller"}
                },
                {
                  "term":{"field4" : "23"}
                }
                  ]
              }
              
            }

          ]
        }

}

Be aware that _type is deprecated.

FredvN
  • 504
  • 1
  • 3
  • 14