php PHP中的elasticsearch查询和cURL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11647507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
elasticsearch query and cURL in PHP
提问by user1249791
I am just starting with elasticsearch. I want to query using cURL in php.
我刚开始使用elasticsearch。我想在 php 中使用 cURL 进行查询。
This code gives nothing... (see error below if I execute from command line. I am not sure that this error is caused of line breaks in console...)
这段代码什么也没给出......(如果我从命令行执行,请参阅下面的错误。我不确定这个错误是由控制台中的换行符引起的......)
$url = "curl -s -XGET http://<my_url>:9200/idx_occurrence/Occurrence/_search -d '
{
'filtered' : {
'query' : {
'term' : { 'kingdom_interpreted' : 'Plantae' }
}
}
}' ";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
$return=curl_exec($ch);
var_dump($return);
but if I use this url http://<my_url>:9200/idx_occurrence/Occurrence/_search?q=kingdom_interpreted:Plantae
但如果我使用这个网址 http://<my_url>:9200/idx_occurrence/Occurrence/_search?q=kingdom_interpreted:Plantae
then I get results from cURL.
然后我从 cURL 得到结果。
Maybe may query filter is incorrect? (I tried several options without success)
也许查询过滤器不正确?(我尝试了几个选项都没有成功)
ERROR: {"error":"SearchPhaseExecutionException[Failed to execute phase [query], total failure; shardFailures {[AS6HqxgNRtyU9-pQKhJsXQ][idx_occurrence][3]: SearchParseException[[idx_occurrence][3]: from[-1],size[-1]: Parse Failure [Failed to parse source [\n{\n filtered : {\n query : {\n term : { kingdom : Plantae }\n }\n}\n}]]]; nested: SearchParseException[[idx_occurrence][3]: from[-1],size[-1]: Parse Failure [No parser for element [filtered]]]; }{[AS6HqxgNRtyU9-pQKhJsXQ][idx_occurrence][2]: SearchParseException[[idx_occurrence][2]: from[-1],size[-1]: Parse Failure [Failed to parse source [\n{\n filtered : {\n query : {\n term : { kingdom : Plantae }\n }\n}\n}]]]; nested: SearchParseException[[idx_occurrence][2]: from[-1],size[-1]: Parse Failure [No parser for element [filtered]]]; }]","status":500}
解析失败[元素没有解析器[过滤]]]; }]","状态":500}
采纳答案by Todd Holmberg
I have been using the Elastica PHP library for my elasticsearch interactions:
我一直在使用 Elastica PHP 库进行 elasticsearch 交互:
https://github.com/ruflin/Elastica
https://github.com/ruflin/Elastica
It had a very short learning curve. Here's an example:
它的学习曲线非常短。下面是一个例子:
$client = new Elastica_Client();
$index = $client->getIndex('idx_occurrence');
$index->getType('Occurrence');
$query_string = new Elastica_Query_QueryString('Plantae');
$query_string->setFields(array('kingdom_interpreted'));
$query = new Elastica_Query($query_string);
$index->refresh();
$searchResults = $index->search($query);
This illustrates a Query String search limited to a specific field. $searchResultsis an array of Elastica_ResultSetobjects. I like Elastica because it abstracts away any cURL-related issues.
这说明了仅限于特定字段的查询字符串搜索。$searchResults是一个Elastica_ResultSet对象数组。我喜欢 Elastica,因为它抽象了任何与 cURL 相关的问题。
回答by www.atob.site
this is a simple request demo:
这是一个简单的请求演示:
$param = "
{
'filtered' : {
'query' : {
'term' : { 'kingdom_interpreted' : 'Plantae' }
}
}
}";
$header = array(
"content-type: application/x-www-form-urlencoded; charset=UTF-8"
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://xxxxx:9200/idx_occurrence/Occurrence/_search");
curl_setopt($curl,CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $param);
$res = curl_exec($curl);
curl_close($curl);
return $res;
回答by user1249791
I've found an answer myself to part of the question. I managed to get it by command line.
我自己找到了部分问题的答案。我设法通过命令行获取它。
curl -XGET my_server:9200/idx_occurrence/Occurrence/_search?pretty=true -d '{ "query": { "query_string" :{"fields" : ["kingdom_interpreted"], "query": "Plantae" } } }'
using PHP to execute the (correct) cURL request just sends back an empty string. No errors in PHP logs.
使用 PHP 执行(正确的)cURL 请求只是发送回一个空字符串。PHP 日志中没有错误。
$url='curl -XGET http://<my_url>:9200/idx_occurrence/Occurrence/_search?pretty=true
-d \'{ "query": { "query_string" :{ "fields" : ["kingdom_interpreted"],
"query": "Plantae" } } }\'';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
ob_start();
curl_exec ($ch);
curl_close ($ch);
$data = ob_get_contents();
ob_end_clean();
var_dump($data);
Again, if instead of this $url I send this url my_url:9200/idx_occurrence/Occurrence/_search?q=kingdom_interpreted:Plantae
同样,如果不是这个 $url 我发送这个 url my_url:9200/idx_occurrence/Occurrence/_search?q=kingdom_interpreted:Plantae
It works. Why?
有用。为什么?
回答by Anuga
$search = 'Plantae'; //search query
$fields = 'kingdom_interpreted'; //fields to look in
$results = file_get_contents('http://server:port/idx_occurrence/Occurrence/_search?q='.$search.'&fields='.$fields);

