我有使用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"]. "|@";
} 发布于 2013-07-15 11:55:29
我可以看到一些潜在的问题:
terms过滤器,而不是term过滤器。后者只接受一个要对其进行筛选的术语,但您要将两个项发送给构造函数,即["off_furniture", "home_furniture"]。terms过滤器包装在bool过滤器中!prodcat字段的映射需要是{"type": "string", "index": "not_analyzed"},否则标记器很可能会将短语'home_furniture'分为两个标记-- home和furniture --而过滤器将不能正常工作。如果没有显式指定映射,则需要这样做。在elasticsearch上发送字符串将自动应用标准分析器。试试这个:
$prodcatFilter = new Elastica_Filter_Terms('prodcat', array('off_furniture', 'home_furniture'));
$query->setFilter($prodcatFilter);祝好运!
https://stackoverflow.com/questions/17645396
复制相似问题