【问题标题】:Elastica multiple term search not returning the correct resultElastica 多词搜索未返回正确结果
【发布时间】:2013-07-15 01:09:00
【问题描述】:

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

请给我一些启发。以下是我的代码:

$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"]. "|@";
        }    

【问题讨论】:

  • 您的问题解决了吗?想知道我是否能提供帮助!

标签: elasticsearch elastica


【解决方案1】:

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

  1. 您需要使用terms 过滤器而不是term 过滤器。后者只接受一个要过滤的术语,但您将两个发送给构造函数,即["off_furniture", "home_furniture"]

  2. 如果您只有一个过滤器,则无需将terms 过滤器包装在bool 过滤器中!

  3. 我无法仅从这段代码中看出,但 prodcat 字段的映射需要为 {"type": "string", "index": "not_analyzed"} 否则标记器很可能会将短语 'home_furniture' 拆分为两个标记 homefurniture,您将无法使过滤器正常工作。如果您没有明确指定映射,则需要这样做。在 elasticsearch 发送字符串会自动应用标准分析器。

试试这个:

$prodcatFilter = new Elastica_Filter_Terms('prodcat', array('off_furniture', 'home_furniture'));
$query->setFilter($prodcatFilter);

祝你好运!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多