Elasticsearch:为 PHP 数组中的多个匹配设置参数

Joh*_*rgo 0 php arrays elasticsearch

我正在寻找使用 boost 来调整相关性的多个关键字的直接匹配弹性搜索。为此,我正在使用 PHP 客户端。我很难纠正我有多个同名数组索引的事实。

$params = [
'index' => 'people',
'type' => 'person',
'body' => [
    'query' => [
        'bool' => [
            'should' => [
                'match' => [
                    'Company1' => [
                        'query' => $order['BillTo_Name'],
                        'boost' => $_GET['company-name-weight']
                    ]
                ],
                'match' => [
                    'Address1' => [
                        'query' => $order['ShipTo_Addr1'],
                        'boost' => $_GET['address-weight']
                    ]
                ],
                'match' => [
                    'PostalCode' => [
                        'query' => $order['ShipTo_Zip'],
                        'boost' => $_GET['company-name-weight']
                    ]
                ],
                'match' => [
                    'LastName' => [
                        'query' => $order['contact'],
                        'boost' => $_GET['name-weight']
                    ]
                ]
            ]
        ]
    ]
]
];
Run Code Online (Sandbox Code Playgroud)

这会导致我的数组折叠为单个匹配条件,因为我有 4 行具有相同的数组索引 $params[body][query][bool][should][match]。

var_dump($params);

array(3) {
  ["index"]=>
  string(5) "leads"
  ["type"]=>
  string(4) "lead"
  ["body"]=>
  array(1) {
    ["query"]=>
    array(1) {
      ["bool"]=>
      array(1) {
        ["should"]=>
        array(1) {
          ["match"]=>
          array(1) {
            ["LastName"]=>
            array(2) {
              ["query"]=>
              string(10) "Parker"
              ["boost"]=>
              string(1) "1"
        }
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我不知道如何使用 4 个单独的匹配项重新构造此查询,并以 PHP 客户端可以消化的方式为每个匹配项指定 boost。

Joh*_*rgo 5

我想在这里发帖就是我结束痛苦所需要的一切。发在这里供后人使用。解决方案是将每个匹配子句嵌套更深一个数组。

$params = [
'index' => 'leads',
'type' => 'lead',
'body' => [
    'query' => [
        'bool' => [
            'should' => [
            [
                'match' => [
                    'Company1' => [
                        'query' => $order['BillTo_Name'],
                        'boost' => $_GET['company-name-weight']
                    ]
                ]
            ],[
                'match' => [
                    'Address1' => [
                        'query' => $order['ShipTo_Addr1'],
                        'boost' => $_GET['address-weight']
                    ]
                ]
            ],[
                'match' => [
                    'PostalCode' => [
                        'query' => $order['ShipTo_Zip'],
                        'boost' => $_GET['company-name-weight']
                    ]
                ]
            ],[
                'match' => [
                    'LastName' => [
                        'query' => $order['contact'],
                        'boost' => $_GET['name-weight']
                    ]
                ]
            ]
            ]
        ]
    ]
]
Run Code Online (Sandbox Code Playgroud)

];