使用弹性过滤器的工作

时间:2017-01-03 11:45:34

标签: php elasticsearch 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');

我正在尝试使用导致跟随错误的上述方法

  

参数无效。必须是数组或实例   弹性曲线\查询\ AbstractQuery

1 个答案:

答案 0 :(得分:0)

不推荐使用过滤器。在过滤器上下文中使用查询。

例如:

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

修改: @Sattu我也在使用Elastica。这取决于你拥有的Elastica版本。如果你有v3.2.3,你必须使用原始查询。像这样:

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

但是如果你有Elastica v5,你可以使用Elastica \ Query类并通过addFilter()方法设置过滤器。