【问题标题】:search error: [parse_exception] failed to parse search source. expected field name but got [START_OBJECT]搜索错误:[parse_exception] 无法解析搜索源。预期的字段名称,但得到 [START_OBJECT]
【发布时间】:2017-04-17 21:59:08
【问题描述】:

我遇到查询解析异常。我正在使用 JavaScript。我的配置存在于 elastic.js 文件中。

如果我删除过滤的部分,我会得到结果。但是如果我添加它,我会得到异常

var client = require('./elastic.js');

client.search({
    index: 'test-2017.03.25',
    size: 0,
    body: {
        query: {
            bool: {
                must: {
                    match: {
                        status: 502,
                    }
                },

            },
            filtered: {
                query: {
                    range: {
                        timestamp: {'gt': 1490380200000}
                    }
                }
            }
        }
    }
}, function (error, response, status) {
    if (error) {
        console.log("search error: " + error)
    }
    else {
        console.log("--- Response ---");
        console.log(response);
        console.log("--- Hits ---");
        response.hits.hits.forEach(function (hit) {
            console.log(hit);
        })
    }
});

这是我的对象映射:

"test-2017.03.02": {
    "mappings": {
      "log": {
        "properties": {
          "@timestamp": {
            "type": "date",
            "format": "strict_date_optional_time||epoch_millis"
          },
          "@version": {
            "type": "string"
          },
          "beat": {
            "properties": {
              "hostname": {
                "type": "string"
              },
              "name": {
                "type": "string"
              }
            }
          },
          "body_bytes_sent": {
            "type": "string"
          },
          "count": {
            "type": "long"
          },
          "fields": {
            "properties": {
              "label": {
                "type": "string"
              }
            }
          },
          "host": {
            "type": "string"
          },
          "http_referrer": {
            "type": "string"
          },
          "http_user_agent": {
            "type": "string"
          },
          "input_type": {
            "type": "string"
          },
          "message": {
            "type": "string"
          },
          "offset": {
            "type": "long"
          },
          "remote_addr": {
            "type": "string"
          },
          "remote_user": {
            "type": "string"
          },
          "request": {
            "type": "string"
          },
          "request_method": {
            "type": "string"
          },
          "request_time": {
            "type": "double"
          },
          "source": {
            "type": "string"
          },
          "status": {
            "type": "string"
          },
          "tags": {
            "type": "string"
          },
          "time": {
            "type": "string"
          },
          "type": {
            "type": "string"
          }
        }
      }
    }
  }

我想根据状态和请求获取该数据,并使用时间戳字段进行过滤。

我收到以下错误:

搜索错误:[parse_exception] 无法解析搜索源。预期的字段名称,但得到 [START_OBJECT]

请帮忙。

示例文档:

{
  "_index": "test-2017.03.25",
  "_type": "log",
  "_id": "JI9u8hGG8y8gGUk",
  "_score": 1.0,
  "_source": {
    "@version": "1",
    "@timestamp": "2017-03-25T00:00:01.617Z",
    "count": 1,
    "offset": 1114273370,
    "type": "log",
    "input_type": "log",
    "fields": {
      "label": "test"
    },
    "source": "/var/log/nginx/access.log",
    "tags": [
      "_grokparsefailure"
    ],
    "time": "25/Mar/2017:05:30:00 +0530",
    "body_bytes_sent": "81",
    "request_time": 0.052,
    "status": "200",
    "request": "GET /api/test?status=test HTTP/1.1",
    "request_method": "GET",
    "http_referrer": "-",
    "http_user_agent": "Java/1.8.0_31"
  }
}

【问题讨论】:

    标签: javascript elasticsearch


    【解决方案1】:

    您的查询无效,请将其更改为:

    client.search({
        index: 'test-2017.03.25',
        size: 0,
        body: {
            query: {
                bool: {
                    filter: [
                      {
                        match: {
                            status: 502,
                        }
                      },
                      {
                        range: {
                            '@timestamp': {'gt': 1490380200000}
                        }
                      }
                    ]
                }
            }
        }
    

    【讨论】:

    • 它工作但没有得到任何点击。我正在添加一个示例文档。你能不能给一个查询以获得类似的文件。
    • 欢迎分享一个应该匹配的示例文档。
    • 根据你的映射,范围查询中timestamp应该是@timestamp
    猜你喜欢
    • 2016-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多