首页 文章

通过组合范围和术语匹配json格式来查询Elasticsearch

提问于
浏览
10

我试图按时间范围查询Elasticsearch索引,另外还有一个术语匹配特定的字符串值 .

我试过这个查询,看起来非常简单:

{
  "query" : {
    "bool": {
      "must": [
        {
          "match": {
            "method": "/customer/help"
          }
        },
        {
          "range" : {
            "startTime": {
              "from" : "2015-10-20T13:00-04:00",
              "to" : "2015-10-20T14:00-04:00"
            }
          }
        }
      ]
    }
  }
}

在这种情况下,我希望给定时间范围内的所有文档的方法值也为 "/customer/help" .

在我的结果中,我收到的时间范围内的结果,但我得到的文件具有 "method" 字段的各种值,当我只想在该字段中使用 "/customer/help" 的结果时 .

1 回答

  • 15

    在映射中,您需要 methodnot_analyzed (或使用 keyword analyzer进行分析),查询应使用 term . 这样,您在方法中索引的文本将被索引为单个标记,并且 term 确保您搜索的文本与 method 中索引的标记完全匹配:

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

    您需要使用的查询:

    {
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "method": "/customer/help"
              }
            },
            {
              "range": {
                "startTime": {
                  "from": "2015-10-20T13:00-04:00",
                  "to": "2015-10-20T14:00-04:00"
                }
              }
            }
          ]
        }
      }
    }
    

相关问题