【问题标题】:More like this elasticsearch: same request different response更像这样的弹性搜索:相同的请求不同的响应
【发布时间】:2016-09-20 01:09:43
【问题描述】:

我正在玩弹性搜索,但遇到了一个奇怪的问题:我有一个更像这样的请求,我通过多种方式建立起来:

curl -XGET 'http://127.0.0.1:9200/train-recipe/_search' -d '{
    'query': {
        'more_like_this': {
            'fields': ['ingredients'],
            'max_query_terms': 12,
            'like': [{'_type': 'recipe', '_id': 2938, '_index': 'train-recipe'}],
            'min_term_freq': 1
        }
    }, 
    'from': 0, 
    'size': 10
}'

我得到以下回复:

{"error":{"root_cause":[{"type":"json_parse_exception","reason":"json_parse_exception: Unrecognized token 'ingredients': was expecting ('true', 'false' or 'null')\n at [Source: [B@6f7a6ea4; line: 4, column: 34]"}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"train-recipe","node":"kfORe_NWSE2gIeHSHGgIQw","reason":{"type":"query_parsing_exception","reason":"Failed to parse","index":"train-recipe","caused_by":{"type":"json_parse_exception","reason":"json_parse_exception: Unrecognized token 'ingredients': was expecting ('true', 'false' or 'null')\n at [Source: [B@6f7a6ea4; line: 4, column: 34]"}}}]},"status":400}

我也有这个要求,对我来说和第一个一样:

curl -XGET 'http://127.0.0.1:9200/train-recipe/_search' -d '{
  "query": {
    "more_like_this": {
      "fields": ["ingredients"],
      "like": [{"_index" : "train-recipe","_type" : "recipe","_id" : 2938}],
      "min_term_freq": 1,
      "max_query_terms": 12
    }
  },
    'from' : 0,
    'size':10
}'

但是这个工作得很好。我也尝试使用 python 请求来做到这一点,如下所示:

def build_mlt(nb, doc_id):
   mlt = {}
   mlt['from'] = 0
   mlt['size'] = nb
   mlt['query'] = {}
   mlt['query']['more_like_this'] = {}
   mlt['query']['more_like_this']['fields'] = ['ingredients']
   mlt['query']['more_like_this']['like'] = [{"_index" : "train-recipe","_type" : "recipe","_id" : doc_id}]
   mlt['query']['more_like_this']['min_term_freq'] = 1
   mlt['query']['more_like_this']['max_query_terms'] = 10
   return mlt

def get_similar(nb, doc_id):
   mlt = build_mlt(10, 2938)
   response = requests.get("http://localhost:9200/test-recipe/recipe/_search", data=json.dumps(mlt))
   print json.loads(response.text)

而这一次我有另一个回应:

{u'hits': {u'hits': [], u'total': 0, u'max_score': None}, u'_shards': {u'successful': 5, u'failed': 0, u'total': 5}, u'took': 2, u'timed_out': False}

对我来说,这三个请求是相同的。我根据我的函数melt_builder生成的字典做了第二个。有人可以向我解释为什么我会收到三种不同的回复吗?

【问题讨论】:

    标签: python curl elasticsearch request


    【解决方案1】:

    在第一种情况下,单引号肯定有问题。为了将有效负载传递给 -d 参数,您的 JSON 和 JSON 周围都有单引号。

    在第二种情况下,您使用的是双引号,所以没问题。

    在第三种情况下,您应该使用requests.post() 发送请求,否则不会发送带有查询的有效负载。

    【讨论】:

    • 我修改了我的请求方法来发布,我仍然得到相同的答案一个空数组。
    • 你应该查询名为train-recipe而不是test-recipe的索引
    • 事实上,我在 kaggle 上工作,在 train-recipe 中有我的训练集,在 test-recipe 中有我必须预测的食谱。 train-recipe 和 test-recipe 包含完全相同的文档,只是在 train-recipe 中还有一个字段(我必须预测的字段)并且它适用于很多配方,但不适用于这个
    • 机器学习,明白了。尽管如此,在您的类似规范中,您使用的是train-recipe,但随后将您的查询发送到test-recipe,而在上述两种情况下,您将查询发送到train-recipe
    • 我也尝试在 curl 上执行相同的请求,它可以工作,但是使用 python 我得到一个空数组
    【解决方案2】:

    正如这里所说:python: single vs double quotes in JSON 您需要在 json 中使用双引号。

    第三种情况由 Val 解释。

    【讨论】:

    • 我将我的请求方法修改为发布,并将所有单引号更改为双引号,但空数组仍然得到相同的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-24
    • 2014-09-10
    • 1970-01-01
    • 1970-01-01
    • 2020-07-07
    相关资源
    最近更新 更多