我正在通過以下文章學習ElasticSearch - https://qbox.io/blog/using-elasticsearch-in-e-commerce-part-1文章。通過運行以下CURL命令創建了用於elasticsearch的索引&類型。如何在PHP的ElasticSearch查詢中使用SQL和條件?
curl -XPOST 'localhost:9200/ecomercedata/gadgets/_bulk?pretty' -d'
{ "index": { "_id": 1 }}
{ "name" : "MacBook Pro", "category" : "Laptop", "brand" : "Apple", "rating" : 9, "prize" : 1299.00, "piecesSold" : 9500, "dateOfRelease" : "2005-02-01"}
{ "index": { "_id": 2 }}
{"name" : "MacBook Air", "category" : "Laptop", "brand" : "Apple", "rating" : 8, "prize" : 1099.00, "piecesSold" : 8700, "dateOfRelease" : "2006-05-01"}
{ "index": { "_id": 3 }}
{"name" : "ATIV Book", "category" : "Laptop", "brand" : "Samsung", "rating" : 8, "prize" : 1899.00, "piecesSold" : 3500, "dateOfRelease" : "2014-05-01"}
{ "index": { "_id": 4 }}
{"name" : "Inspiron", "category" : "Laptop", "brand" : "Dell", "rating" : 6, "prize" : 700.00, "piecesSold" : 4600, "dateOfRelease" : "2008-03-01"}
{ "index": { "_id": 5 }}
{"name" : "Ipad", "category" : "Tablet", "brand" : "Apple", "rating" : 9, "prize" : 600.00, "piecesSold" : 9500 , "dateOfRelease" : "2005-07-01"}
{ "index": { "_id": 6 }}
{"name" : "Galaxy Tab", "category" : "Tablet", "brand" : "Samsung", "rating" : 8, "prize" : 550.00, "piecesSold" : 8500 , "dateOfRelease" : "2007-07-01"}
{ "index": { "_id": 7 }}
{"name" : "Lumia", "category" : "Mobile", "brand" : "Nokia", "rating" : 6, "prize" : 50.00, "piecesSold" : 12000 , "dateOfRelease" : "2009-03-01"}
{ "index": { "_id": 8 }}
{"name" : "Iphone", "category" : "Mobile", "brand" : "Apple", "rating" : 8, "prize" : 60.00, "piecesSold" : 28000 , "dateOfRelease" : "2002-03-01"}
{ "index": { "_id": 9 }}
{"name" : "Xperia", "category" : "Mobile", "brand" : "Sony", "rating" : 8, "prize" : 70.00, "piecesSold" : 24000 , "dateOfRelease" : "2004-03-01"}'
字段映射腳本used-
curl -X PUT "http://localhost:9200/ecomercedata/gadgets/_mapping" -d '{
"gadgets" : {
"properties" : {
"category" : {
"type" : "String",
"index" : "not_analyzed"
},
"brand" : {
"type" : "String",
"index" : "not_analyzed"
},
"name" : {
"type" : "String"
},
"rating" : {
"type" : "Integer"
},
"dateOfRelease" : {
"type" : "date",
"format" : "YYYY-mm-dd"
},
"prize" : {
"type" : "Double"
},
"piecesSold" : {
"type" : "Integer"
}
}
}
}'
我使用PHPto從ElasticSearch提取記錄。這是我的PHP腳本來做到這一點。
<?php
require 'vendor/autoload.php';
$hosts = [
'http://localhost:9200', // SSL to localhost
];
$client = Elasticsearch\ClientBuilder::create() // Instantiate a new ClientBuilder
->setHosts($hosts) // Set the hosts
->build();
$params = [
'index' => 'ecomercedata',
'type' => 'gadgets',
'body' => [
'query' => [
'constant_score' => [
'filter' => [
'bool' => [
'must' => [
'term' => [
'category' => 'Laptop'
],
'term' => [
'brand' => 'Apple'
]
]
]
]
]
]
]
];
try {
$results = $client->search($params);
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
exit;
}
echo '<pre>';
print_r($results);
echo '</pre>';
?>
基本上,我試圖提取所有記錄,其中category=laptop and brand=Apple
。但是,這不是給我一個正確的記錄數。根據輸入的數據集,我應該得到2條記錄,但我得到4條記錄。似乎,category and brand
條件的工作原理類似於OR
而不是AND
。
我搜索了很多。但是,無法弄清楚我做錯了什麼。
是的,這是一個工作。有我的錯誤。感謝您的幫助。 – mi6crazyheart
很高興幫助! – Val