[英]AND query in elasticsearch with curl
讓我們在Elasticsearch中注入一些數據
curl -XPUT 'localhost:9200/customer/external/1' -d '{ "author": "John", "published_from":"2016-08-03" }'
curl -XPUT 'localhost:9200/customer/external/2' -d '{ "author": "Jeanne", "published_from":"2016-08-03" }'
curl -XPUT 'localhost:9200/customer/external/3' -d '{ "author": "Jean", "published_from":"2016-08-05" }'
我正在嘗試使用Published_from = 2016-08-03和author = John查詢文檔。 我嘗試使用以下curl命令來做到這一點:
curl -g "localhost:9200/customer/external/_search?pretty&filter_path=hits.hits._source.author&q=+author:John+published_from:2016-08-03"
但是,輸出顯示Jeanne
{
"hits" : {
"hits" : [
{
"_source" : {
"author" : "John"
}
},
{
"_source" : {
"author" : "Jeanne"
}
}
]
}
}
當我嘗試這個curl命令時:
curl "localhost:9200/customer/external/_search?pretty&filter_path=hits.hits._source.author&q=%2Bauthor%3AJohn+%2Bpublished_from%3A2016-08-03"
輸出正是我想要的。
{
"hits" : {
"hits" : [
{
"_source" : {
"author" : "John"
}
}
]
}
}
為什么第一個命令無法按預期運行?
第一個URL中的+
號 :
...&q=+author:John+published_from:2016-08-03
根據百分比編碼規則(在服務器端)被解釋為space 。 通常將空格編碼為%20
,但是由於歷史原因, +
也是空格字符的有效編碼。
這意味着ElasticSearch獲取的查詢字符串如下所示:
author:John published_from:2016-08-03
根據查詢字符串語法 ,它將查找包含一個或多個author:John
或published_from:2016-08-03
任何文檔。
當您正確編碼Elastic的+
運算符(在第二個URL中)時,收到的查詢為:
+author:John +published_from:2016-08-03
注意+
被解碼為空格 ,
%2B
為+
。
curl
簡單的查詢參數編碼 為避免對查詢進行手動(百分比)編碼,可以使用--data-urlencode
選項,並提供原始的key=value
對。
例如:
curl -G 'localhost:9200/customer/external/_search?pretty&filter_path=hits.hits._source.author' --data-urlencode 'q=+author:John +published_from:2016-08-03'
在這里curl
會將URL中的查詢參數與-d
/ --data-urlencode
選項提供的參數結合在一起。 我們需要-G
來強制執行GET
請求,因為-d
/ --data-urlencode
默認為POST
請求。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.