Elasticsearch,是否可以查询具有两种不同类型索引,这两种类型具有不同的字段集?

k5hmc34c  于 2022-12-03  发布在  ElasticSearch
关注(0)|答案(1)|浏览(170)

我有一个两种不同类型的索引:

[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
...

如何创建一个可以同时检查两种类型字段的查询?

我通过在类型字段中添加两种类型来创建查询,希望我可以访问这些字段,但它没有返回好的结果。

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

我使用laravel与“ElasticSearch/ElasticSearch”:“~7.1”。
谢谢你

hivapdat

hivapdat1#

选择_type等于merc且匹配字段1的记录或_type等于merchSeller且匹配字段4的记录

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

          ]
        }

}
请注意,_type已被取代。

相关问题