本文档为您介绍使用Elasticsearch(简称ES)服务进行复杂搜索的方法。

通过Kibana控制台访问阿里云ES,执行以下命令进行复杂搜索。
  • 按照时间范围搜索:
    GET /my_index/_search?pretty
    {
        "query" : {
            "range" : {
                "post_date" : { "from" : "2009-11-15T13:00:00", "to" : "2009-11-15T14:00:00" }
            }
        }
    }
    搜索成功后,返回如下结果:
    {
      "took" : 7,
      "timed_out" : false,
      "_shards" : {
        "total" : 5,
        "successful" : 5,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : 2,
        "max_score" : 1.0,
        "hits" : [
          {
            "_index" : "my_index",
            "_type" : "my_type",
            "_id" : "2",
            "_score" : 1.0,
            "_source" : {
              "title" : "Two",
              "tags" : [
                "ruby"
              ],
              "post_date" : "2009-11-15T14:00:00"
            }
          },
          {
            "_index" : "my_index",
            "_type" : "my_type",
            "_id" : "1",
            "_score" : 1.0,
            "_source" : {
              "title" : "One World",
              "tags" : [
                "ruby"
              ],
              "post_date" : "2009-11-15T13:00:00"
            }
          }
        ]
      }
    }
  • 按照关键词搜索:
    GET /my_index/_search?pretty
    {
        "query" : {
        "simple_query_string" : {
            "query": "One +(python | ruby) -Two",
            "fields": ["title^5", "tags"],
            "default_operator": "AND"
        }
      }
    }
    搜索成功后,返回如下结果:
    {
      "took" : 8,
      "timed_out" : false,
      "_shards" : {
        "total" : 5,
        "successful" : 5,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : 1,
        "max_score" : 2.7260926,
        "hits" : [
          {
            "_index" : "my_index",
            "_type" : "my_type",
            "_id" : "1",
            "_score" : 2.7260926,
            "_source" : {
              "title" : "One World",
              "tags" : [
                "ruby"
              ],
              "post_date" : "2009-11-15T13:00:00"
            }
          }
        ]
      }
    }