[英]Search in elasticsearch using php curl
我已經使用elasticsearch制作了應用程序,除了使用php curl進行搜索之外,其他所有程序都運行完美; 錯誤如下
[match] query parsed in simplified form, with direct field name, but included more options than just the field name, possibly use its 'options' form, with 'query' element?]
但是同一查詢在命令行中運行良好。
當我做一些更改時,我發現這是在curl POST發生的。
我正在使用以下代碼來運行php curl並嘗試了GET方法
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 200);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($method));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
$response = curl_exec($ch);
curl_close ($ch);
和查詢是
{"query":{"filtered":{"query":{"bool":{"should":[{"bool":{"should":[{"match_phrase":{"name":"india"}}],"boost":16}},{"bool":{"should":[{"match_phrase":{"description":"india"}}],"boost":8}},{"bool":{"should":[{"match":{"name":{"query":"india","analyzer":"standard"}}},{"match":{"description":{"query":"india","analyzer":"standard"}}}],"boost":4}},{"match":{"name.ngram":[{"query":"india","analyzer":"standard"}]}},{"match":{"description":[{"query":"india","analyzer":"standard"}]}},{"match":{"name.ngram":[{"query":"india","analyzer":"standard","fuzziness":"auto"}]}},{"match":{"description":[{"query":"india","analyzer":"standard","fuzziness":"auto"}]}}],"boost":2}},"filter":{"and":[{"terms":{"type":["book"]}},{"range":{"price":{"from":"1","to":"100"}}}]}}},"from":0,"size":20,"filter":{"and":[]},"sort":[{"popularity":{"order":"desc","missing":"_last"}}]}
問題可能出在查詢dsl中使用空白過濾器。
"filter": {
"and":[]
}
嘗試不使用空白過濾器。 此外,頂級過濾器在Elasticsearch 1.0及更高版本中已重命名。
更新查詢DSL:
{
"query": {
"filtered": {
"query": {
"bool": {
"should": [
{
"bool": {
"should": [
{
"match_phrase": {
"name": "india"
}
}
],
"boost": 16
}
},
{
"bool": {
"should": [
{
"match_phrase": {
"description": "india"
}
}
],
"boost": 8
}
},
{
"bool": {
"should": [
{
"match": {
"name": {
"query": "india",
"analyzer": "standard"
}
}
},
{
"match": {
"description": {
"query": "india",
"analyzer": "standard"
}
}
}
],
"boost": 4
}
},
{
"match": {
"name.ngram": [
{
"query": "india",
"analyzer": "standard"
}
]
}
},
{
"match": {
"description": [
{
"query": "india",
"analyzer": "standard"
}
]
}
},
{
"match": {
"name.ngram": [
{
"query": "india",
"analyzer": "standard",
"fuzziness": "auto"
}
]
}
},
{
"match": {
"description": [
{
"query": "india",
"analyzer": "standard",
"fuzziness": "auto"
}
]
}
}
],
"boost": 2
}
},
"filter": {
"and": [
{
"terms": {
"type": [
"book"
]
}
},
{
"range": {
"price": {
"from": "1",
"to": "100"
}
}
}
]
}
}
},
"from": 0,
"size": 20,
"sort": [
{
"popularity": {
"order": "desc",
"missing": "_last"
}
}
]
}
我找到了解決方案; 以下是正確的json
{"query":{"filtered":{"query":{"bool":{"should":[{"bool":{"should":[{"match_phrase":{"name":"india"}}],"boost":16}},{"bool":{"should":[{"match_phrase":{"description":"india"}}],"boost":8}},{"bool":{"should":[{"match":{"name":{"query":"india","analyzer":"standard"}}},{"match":{"description":{"query":"india","analyzer":"standard"}}}],"boost":4}},{"match":{"name.ngram":{"query":"india","analyzer":"standard"}}},{"match":{"description":{"query":"india","analyzer":"standard"}}},{"match":{"name.ngram":{"query":"india","analyzer":"standard","fuzziness":"auto"}}},{"match":{"description":{"query":"india","analyzer":"standard","fuzziness":"auto"}}}],"boost":2}},"filter":[]}},"from":0,"size":20,"sort":[{"popularity":{"order":"desc","missing":"_last"}}]}
問題在匹配查詢階段; 我將匹配查詢中的參數附加為嵌套數組。
現在,我刪除了嵌套數組,並將匹配查詢階段中的選項附加為順序數組
謝謝你們的合作
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.