2014-09-22 66 views
2

我有一個名爲publications_items的索引和一個名爲「publication」的類型。ElasticSearch PHP - 獲取類型/索引中的10個(最近的)項目的列表

我想最近添加10個出版物。 (一些matchAll原則)

我使用ElasticSearch PHP(http://www.elasticsearch.org/guide/en/elasticsearch/client/php-api/current/_quickstart.html

基本上我只是做了默認拿到,但我不知道如何在ElasticSearchPHP做到這一點。

在sense.qbox.io我做的:

POST /publications_items/_search { "query": { 
     "match_all": {} } } 

,它工作正常

映射:

PUT /publications_items/ { "mappings": { 
     "publication": { 
     "properties": { 
      "title": { 
       "type": "string" 
      }, 
      "url": { 
       "type": "string" 
      }, 
      "description": { 
       "type": "string" 
      }, 
      "year": { 
       "type": "integer" 
      }, 
      "author": { 
       "type": "string" 
      } 
     } 
     } } } 
+0

任何運氣,纔有可能看到你的映射? – eliasah 2014-09-22 14:54:23

+0

對不起,格式問題 – Paul 2014-09-22 14:57:32

回答

4

您需要to enable "_timestamp" mapping

PUT /test/doc/_mapping 
{ 
    "_timestamp": { 
    "enabled": "true", 
    "store": "true" 
    } 
} 

而且在你的

GET /test/_search 
{ 
    "sort" : { 
    "_timestamp" : { "order" : "desc" } 
    }, 
    "from" : 0, "size" : 10 
} 
在Elasticsearch PHP

而具體:你需要to sort by it和檢索first 10 documents搜索查詢

  • 映射關係發生變化:
require 'vendor/autoload.php'; 

$client = new Elasticsearch\Client(); 
$params = array(); 

$params2 = [ 
     '_timestamp' => [ 
      'enabled' => 'true', 
      'store' => 'true' 
     ] 
]; 
$params['index']='test'; 
$params['type']='doc'; 
$params['body']['doc']=$params2; 
$client->indices()->putMapping($params); 
  • 查詢:
require 'vendor/autoload.php'; 

$client = new Elasticsearch\Client(); 

$json = '{ 
    "sort" : { 
    "_timestamp" : { "order" : "desc" } 
    }, 
    "from" : 0, "size" : 10 
}'; 

$params['index'] = 'test'; 
$params['type'] = 'doc'; 
$params['body'] = $json; 

$results = $client->search($params); 
echo json_encode($results, JSON_PRETTY_PRINT); 
+0

乾杯人,我現在明白了 – Paul 2014-09-23 07:35:58

相關問題