HelperMC::strToUtf8(trim($name)), "urln-6ren">
gpt4 book ai didi

php - Elasticsearch搜索不起作用

转载 作者:行者123 更新时间:2023-12-03 00:23:32 24 4
gpt4 key购买 nike

我添加数据,然后在Elasticsearch中添加以下代码:

$data = array("name"=>HelperMC::strToUtf8(trim($name)),
"urlname"=>HelperURL::strToURL(trim($name)),
"price"=>number_format(HelperParser::clearTextFromPrice($price), 2, ',', '.'),
"price_for_filter"=>HelperParser::clearTextFromPrice($price),
"image"=>$imgname,
"description"=>HelperMC::strToUtf8($description),
"keywords"=>HelperMC::strToUtf8($keywords),
"url"=>$k['url'],
"sitemap_id"=>intval($sitemapId),
"shop_id"=>$shop['shop_id'],
"logo"=>$shop['logo'],
"key"=>$hashKey,
"type"=>intval(1),
"shop_rating_count"=>intval($shop['rating_count']),
"shop_rating_point"=>intval($shop['rating_point']),
"created_at"=>date("Y-m-d H:i:s"),
"updated_at"=>date("Y-m-d H:i:s"));

//create elasticsearch index
HelperES::insertEntry(json_encode($data),$hashKey);

我的insertEntry函数:
private static $elasticBase = "http://localhost:9200/shopping/items";

public static function insertEntry($data_string,$id = false){
if($id)
$url = self::$elasticBase."/".$id;
else
$url = self::$elasticBase;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL , $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
@$result = curl_exec($ch);
curl_close($ch);

return $result;
}

和我的搜索功能:
public static function search($keyword, $defaultOperator = "OR" ,$start = 0, $size = 25, $from = 0, $to = 10000, $shopsArr = array(),$orderBy = "price", $order = "asc"){
$searchUrl = self::$elasticBase."/_search";

if(count($shopsArr) > 0){
$query = '{
"from" : '.$start.', "size" : '.$size.',
"query" : {
"filtered" : {
"query": {
"query_string": { "query": "'.$keyword.'",
"fields": ["name","urlname"],
"default_operator": "'.$defaultOperator.'"
}
},
"filter" : {
"bool" : {
"must" : {
"terms" : { "sitemap_id" : '.json_encode($shopsArr).' }
},
"must" : {
"range" : {
"price_for_filter" : {
"from" : "'.$from.'",
"to" : "'.$to.'"
}
}
}
}
}
}
},
"sort" : [
{"'.$orderBy.'" : {"order" : "'.$order.'", "mode" : "avg"}}
]
}';
}else{
$query = '{
"from" : '.$start.', "size" : '.$size.',
"query" : {
"filtered" : {
"query": {
"query_string": {
"query": "'.$keyword.'",
"fields": ["name","urlname"],
"default_operator": "'.$defaultOperator.'"
}
},
"filter" : {
"range" : {
"price_for_filter" : {
"from" : "'.$from.'",
"to" : "'.$to.'"
}
}
}
}
},
"sort" : [
{"'.$orderBy.'" : {"order" : "'.$order.'", "mode" : "avg"}}
]
}';
}

echo $query;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL , $searchUrl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
@$result = curl_exec($ch);
curl_close($ch);
return $result;
}

但是我的搜索结果不起作用。

示例: http://azdanaz.com:9200/shopping/items/_search?q=name:telefon&pretty

工作,但是

http://www.azdanaz.com/arama/telefon

不工作。

最佳答案

查看shopping索引的映射,它表明price_for_filter字段实际上是字符串,而不是数字类型。因此,您的范围过滤器正在执行字符串比较,而不是数字比较。这正在过滤掉所有结果。

为了说明这一点,您的范围从“0”到“100000”进行过滤。您的第一个预期结果的price_for_filter值为“12.97”。在进行字符串比较时,“12.97”大于“0”,但是,作为字符串,它也大于“100000”,因此该结果将被滤除(“12”>“10”)。

您要么需要将price_for_filter字段更改为数字字段,要么需要添加一个新的数字类型字段,并更改范围过滤器以使用该新字段。

关于php - Elasticsearch搜索不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28446589/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com