PUT /es_db
{
"settings": {
"index": {
"analysis.analyzer.default.type": "ik_max_word"
}
}
}
GET /es_db
DELETE /es_db
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": "广州"
}
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
GET /es_db/_doc/_mget
{
"ids":["1", "2"]
}
类似SQL: select * from student where name = ‘张三’
POST /es_db/_doc/_search
{
"query": {
"term": {
"name": "张三"
}
}
}
POST _analyze
{
"analyzer": "ik_max_word",
"text": "广州公园"
}
类似于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": "广州公园"
}
}
}
类似于SQL: select * from student where name like ‘%广州%’ or address like ‘%广州%’
POST /es_db/_doc/_search
{
"query": {
"multi_match": {
"query": "广州",
"fields": ["name", "address"]
}
}
}
类似于SQL: select * from user where age between 20 and 30
POST /es_db/_doc/_search
{
"query": {
"range": {
"age": {
"gte": 20,
"lte": 30
}
}
}
}
类似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"
}
}
POST /es_db/_doc/_search
{
"query": {
"bool": {
"filter": {
"term": {
"age": 23
}
}
}
}
}
GET /es_db/_mapping
GET /es_db/_mget
{
"docs": [
{
"_type": "_doc",
"_id": 1
},
{
"_type": "_doc",
"_id": 4
}
]
}
GET /es_db/_doc/_mget
{
"docs": [
{
"_id": 1
},
{
"_id": 4
}
]
}
GET _mget
{
"docs": [
{
"_index": "es_db",
"_type": "_doc",
"_id": 4
},
{
"_index": "es_db_second",
"_type": "_doc",
"_id": 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}}
POST /es_db/_search
{
"query": {
"query_string": {
"query": "广州 OR 曹丕"
}
}
}
GET /es_db/_doc/2/_explain
{
"query": {
"match": {
"address": "广州天河"
}
}
}
match
模糊匹配,需要指定字段名,会对查询条件进行分词;也就是说match是一个部分匹配的模糊查询。查询条件相对来说比较宽松。
term
term是精确查询,不会对查询条件进行分词操作;
该查询方式和match在查询单个词,无法进行分词的信息时,是等价的,查询结果一样;
match_phase
会对查询条件进行分词操作,但是查询的结果是需要包含查询条件所有的分词,而且顺序要是一样;
例:查询“hello world”,则查询结果中必须包含“hello”和“world”,且hello要在world之前,且是连续的,“world hello”不满足,“hello that world”也不满足;
query_string
与match类似,但是match需要指定字段名,query_string是在所有字段中搜索,范围更广泛。
注:目前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 ] [, ...] ) ) ]
例:
查询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 | 制表符分隔符 |
分别统计不同性别50岁以上的总年龄
GET /_sql?format=txt
{
"query": "SELECT sex, sum(age) FROM es_db where age >= 50 group by sex"
}
将SQL转换为DSL
GET /_sql/translate
{
"query": "SELECT sex, sum(age) FROM es_db where age >= 50 group by sex"
}