2015-08-28 66 views
0

我想排序一些數據,在我的基本框架我的排序不工作,如果我刪除排序它工作正常。如何結合布爾必須和排序elasticsearch

那麼我怎麼能把排序在我的基本骨架和排序一些數據。

我不能把剛

$params['body'] = [   
    'sort' => [['title' => ['order' => 'asc']]]]; 
$results = $client->search($params); 

,因爲我有其他的條件,我需要的必備條件。 任何人都可以知道如何解決。

任何建議將非常感激。

 // my base skeleton   
     $params = array(
       'index' => "myIndex", 
       'type' => "myType", 
       'body' => array(
        'query' => array(
         'bool' => array(
          'must' => array(
          // empty should clause for starters 
          ) 
         ) 
        ), 
       'sort' => array() 
       ) 
      ); 

    // sorting is not working with bool and must 
    if ($request->request->get('salarySort')) { 
       $params['body']['query']['bool']['must'][] = array(
        'sort' => array(
         "title" => array('order' => 'asc') 
        ) 
       ); 
      } 

這是我得到的json_encode ---

{ 
    "took": 4, 
    "timed_out": false, 
    "_shards": { 
    "total": 2, 
    "successful": 1, 
    "failed": 0 
    }, 
    "hits": { 
    "total": 1066, 
    "max_score": null, 
    "hits": [ 
     { 
     "_index": "myIndex", 
     "_type": "myType", 
     "_id": "pe065319de73937aa6ef46413afd7aac26a58a611", 
     "_score": null, 
     "_source": { 
      "title": "Smarason trycker ", 
      "content": "HIF gör 2-0 mot Halmstad.", 
      "tag": [ 
      "Soprts" 
      ], 
      "category": [ 
      "Sports" 
      ] 
     }, 
     "sort": [ 
      "0" 
     ] 
     }, 
     { 
     "_index": "myIndex", 
     "_type": "myType", 
     "_id": "pebc44a70008f53f74f23ab23f8a1f79b2b729448", 
     "_score": null, 
     "_source": { 
      "title": "Anders Svenssons tips gav 1-0", 
      "content": "Anders Svenssons tips i halvtid Kalmar FF.", 
      "source": "Unknown", 
      "tag": [ 
      "Soprts" 
      ], 
      "category": [ 
      "Sports" 
      ] 
     }, 
     "sort": [ 
      "0" 
     ] 
     } 
    ] 
    } 
} 

查詢在JSON ---

{ 
    "index": "myIndex", 
    "type": "myType", 
    "size": 30, 
    "body": { 
    "query": { 
     "match_all": [] 
    }, 
    "sort": [ 
     { 
     "title": "asc" 
     } 
    ] 
    } 
} 

回答

1

你幾乎沒有。您已將空sort陣列正確放置在與query相同的級別,這是正確的。

當您嘗試將其作爲bool/must約束而不是在空的sort陣列中輸入時,會出現此問題。

// sorting is not working with bool and must 
if ($request->request->get('salarySort')) { 
    $params['body']['sort'][] = array(  <---- this line needs to be changed 
     "Salary" => 'asc'     <---- this line needs to be changed, too 
    ); 
} 
+0

非常感謝您的回答,但它不排序,因爲它應該能不能請再次檢查一次,非常感謝您的回答:) –

+0

我的壞,我已經編輯我的答案 – Val

+0

@感謝你的回答val :)很多,雖然它不工作,不排序文檔:) –