【问题标题】:Elasticsearch and PHP. Combine match and multi_match in one query弹性搜索和 PHP。在一个查询中结合 match 和 multi_match
【发布时间】:2016-10-28 18:32:35
【问题描述】:

我有三个字段 - 一个整数类型 (field1) 和两个十进制类型 (field2, field3)。我希望能够按所有字段进行查询。这些单独的查询在我的情况下工作得很好:

$params = [
    'index' => 'test_index',
    'type' => 'text_index_type',
    'body' => [
        'query' => [
            'match' => [
                'field1' => '12'
            ]
        ]
    ]
];

这个查询效果很好:

$params = [
    'index' => 'test_index',
    'type' => 'text_index_type',
    'body' => [
        'query' => [
            'multi_match' => [
                'query' => '345',
                'fields' => ['field2', 'field3']
            ]
        ]
    ]
];

但是,如果我将它们结合起来:

$params = [
    'index' => 'test_index',
    'type' => 'text_index_type',
    'body' => [
        'query' => [
            'match' => [
                'field1' => '12'
            ],
           'multi_match' => [
                'query' => '345',
                'fields' => ['field2', 'field3']
            ]
        ]
    ]

];

我收到一个错误:

未捕获的异常 'Elasticsearch\Common\Exceptions\BadRequest400Exception' ... [match] 格式错误的查询,发现意外的 [FIELD_NAME] [multi_match]

那么,这有什么问题,我该如何解决?

PS。就 SQL 而言,这就是我想要实现的目标:

 SELECT * FROM mytable where field1 = 12 or field2 = 345 or field3 = 345

【问题讨论】:

  • 顺便说一下,这是一个简化的查询。在现实世界中我会有更多的领域,所以我真的需要使用multi_match

标签: php elasticsearch


【解决方案1】:

您可以将它们与bool queries结合起来

$params = [
    'index' => 'test_index',
    'type' => 'test_index_type',
    'body' => [
        'query' => [
            'bool' => [
                'should' => [
                    [ 'match' => [ 'field1' => '12' ] ],
                    [ 'multi_match' => [ 'query' => '345',
                                         'fields' => ['field2', 'field3']] ],
                ]
            ]
        ]
    ]
];

应该等于“OR”,而必须等于“AND”

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多