搜索过滤器在 Elastic Search PHP 中不起作用

通过以下代码存储了一个新索引。


$data = array(

    array("Name"=>"Norman","Email"=>"in.dolor@vulputatemauris.ca","Created"=>"2019-12-29 10:28:03","Modified"=>"2020-11-07 01:45:23"),

    array("Name"=>"Drake","Email"=>"posuere@sedorcilobortis.co.uk","Created"=>"2020-11-08 14:37:00","Modified"=>"2019-08-10 06:42:07"),

    array("Name"=>"Wynne","Email"=>"ligula.Donec@adipiscingenim.net","Created"=>"2019-05-19 23:30:42","Modified"=>"2019-06-09 08:13:58"),

    array("Name"=>"Kirsten","Email"=>"lobortis@Suspendisseeleifend.net","Created"=>"2020-01-09 23:34:19","Modified"=>"2020-04-16 10:23:07"),

    array("Name"=>"Ainsley","Email"=>"elit.dictum.eu@Quisquelibero.org","Created"=>"2019-01-22 18:14:39","Modified"=>"2019-09-02 18:44:30"),

    array("Name"=>"Walker","Email"=>"ullamcorper@luctussitamet.org","Created"=>"2020-11-05 23:04:46","Modified"=>"2020-01-03 09:29:36"),

    array("Name"=>"Evelyn","Email"=>"amet.metus.Aliquam@dui.org","Created"=>"2020-06-28 13:23:09","Modified"=>"2019-04-02 05:41:33"),

    array("Name"=>"James","Email"=>"amet.risus@nullaCras.net","Created"=>"2020-04-20 10:15:54","Modified"=>"2020-07-22 12:04:49"),

    array("Name"=>"Melvin","Email"=>"nec.eleifend.non@elit.edu","Created"=>"2020-03-07 05:19:53","Modified"=>"2018-12-30 19:33:29"),

);


$hosts = ['http://localhost:9200'];

$client = ClientBuilder::create()->setHosts($hosts)->build();


$params = [

    'index' => 'dummy_data',

    'id'    => 'my_id',

    'body'  => ['data' => $data]

];


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

尝试通过以下方式搜索第一条记录:


$params = [

    'index' => 'dummy_data',

    'body'  => [

        'query' => [

            'bool' => [

                'must' => [

                    [ 'match' => [ 'data.Name' => 'Norman' ] ],

                ],

            ]

        ]

    ]

];


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

问题是它返回查询仅与第一个匹配的所有记录。


请帮助解决这个问题。


胡子哥哥
浏览 76回答 1
1回答

胡说叔叔

我猜你正在使用官方的 Elasticsearch-PHP 库。在这种情况下,您似乎将所有提供的数据索引为 1 个文档,其中 id 为“my_id”。这就是为什么您在搜索匹配时获取整个数据集的原因。如果您想同时索引多个文档,您应该查看bulk_index端点。以下是官方示例供参考:for($i = 0; $i < 100; $i++) {&nbsp; &nbsp; $params['body'][] = [&nbsp; &nbsp; &nbsp; &nbsp; 'index' => [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '_index' => 'my_index',&nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; ];&nbsp; &nbsp; $params['body'][] = [&nbsp; &nbsp; &nbsp; &nbsp; 'my_field'&nbsp; &nbsp; &nbsp;=> 'my_value',&nbsp; &nbsp; &nbsp; &nbsp; 'second_field' => 'some more values'&nbsp; &nbsp; ];}$responses = $client->bulk($params);
打开App,查看更多内容
随时随地看视频慕课网APP