2017-01-03 98 views
0

如何在elastica庫中應用彈性搜索過濾器。彈性過濾器的工作

需要一個術語過濾器的例子。

$query1 = new \Elastica\Filter\Term(); 
$query1->setTerm('categories', array('short_description' =>'test metal')); 
$bool->addShould($query); 

$query1 = new \Elastica\Filter\Term(); 
$query1->setTerm('categories', 'test metal'); 

我嘗試使用上面這導致以下錯誤

無效參數的方法。必須是數組或實例 Elastica \ Query \ AbstractQuery

回答

0

過濾器已被棄用。在篩選上下文中使用查詢。

例如:

{ 
"query": { 
"bool": { 
    "must": [ 
    { "match": { "title": "Search"  }}, 
    { "match": { "content": "Elasticsearch" }} 
    ], 
    "filter": [ 
    { "term": { "status": "published" }}, 
    { "range": { "publish_date": { "gte": "2015-01-01" }}} 
    ] 
} 
} 
} 

編輯: @Sattu我使用的彈性曲線了。這取決於你擁有哪個版本的Elastica。如果你有v3.2.3,你必須使用原始查詢。事情是這樣的:

$query = [ 
'query' => [ 
    'bool' => [ 
    'filter' => [ 
     'term => [$field => $value] 
    ] 
    ] 
    ] 
]; 
$index = $client->getIndex('products); 
$type = $index->getType('product'); 
$type->search($query); 

但如果你有彈性曲線V5則可以使用彈性曲線\查詢類,並設置過濾器通過addFilter()方法。

+0

這是使用彈性搜索的過濾器。我如何使用elastica庫過濾器。 – Sattu