首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Elastica多项搜索未返回正确结果

Elastica多项搜索未返回正确结果
EN

Stack Overflow用户
提问于 2013-07-15 01:09:00
回答 1查看 1K关注 0票数 2

我有使用php的代码,使用elastica来执行产品搜索。当我选择产品类别为"off_furniture“和"home_furniture”时,elasticsearch只返回"home_category“类别的产品。

请给我来点灯。下面是我的代码:

代码语言:javascript
复制
$value = $_GET['prod'];
$filter_manufacturer = $_GET['man'];
$filter_price = $_GET['price'];
$cat = $_GET['cat'];

$queryString = new Elastica_Query_QueryString((string)$value);
$queryString->setDefaultOperator('OR')
->setFields(array('name'));

$category = explode("|", $cat);
$elasticaFilterBool = new Elastica_Filter_Bool();  
$filter2 = new Elastica_Filter_Term();
$filter2->setTerm('prodcat', array('off_furniture','home_furniture'));   

$elasticaFilterBool->addMust($filter2);
$query->setFilter($elasticaFilterBool);


// Create the search object and inject the client
$search = new Elastica_Search(new Elastica_Client());

// Configure and execute the search
$resultSet = $search->addIndex('products3')
                ->addType('product3')
                ->search($query);

foreach ($resultSet as $elasticaResult) {
            $result = $elasticaResult->getData();
            echo $result["name"]. "|";
    echo $result["prodcat"]. "|";
            echo $result["description"]. "|";
            echo $result["price"]. "|";
            echo $result["manufacturer"]. "|@";
        }    
EN

回答 1

Stack Overflow用户

发布于 2013-07-15 11:55:29

我可以看到一些潜在的问题:

  1. 您需要使用terms过滤器,而不是term过滤器。后者只接受一个要对其进行筛选的术语,但您要将两个项发送给构造函数,即["off_furniture", "home_furniture"]
  2. 如果只有一个过滤器,则不需要将terms过滤器包装在bool过滤器中!
  3. 从这段代码中我看不出来,但是prodcat字段的映射需要是{"type": "string", "index": "not_analyzed"},否则标记器很可能会将短语'home_furniture'分为两个标记-- homefurniture --而过滤器将不能正常工作。如果没有显式指定映射,则需要这样做。在elasticsearch上发送字符串将自动应用标准分析器。

试试这个:

代码语言:javascript
复制
$prodcatFilter = new Elastica_Filter_Terms('prodcat', array('off_furniture', 'home_furniture'));
$query->setFilter($prodcatFilter);

祝好运!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17645396

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档