Elasticsearch搜索延迟了索引后提取最新数据的时间


Elasticsearch search delays pulling latest data after indexing

我使用官方PHP驱动程序连接到Elasticsearch(v 2.3(,每次索引新文档时,都需要从5秒60秒

这是我的索引查询

        # Document Body
        $data                    = [];
        $data['time']           = $time;
        $data['unique']         = 1;
        $data['lastACtivity']   = $time;
        $data['bucket']      = 20,
        $data['permission']     = $this->_user->permission; # Extracts User Permission
        $data['ipaddress']      = $this->_client->ipaddress(); # Extracts User IP Address
        # Construct Index
        $indexRequest = [
            'index'     => 'gorocket',
            'type'      => 'log',
            'refresh'   => true,
            'body'      => $data  
        ];
        # Indexing Document
        $confirmation = $client->index( $indexRequest );

这是我的搜索过滤器查询

# Query array
        $query =[ 'query' => [
                        'filtered' => [
                            'filter' => [
                                'bool' => [
                                            'must' =>[
                                                [
                                                    'match' => [ 'unique' => 1 ]
                                                ],
                                                [
                                                    'range' => [
                                                            'lastACtivity' => [
                                                                'gte'   => $from,
                                                                'lte'   => $to
                                                            ],
                                                            '_cache' => false
                                                    ]
                                                ]
                                            ],
                                            'must_not' => [
                                                [ 'match' => [ 'type' => 'share' ] ],
                                            ]
                                        ]
                            ]
                        ]
                    ]
                ];
        # Prepare filter parameters
        $filterParams = [
            'index'     => 'gorocket',
            'type'      => 'log',
            'size'      => 20,
            'query_cache' => false,
            'body'      => $query
        ];
        $client->search($filterParams);

谢谢

为新文档编制索引时,可以指定refresh参数,以便使新文档立即可用于下一次搜索操作。

$params = [
    'index' => 'my-index',
    'type' => 'my-type',
    'id' => 123,
    'refresh' => true               <--- add this
];
$response = $client->index($params);

如果您正在使用bulk操作,refresh参数也可用。

不过,请注意,过于频繁地刷新可能会对性能产生负面影响。

提供了一个刷新选项,需要一个值(以秒为单位(来刷新索引。例如,如果更新了索引中的某个内容,它会被写入索引中,但在刷新索引之前还没有准备好读取。

Refresh可以设置为true,以便在发生任何更改时立即刷新索引。这需要非常仔细地考虑,因为很多时候,它会降低您的性能,因为对每个小操作进行刷新都是过度的,再加上多次大容量刷新会使索引繁忙。

提示:使用弹性搜索插件,如kopf,并查看刷新率等更多选项进行配置。