elasticsearch查询语法整理

elasticsearch(以下简称es)查询语法非常灵活, 整理如下:
环境:

es 7.3.1

查询语法整理

为了表示文档, 测试索引名为myindex(其中记录的为一些日志), 其有一个别名(alias)为applog.

获取所有文档(写多种方式表示都可以, 下同)

GET /_search
{
  "query": {
    "match_all": {}
  }
}

GET /_search
GET /_all/_search

简单查询

查询applog中包含DELETE串的文档
GET /applog/_search?q=DELETE

mapping

应该先创建mapping(会自动创建一个index), 再创建index. 因为mapping不可修改.
相当于提前定义好数据类型, 再插入数据.
为索引test_index创建mapping:

PUT /test_index
{
  "mappings": {
    "properties": {
      "id": {
        "type": "long"
      },
      "date": {
        "type": "date"
      },
      "name": {
        "type": "text"
      }
    }
  }
}

查看索引的mapping:
GET /applog/_mapping (返回的其实是myindex)的mapping

别名(alias)

为索引创建别名

为索引myindex创建别名: applog
PUT /myindex/_alias/applog,

然后下面两句效果就是一样的了.

GET /applog/_search
GET /myindex/_search

带过滤条件的别名

创建别名applog_admin, 其代表的数据为: 操作都角色为管理员的所有文档.

PUT /myindex/_alias/applog_admin
{
  "filter": {
    "term": {
      "Role": "Admin"
    }
  }
}

那么下面两种查询就是一样的了:

GET /applog_admin/_search
GET /applog/_search
{
  "query": {
    "bool": {
      "filter": {
        "term": {
          "Role": "Admin"
        }
      }
    }
  }
}

分析器(analyzer)

查看标准分析器对文本的分析结果(查看分词效果)

GET /_analyze
{
  "analyzer": "standard",
  "text": ["管理员", "2019-09-06"]
}

结果为:

{
  "tokens" : [
    {
      "token" : "管",
      "start_offset" : 0,
      "end_offset" : 1,
      "type" : "<IDEOGRAPHIC>",
      "position" : 0
    },
    {
      "token" : "理",
      "start_offset" : 1,
      "end_offset" : 2,
      "type" : "<IDEOGRAPHIC>",
      "position" : 1
    },
    {
      "token" : "员",
      "start_offset" : 2,
      "end_offset" : 3,
      "type" : "<IDEOGRAPHIC>",
      "position" : 2
    }
  ]
}

可见标准分析器(standard)对于中文, 一个字算一个词.

未完待续.

发布了231 篇原创文章 · 获赞 77 · 访问量 52万+

猜你喜欢

转载自blog.csdn.net/butterfly5211314/article/details/100636296