Setting Up Params for Multiple Match: Array Problem


(John Vargo) #1

Hey, I'm having a newbie problem for sure.

I'm looking to do a direct match 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.

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.


(John Vargo) #2

Answered my own question, leaving here for posterity. You need to nest each match clause 1 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']
                    ]
                ]
            ]
            ]
        ]
    ]
]
];

(system) #3