0
votes

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
I think you should lock here php.net/manual/en/function.array-merge-recursive.php hope this will help youLaurentiu
Thanks for the reply, but I don't see how this could help me. I'm not going to get around the PHP restriction on array indices, so I'll need to restructure my Elasticsearch query in order to conform.John Vargo

1 Answers

5
votes

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

];