【问题标题】:Search in elasticsearch using php curl使用 php curl 在 elasticsearch 中搜索
【发布时间】:2015-03-13 18:50:21
【问题描述】:

我已经使用 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"}}]}

【问题讨论】:

  • 您的查询是什么? IE。你把$params传递给了什么?
  • @king_nak 我已经更新了问题;请立即查找查询

标签: php curl elasticsearch


【解决方案1】:

问题可能在于您在查询 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"
         }
      }
   ]
}

【讨论】:

  • 我已经更新了你给出的查询,它也给出了同样的错误
【解决方案2】:

我找到了解决办法;下面是正确的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"}}]}

问题出在匹配查询阶段;我将匹配查询中的参数附加为嵌套数组。

现在我已经删除了嵌套数组,并将匹配查询阶段的选项附加为顺序数组

感谢大家的配合

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-06
    • 1970-01-01
    • 2019-03-18
    • 2012-01-12
    • 2017-03-16
    • 2016-11-26
    • 1970-01-01
    • 2017-07-02
    相关资源
    最近更新 更多