es的基本操作(elasticsearch-3)

  1. 创建es_db索引,并将该索引的默认分词方法设置为ik_max_word
PUT /es_db
{
  "settings": {
    "index": {
      "analysis.analyzer.default.type": "ik_max_word"
    }
  }
}
  1. 针对索引的基本操作
GET /es_db
DELETE /es_db
  1. 添加文档
PUT /es_db/_doc/1
{
  "name": "张三",
  "sex": 1,
  "age": 23,
  "address": "广州天河公园"
}
PUT /es_db/_doc/2
{
  "name": "张三",
  "sex": 0,
  "age": 43,
  "address": "广州天河棠下"
}
PUT /es_db/_doc/3
{
  "name": "李四",
  "sex": 0,
  "age": 18,
  "address": "深圳龙岗公园"
}
PUT /es_db/_doc/4
{
  "name": "李斯",
  "sex": 0,
  "age": 51,
  "address": "广州白云山"
}
PUT /es_db/_doc/4
{
  "name": "李斯",
  "sex": 0,
  "age": 52,
  "address": "广州白云山"
}
PUT /es_db/_doc/5
{
  "name": "广州人",
  "sex": 0,
  "age": 100,
  "address": "广州"
}
  1. 查询文档的基础操作
GET /es_db/_doc/_search

GET /es_db/_doc/_search?q=age:52

GET /es_db/_doc/_search?q=age:<=20

GET /es_db/_doc/_search?q=age:>20

GET /es_db/_doc/_search?q=age[40 TO 50]

GET /es_db/_doc/_search?q=age[0 TO 150]&from=2&size=2

GET /es_db/_doc/_search?_source=name,age

GET /es_db/_doc/_search?_source=name,age&sort=age:desc
  1. 根据多个文档id进行批量查询
GET /es_db/_doc/_mget
{
  "ids":["1", "2"]
}

DSL语言高级查询

  1. 根据某一字段进行精确查询,term查询不会对字段进行分词查询,会采用精确匹配

类似SQL: select * from student where name = ‘张三’

POST /es_db/_doc/_search
{
  "query": {
    "term": {
      "name": "张三"
    }
  }
}
  1. 分词测试
POST _analyze
{
  "analyzer": "ik_max_word",
  "text": "广州公园"
}
  1. 根据备注信息模糊查询 match, match会根据该字段的分词器,进行分词查询

类似于SQL: select * from user where (address like ‘%广州%’ or address like ‘%公园%’) limit 0, 5

POST /es_db/_doc/_search
{
  "from": 0,
  "size": 5,
  "query": {
    "match": {
      "address": "广州公园"
    }
  }
}
  1. 多字段模糊匹配查询与精准查询 multi_match

类似于SQL: select * from student where name like ‘%广州%’ or address like ‘%广州%’

POST /es_db/_doc/_search
{
  "query": {
    "multi_match": {
      "query": "广州",
      "fields": ["name", "address"]
    }
  }
}
  1. 范围查询
  • range:范围关键字
  • gte:大于等于
  • lte:小于等于
  • gt:大于
  • lt:小于

类似于SQL: select * from user where age between 20 and 30

POST /es_db/_doc/_search
{
  "query": {
    "range": {
      "age": {
        "gte": 20,
        "lte": 30
      }
    }
  }
}
  1. 分页、输出字段、排序综合查询

类似SQL: select name, age, address from user where age between 10 and 50 order by age desc limit 0,5

POST /es_db/_doc/_search
{
  "query": {
    "range": {
      "age": {
        "gte": 10,
        "lte": 50 
      }
    }
  },
  "from": 0,
  "size": 5,
  "_source": ["name", "age", "address"],
  "sort": {
    "age": "desc"
    
  }
}
  1. Filter过滤器方式查询,它的查询不会计算相关性分值,也不会对结果进行排序, 因此效率会高一点,查询的结果可以被缓存
POST /es_db/_doc/_search
{
  "query": {
    "bool": {
      "filter": {
        "term": {
          "age": 23
        }
      }
    }
  }
}
  1. 获取文档映射
GET /es_db/_mapping
  1. 根据文档ID批量获取
GET /es_db/_mget
{
  "docs": [
    {
      "_type": "_doc",
      "_id": 1
    },
    {
      "_type": "_doc",
      "_id": 4
    }
    ]
}

