c

4 years ago

ScooterSam

Code

use Elasticsearch\ClientBuilder;
use Elasticsearch\Common\Exceptions\Serializer\JsonErrorException;
use Elasticsearch\Common\Exceptions\ServerErrorResponseException;

$handler = ClientBuilder::defaultHandler();
$client = ClientBuilder::create()
    ->setHandler($handler)
    ->setHosts(config('services.search.hosts'))
    ->build();

$bool = [
    'match' => [
        'query' => [
            'multi_match' => [
                'query' => 'sam parton',
                'type' => 'cross_fields',
                'fields' => ['first_name', 'last_name'],
                'operator' => 'and',
            ],
        ],
        //'filter' => $validation_rules['filter'],
    ],
];

$params = [
    'index' => 'users',
    'type' => 'users',
    'body' => [
        'size' => 20,
        'query' => [
            //'match' => ['username' => 'sammeh']
            // 'multi_match' => [
            //     'query' => 'meh',
            //     'type' => 'best_fields',
            //     'fields' => ['first_name', 'username'],
            //     'operator' => 'and',
            // ],
            // 'simple_query_string' => [
            // 'query' => 'hb',
            // 'fields' => ['first_name', 'username'],
            // 'default_operator' => 'and',
            // ]

            'bool' => [
                'must' => [
                    'match' => [
                        'username' => [
                            'query' => 'had',
                            'fuzziness' => 2,
                            'minimum_should_match' => 1,
                        ],
                    ],
                    // 'match_phrase_prefix' => [
                    //     'username' => [
                    //         'query' => 'sam'
                    //     ],
                    // ],
                ],
                // 'filter' => [
                //     [
                //         "wildcard" => [
                //             "username" => [
                //                 'value' => 'sam' . '*',
                //                 "rewrite" => "constant_score",
                //             ],
                //         ],
                //         'wildcard' => [
                //             "first_name" => [
                //                 'value' => 'sam' . '*',
                //                 "rewrite" => "constant_score",
                //             ],
                //         ],
                //     ],
                // ],
            ],
        ],
    ],
];

$response = $client->search($params);

Output

=> Illuminate\Database\Eloquent\Collection {#1792
     all: [
       App\UserConnections {#1793
         id: 154508,
         user_id: 92493,
         connections_with_user_id: 95414,
         status: 1,
         view: 0,
         reject_reason: 0,
         message: "{"text": null, "media_id": null}",
         created_at: "2020-08-27 09:06:36",
         updated_at: "2020-10-09 07:17:25",
         is_couple_connection: 0,
       },
       App\UserConnections {#1807
         id: 154512,
         user_id: 92493,
         connections_with_user_id: 95414,
         status: 1,
         view: 0,
         reject_reason: 0,
         message: null,
         created_at: "2020-08-27 10:20:36",
         updated_at: "2020-08-27 10:20:36",
         is_couple_connection: 0,
       },
     ],
   }
New course: Desktop apps with Electron