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

时间:2013-07-15 01:09:00

标签: elasticsearch elastica

我有使用elastica的php代码来执行产品搜索。当我选择产品类别为“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"]. "|@";
        }    

1 个答案:

答案 0 :(得分:1)

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

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

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

  3. 我无法单独使用此代码,但prodcat字段的映射需要为{"type": "string", "index": "not_analyzed"},否则令牌生成器很可能会将短语'home_furniture'拆分为两个令牌homefurniture,您将无法正常使用过滤器。如果未明确指定映射,则需要执行此操作。在elasticsearch发送字符串将自动应用标准分析器。

  4. 试试这个:

    $prodcatFilter = new Elastica_Filter_Terms('prodcat', array('off_furniture', 'home_furniture'));
    $query->setFilter($prodcatFilter);
    
    祝你好运!

相关问题