【问题标题】:Working of filters with elastica使用弹性过滤器的工作
【发布时间】:2017-05-17 12:04:22
【问题描述】:

如何在与弹性搜索一起使用的弹性库中应用过滤器。

需要一个术语过滤器的示例。

$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

【问题讨论】:

    标签: php elasticsearch elastica


    【解决方案1】:

    过滤器已弃用。在过滤器上下文中使用查询。

    例如:

    {
    "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() 方法设置过滤器。

    【讨论】:

    • 这是使用弹性搜索的过滤器。如何使用 elastica 库放入过滤器。
    【解决方案2】:

    ruflin/elastica v 7.0.1:

    $bool = new Elastica\Query\BoolQuery();
    $filter = new Elastica\Query\Term(['tagType' => 'value']);
    $bool->addFilter($filter);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-23
      • 1970-01-01
      • 2019-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多