0

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.

2
  • I think you should lock here php.net/manual/en/function.array-merge-recursive.php hope this will help you Commented Nov 12, 2015 at 18:13
  • 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. Commented Nov 12, 2015 at 18:21

1 Answer 1

5

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

];

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.