【问题标题】:ElasticSearch: Query post body not working but uri search workingElasticSearch:查询帖子正文不起作用,但 uri 搜索工作
【发布时间】:2017-04-06 22:15:32
【问题描述】:

我正在发送以下弹性搜索查询,它在通过 uri-search 发送时表现得非常好。但是对于带有身体呼叫的帖子 - 它无法按预期工作。请建议如何更正查询。

这行得通:

接听电话

<someUrl>/elasticsearch/index/_search?q=host:host-0

响应(仅限于 host-0)

{
    "took": 4,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 128040,
        "max_score": 2.0973763,
        "hits": [{
                "_index": "123"
                "_type": "log_message",
                "_id": "123",
                "_score": 111,
                "_source": {
                    "host": "host-0",
                    "pid": 333,
                    "timestamp": "2017-04-06T04:29:44.724Z",
                    "priority": 7,
                    "namespace": "syslog",
                    "msg": "aaaaa"
                }
            },

            "_index": "345"
            "_type": "log_message",
            "_id": "345",
            "_score": 111,
            "_source": {
                "host": "host-0",
                "pid": 333,
                "timestamp": "2017-04-06T04:29:44.724Z",
                "priority": 7,
                "namespace": "syslog",
                "msg": "aaaaa"
            }
        }, 
        .....
}

这不起作用:

发布电话

<someUrl>/elasticsearch/index/_search

POST 调用的正文:

{
    "query" : {
        "term" : { "host": "host-0" }
    }
}

RESPONSE(不限于 host-0)

{
    "took": 4,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 128040,
        "max_score": 2.0973763,
        "hits": [{
                "_index": "123"
                "_type": "log_message",
                "_id": "123",
                "_score": 111,
                "_source": {
                    "host": "host-1",
                    "pid": 333,
                    "timestamp": "2017-04-06T04:29:44.724Z",
                    "priority": 7,
                    "namespace": "syslog",
                    "msg": "aaaaa"
                }
            },

            "_index": "345"
            "_type": "log_message",
            "_id": "345",
            "_score": 111,
            "_source": {
                "host": "host-0",
                "pid": 333,
                "priority": 7,
                "namespace": "syslog",
                "msg": "aaaaa"
            }
        }, 
            "_index": "546"
            "_type": "log_message",
            "_id": "546",
            "_score": 111,
            "_source": {
                "host": "host-0",
                "pid": 222,                 
                "priority": 7,
                "namespace": "syslog",
                "msg": "aaaaa"
            }
        }, 
        .....
}

此索引上的 Get 返回 获取 /elasticsearch/

      "host": {
        "type": "string", 
        "index": "not_analyzed"
      },

【问题讨论】:

  • 如何发送查询?与哪个客户?
  • 也请分享您的架构映射和版本
  • 我是 elasticsearch 新手 - 如何检索版本和映射?
  • 版本:GET "http://elasticsearch-url:9200"映射GET "http://elasticsearch-url:9200/index/_mapping"
  • @JoshiFriday69 total hits 在两个调用中是相同的(128040)。即两个调用都返回相同的结果。但由于host 字段为not_analyzed,你应该得到两个调用只包含host-0 的结果。

标签: elasticsearch


【解决方案1】:

在您的 GET 调用中,会分析令牌 host-0。如果您尝试以下 GET 调用(通过用双引号将 host-0 括起来),您将获得与 POST 调用相同的查询,但不会得到任何结果。

<someUrl>/elasticsearch/index/_search?q=host:"host-0"

如果您想要结果,您需要使用match 查询而不是term 查询。这将等同于您的 GET 调用中的 ...?q=host:host-0

{
    "query" : {
        "match" : { "host": "host-0" }
    }
}

最后我认为您的host 字段具有text 类型,而它应该具有keyword 类型。

【讨论】:

  • OP 说他正在获得 GET 和 POST 调用的结果(在这种情况下,这对我来说看起来很奇怪)。如果分析了host 字段,那么除非他使用其他分析器,否则我们不应该得到 POST 调用的结果!
  • 这就是我获取索引“host”时得到的结果:{“type”:“string”,“index”:“not_analyzed”},
  • 这似乎更像是邮递员工具的问题 - 命令行 curl 的结果正常工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-10
  • 2022-01-16
  • 1970-01-01
  • 1970-01-01
  • 2014-04-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多