Elasticsearch 中查询使用的 DSL(特定查询语言) 相信大家都使用过. 除此之外 Elasticsearch 还可以通过 uri 进行查询条件指定.

虽然功能不及DSL强大,但也不妨多多了解一下.

基本查询

故事从一条查询开始:

GET /movies/_search?q=2012&df=title&sort=year:desc&from=0&size=10&timeout=1s

从上面可以看出,包含以下参数:

  • q 指定查询语句,使用 query string syntax
  • df 默认字段,不指定是会对所有字段进行查询
  • sort 排序 / from 和 size 用于分页
  • profile 可以查看查询是如何被执行的

查询字段语法

  • { "profile":"true" } 展示查询执行计划
  • 指定字段查询 vs 泛查询: q=title:2012 / q=2012
  • Term vs Phrase
    • Beautiful Mind 等效于 or查询
    • "Beautiful Mind" 等效于 and查询
    • Phrase 要求前后顺序保存一致
  • 分组和引号
    • title:(Beautiful Mind) 分组表示 一定要一起出现
    • title="Beautiful Mind" Phrase 表示 一定要一起出现 且 按顺序
  • 布尔操作
    • AND / OR / NOT 或者 && / || / !
      • 必须大写
  • 分组
      • 表示 must
      • 表示 must_not
    • title:(+matrix -reloaded) 必须要有 matrix,且(因为是分组)不能有reloaded
  • 范围查询
    • 区间表示:[] 闭区间,{}开区间
      • year:>=1980
      • year:{2019 TO 2018}
      • year:[* TO 2019]
  • 算数符号
    • year:>2010
    • year:(>2010 && <=2018)
    • year:(+>2010 +<=2018>)
  • 通配符(查询效率低,占用内存大,不建议使用。特别是放在最前面)
    • ? 代表一个字符,* 代表 0 或 多个字符
  • 正则表达
    • title:[bt]oy
  • 模糊匹配与近似查询
    • title:befutifl~1
    • title:"lord rings"~2

具体操作

# 基本查询
GET /movies/_search?q=2012&df=title&sort=year:desc&from=0&size=10&timeout=1s

# 带profile, 展示执行计划
GET /movies/_search?q=2012&df=title
{
	"profile":"true"
}

================== 返回结果 ==================
{
  "took" : 76,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 4.1047716,
    "hits" : [
      {
        "_index" : "tag",
        "_type" : "_doc",
        "_id" : "hFpZH3sBgpUFltoIW7vi",
        "_score" : 4.1047716,
        "_source" : {
          "memberId" : 618,
          "phone" : "15321761517",
          "sex" : "1",
          "channel" : 4,
          "subOpenId" : "gcik-4nwr9lsy7pv",
          "address" : 618,
          "regTime" : "2019-11-22",
          "orderCount" : 3,
          "orderTime" : "2019-11-22",
          "orderMoney" : 7546.0,
          "favGoods" : [
            2,
            11,
            16
          ],
          "couponTimes" : [
            "2019-11-25",
            "2019-11-27",
            "2019-11-13"
          ],
          "chargeMoney" : 25.0,
          "overTime" : 2100
        }
      }
    ]
  },
  "profile" : {
    "shards" : [
      {
        "id" : "[0tHjuZtUQMeFyRCWlNgK4w][tag][0]",
        "searches" : [
          {
            "query" : [
              {
                "type" : "TermQuery",
                "description" : "subOpenId:gcik",
                "time_in_nanos" : 237449,
                "breakdown" : {
                  "score" : 0,
                  "build_scorer_count" : 8,
                  "match_count" : 0,
                  "create_weight" : 231939,
                  "next_doc" : 0,
                  "match" : 0,
                  "create_weight_count" : 1,
                  "next_doc_count" : 0,
                  "score_count" : 0,
                  "build_scorer" : 5501,
                  "advance" : 0,
                  "advance_count" : 0
                }
              }
            ],
            "rewrite_time" : 3399,
            "collector" : [
              {
                "name" : "CancellableCollector",
                "reason" : "search_cancelled",
                "time_in_nanos" : 81522,
                "children" : [
                  {
                    "name" : "SimpleTopScoreDocCollector",
                    "reason" : "search_top_hits",
                    "time_in_nanos" : 64422
                  }
                ]
              }
            ]
          }
        ],
        "aggregations" : [ ]
      },
      {
        "id" : "[0tHjuZtUQMeFyRCWlNgK4w][tag][1]",
        "searches" : [
          {
            "query" : [
              ......
            ],
            "rewrite_time" : 3546,
            "collector" : [
              ......
            ]
          }
        ],
        "aggregations" : [ ]
      },
      {
        "id" : "[0tHjuZtUQMeFyRCWlNgK4w][tag][2]",
        "searches" : [
          {
            "query" : [
              ......
              }
            ],
            "rewrite_time" : 4308,
            "collector" : [
              ......
            ]
          }
        ],
        "aggregations" : [ ]
      },
      {
        "id" : "[0tHjuZtUQMeFyRCWlNgK4w][tag][3]",
        "searches" : [
          {
            "query" : [
              ......
            ],
            "rewrite_time" : 3412,
            "collector" : [
              ......
            ]
          }
        ],
        "aggregations" : [ ]
      },
      {
        "id" : "[0tHjuZtUQMeFyRCWlNgK4w][tag][4]",
        "searches" : [
          {
            "query" : [
              ......
            ],
            "rewrite_time" : 22285,
            "collector" : [
              ......
                ]
              }
            ]
          }
        ],
        "aggregations" : [ ]
      }
    ]
  }
}
复制代码

其他 URI 查询

# 泛查询,正对_all,所有字段
GET /movies/_search?q=2012
{
	"profile":"true"
}

# 指定字段
GET /movies/_search?q=title:2012&sort=year:desc&from=0&size=10&timeout=1s
{
	"profile":"true"
}


# 查找美丽心灵, Mind为泛查询
GET /movies/_search?q=title:Beautiful Mind
{
	"profile":"true"
}

# 泛查询
GET /movies/_search?q=title:2012
{
	"profile":"true"
}

# 使用引号,Phrase查询
GET /movies/_search?q=title:"Beautiful Mind"
{
	"profile":"true"
}

# 分组,Bool查询
GET /movies/_search?q=title:(Beautiful Mind)
{
	"profile":"true"
}


# 布尔操作符
# 查找美丽心灵
GET /movies/_search?q=title:(Beautiful AND Mind)
{
	"profile":"true"
}

# 查找美丽心灵
GET /movies/_search?q=title:(Beautiful NOT Mind)
{
	"profile":"true"
}

# 查找美丽心灵
GET /movies/_search?q=title:(Beautiful %2BMind)
{
	"profile":"true"
}


# 范围查询 ,区间写法
GET /movies/_search?q=title:beautiful AND year:[2002 TO 2018%7D
{
	"profile":"true"
}


# 通配符查询
GET /movies/_search?q=title:b*
{
	"profile":"true"
}

# 模糊匹配&近似度匹配
GET /movies/_search?q=title:beautifl~1
{
	"profile":"true"
}

GET /movies/_search?q=title:"Lord Rings"~2
{
	"profile":"true"
}