繁体   English   中英

使用php curl在elasticsearch中搜索

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM