2014-06-21 23 views
1

我試圖使用FOSElastica Bundle與全局索引搜索一起返回精彩集錦。FOSElastica包:檢索結果的精彩部分

我有我的配置(YML文件)全球指數取景器:

fos_elastica: 
    clients: 
    default: { host: %elastic_host%, port: %elastic_port% } 
    indexes: 
    myIndex: 
     client: default 
     finder: ~ 
     types: 
     # different types here 

,我用它作爲每DOC(here):

$finder = $this->container->get('fos_elastica.finder.myIndex'); 

// Returns a mixed array of any objects mapped 
$results = $finder->find('whatever'); 

完美的作品,並返回預期成績。 現在我想突出使用例如ES的快速向量熒光筆結果中找到的單詞。但我沒有找到任何示例或任何文檔來這樣做。

我想我需要定義的東西,如一個更合適的\查詢對象:

$query = new \Elastica\Query(); 
$query->setHighlights(array("whatever")); 
$query->setTerm("whatever"); 

$results = $finder->find($query); 

但我無法找到任何信息。任何提示?

非常感謝!

回答

5

第一寫JSON查詢:

{ 
    "query" : { 
     "match" : { 
      "content" : "this is a test" 
     } 
    }, 
    "highlight" : { 
     "fields" : { 
      "content" : {} 
     } 
    } 
} 

當它的作品,翻譯成彈性曲線:

$matchQuery = new \Elastica\Query\Match(); 
$matchQuery->setField('content', 'this is a test'); 

$searchQuery = new \Elastica\Query(); 
$searchQuery->setQuery($matchQuery); 

$searchQuery->setHighlight(array(
    "fields" => array("content" => new \stdObject()) 
)); 
+2

謝謝Damien!實際上FOSElasticaBundle文檔是非常小的,所以我不得不在源代碼中查找我手頭有哪些方法來正確地轉換我的Elasticsearch查詢。我結束了https://gist.github.com/tchapi/1ac99f757e0f336c1e1b哪效果很好。儘管你的提示寫第一手的查詢幫助了我;) – tchap