2016-08-19 155 views
-1
我使用 PHP官方驅動程序連接

後拉最新數據Elasticsearch(V 2.3),每當我索引的新文件才能60秒是能夠將其納入我的過濾結果中。我怎樣才能減少延遲時間爲零?Elasticsearch搜索延遲從<strong>5秒</strong>索引

這裏是我的索引查詢

 # 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); 

謝謝。

回答

1

當您索引新文檔時,您可以指定refresh參數,以使新文檔立即可用於您的下一個搜索操作。

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

refresh參數也可在bulk操作,如果你正在使用它。

但請注意,刷新過於頻繁的可能會有性能上的negative impacts

+0

儘管添加'refresh'=> true,但延遲仍然存在@Val –

+0

您可以顯示一些代碼來說明您在做什麼嗎? – Val

+0

我編輯了問題以包含代碼示例。 @Val –

0

提供了一個刷新選項,它需要一個值(以秒爲單位)來刷新索引。例如,如果您更新索引中的某些內容,它將被寫入索引中,但尚未準備好讀取,直到索引刷新。

只要發生任何更改,刷新就可以設置爲true以刷新索引。這需要非常仔細地考慮,因爲很多時候,它會降低您的性能,因爲它是對每個小操作進行刷新的矯枉過正,再加上許多批量刷新可能會使索引繁忙。

提示:使用elasticsearch插件(例如kopf)並查看更多此類選項(如刷新率)來配置。