GET /es_db/_doc/_mget
{
    "docs": [
    {
      "_id": 1
    },
    {
      "_id": 4
    }
    ]
}
  1. 批量查询不同索引下,不同文档ID的数据
GET _mget
{
  "docs": [
    {
      "_index": "es_db",
      "_type": "_doc",
      "_id": 4
    },
    {
      "_index": "es_db_second",
      "_type": "_doc",
      "_id": 1
    }
    ]
}
  1. 批量操作文档

批量对文档进行写操作是通过_bulk的API来实现

请求方式:POST

请求地址:_bulk

请求参数:通过_bulk操作文档,一般至少有两行参数(或偶数行参数)

第一行参数为指定操作的类型及操作的对象

(index,type和id)

第二行参数才是操作的数据

参数设置如下:

{"actionName":{"_index":"indexName", "_type":"typeName","_id":"id"}} 2 {"field1":"value1", "field2":"value2"}

actionName:表示操作类型,主要有create,index,delete和update

批量创建文档create

POST _bulk
{"create": {"_index": "es_db", "_type": "_doc", "_id": 6}}
{"name": "曹操", "sex": 0, "age": 30, "address": "许昌"}
{"create": {"_index": "es_db", "_type": "_doc", "_id": 7}}
{"name": "曹丕", "sex": 0, "age": 20, "address": "许昌"}

如果原文档不存在,则是创建;如果原文档存在,则是替换(全量修改原文档)

POST _bulk
{"index": {"_index": "es_db", "_type": "_doc", "_id": 8}}
{"name": "曹植", "sex": 0, "age": 30, "address": "许昌"}
{"index": {"_index": "es_db", "_type": "_doc", "_id": 7}}
{"name": "曹丕", "sex": 0, "age": 20}

批量删除delete

POST _bulk
{"delete": {"_index": "es_db", "_type": "_doc", "_id": 8}}
{"delete": {"_index": "es_db_second", "_type": "_doc", "_id": 1}}
  1. 不指定字段进行数据匹配查询query_string,含AND与OR条件(会进行分词模糊匹配所有字段的数据)
POST /es_db/_search
{
  "query": {
    "query_string": {
      "query": "广州 OR 曹丕"
    }
  }
}
  1. 分析一个document上的_score是如何被计算出来的
GET /es_db/_doc/2/_explain
{
  "query": {
    "match": {
      "address": "广州天河"
    }
  }
}

总结

  1. match

    模糊匹配,需要指定字段名,会对查询条件进行分词;也就是说match是一个部分匹配的模糊查询。查询条件相对来说比较宽松。

  2. term

    term是精确查询,不会对查询条件进行分词操作;

    该查询方式和match在查询单个词,无法进行分词的信息时,是等价的,查询结果一样;

  3. match_phase

    会对查询条件进行分词操作,但是查询的结果是需要包含查询条件所有的分词,而且顺序要是一样;

    例:查询“hello world”,则查询结果中必须包含“hello”和“world”,且hello要在world之前,且是连续的,“world hello”不满足,“hello that world”也不满足;

  4. query_string

    与match类似,但是match需要指定字段名,query_string是在所有字段中搜索,范围更广泛。

扩展 Elasticsearch SQL语法

注:目前FROM只支持单表

SELECT select_expr [, ...]
[ FROM table_name ]
[ WHERE condition ]
[ GROUP BY grouping_element [, ...] ]
[ HAVING condition]
[ ORDER BY expression [ ASC | DESC ] [, ...] ]
[ LIMIT [ count ] ]
[ PIVOT ( aggregation_expr FOR column IN ( value [ [ AS ] alias ] [, ...] ) ) ]

例:

  1. 查询es_db索引里年龄大于等于50岁的数据

    GET /_sql?format=txt
    {
      "query": "SELECT * FROM es_db where age >= 50"
    }
    

    其中formar=txt表示指定返回的数据类型

    格式 描述
    txt 类cli表示
    json JSON格式
    csv 逗号分隔符
    Yaml yaml人类可读格式
    tsv 制表符分隔符
  2. 分别统计不同性别50岁以上的总年龄

    GET /_sql?format=txt
    {
      "query": "SELECT sex, sum(age) FROM es_db where age >= 50 group by sex"
    }
    
  3. 将SQL转换为DSL

    GET /_sql/translate
    {
      "query": "SELECT sex, sum(age) FROM es_db where age >= 50 group by sex"
    }
    

你可能感兴趣的:(#,ELK,数据库,elasticsearch)