duankange2433 2015-11-12 18:08
浏览 55

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

I'm looking to do a direct match elasticsearch on multiple keywords using boost to adjust relevance. I'm using the PHP client for this. I'm having difficulty rectifying the fact that I have multiple array indices with the same name.

$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']
                    ]
                ]
            ]
        ]
    ]
]
];

This causes my array to collapse down to a single matching criteria, because I have 4 rows with the same array index $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"
        }
          }
        }
      }
    }
  }
}

I'm at a loss how to restructure this query with 4 separate matches, specifying boost for each, in a way that the PHP client can digest.

  • 写回答

1条回答 默认 最新

  • dqxboe2628 2015-11-12 18:39
    关注

    I guess posting here was all I needed to end my misery. Posting here for posterity. The solution is to nest each match clause one array deeper.

    $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']
                        ]
                    ]
                ]
                ]
            ]
        ]
    ]
    

    ];

    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数