Elasticsearch文档

这是 Elasticsearch 的官方文档。 你可以在这里找到 elasticsearch 的所有文档。

入门

入门

Elasticsearch 是一个基于 Lucene 库的搜索引擎。它提供了一个分布式、支持多租户的全文搜索引擎,具有HTTP Web接口和无模式JSON文档。

下载

注意: ${VERSION} 需替换为指定版本,官方包有的功能只能试用,完整功能需要付费,请仔细阅读官网文档。

Windows

https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-${VERSION}-windows-x86_64.zip

linux

$ wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-${VERSION}-linux-x86_64.tar.gz

$ wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-${VERSION}-linux-x86_64.tar.gz.sha512

$ shasum -a 512 -c elasticsearch-${VERSION}-linux-x86_64.tar.gz.sha512 

$ tar -xzf elasticsearch-${VERSION}-linux-x86_64.tar.gz

$ cd elasticsearch-${VERSION}/

macos

$ curl -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-${VERSION}-darwin-x86_64.tar.gz

$ curl https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-${VERSION}-darwin-x86_64.tar.gz.sha512 | shasum -a 512 -c - 

$ tar -xzf elasticsearch-${VERSION}-darwin-x86_64.tar.gz

$ cd elasticsearch-${VERSION}/ 

启动

Elasticsearch 和 RDMS 的对比

RDMS elasticsearch
数据库(database) 索引(index)
表(table) 类型(type)
行(row) 文档(document)
列(column) 字段(field)
表结构 映射
索引 全文索引
SQL 查询DSL
SELECT * FROM tablename GET http://...
UPDATE table SET PUT http://...
DELETE DELETE http://...

操作

基础语法规则

$ curl -X<VERB> '<PROTOCOL>://<HOST>:<PORT>/<PATH>?<QUERY_STRING>' -d '<BODY>'

创建索引

统一请求 api 前缀

http://localhost:9200/

DSL语法

PUT /user_info
{
  "settings": { "number_of_replicas": 1, "number_of_shards": 1 },
  "mappings": {
    "properties": {
      "id": { "type": "long", "index": true },
      "username": { "type": "keyword", "index": true },
      "nickname": { "type": "keyword", "index": true },
      "password": { "type": "keyword", "index": false },
      "age": { "type": "integer", "index": true },
      "info": { "type": "text", "index": true },
      "remark": { "type": "text", "index": true }
    }
  }
}

curl

curl -XPUT "http://localhost:9200/user_info" -H 'Content-Type: application/json' -d'{ "settings": { "number_of_replicas": 1, "number_of_shards": 1 }, "mappings": { "properties": { "id": { "type": "long", "index": true }, "username": { "type": "keyword", "index": true }, "nickname": { "type": "keyword", "index": true }, "password": { "type": "keyword", "index": false }, "age": { "type": "integer", "index": true }, "info": { "type": "text", "index": true }, "remark": { "type": "text", "index": true } } } }'

参数说明

其他参数很多,请参考官网资料

删除索引

DSL语法

DELETE /user_info

curl

curl -XDELETE "http://localhost:9200/user_info"

判断索引是否存在

DSL语法

# 查看索引是否存在
HEAD /user_info

curl

# 查看索引是否存在
curl -XHEAD "http://localhost:9200/user_info"

开启/关闭索引

开启DSL语法

POST /user_info/_open

curl

curl -XPOST "http://localhost:9200/user_info/_open"

关闭 DSL 语法

POST /user_info/_close

curl

curl -XPOST "http://localhost:9200/user_info/_close"

索引的别名

POST /user_info/_alias/user1
curl -XPOST "http://localhost:9200/user_info/_alias/user1"
DELETE /user_info/_alias/user1
curl -XDELETE "http://localhost:9200/user_info/_alias/user1"
GET /_alias/user1
curl -XGET "http://localhost:9200/_alias/useraa"

Mapping 操作

类似修改数据库中列的操作

查看 mapping

DSL语法

GET /user_info/_mapping

curl -XGET "http://localhost:9200/user_info/_mapping"

新增 mapping

DSL语法

PUT /user_info/_mapping
{
    "properties":{
        "sex":{ "type":"keyword" }
    }
}

curl -XPUT "http://localhost:9200/user_info/_mapping" -H 'Content-Type: application/json' -d'{ "properties":{ "sex":{ "type":"keyword" } } }'

注意: 需要注意的是字段映射只能增加,不能更改删除

文档的操作

添加文档

新增一条数据 - DSL语法

POST /user_info/_doc/1
{
    "id":1,
    "username":"username",
    "password":"123456",
    "nickname":"nickname",
    "age":18,
    "info":"一些个人相关的介绍",
    "remark":"备注信息",
    "sex":"男"
}

curl -XPOST "http://localhost:9200/user_info/_doc/1" -H 'Content-Type: application/json' -d'{ "id":1, "username":"username", "password":"123456", "nickname":"nickname", "age":18, "info":"一些个人相关的介绍", "remark":"备注信息", "sex":"男" }'

查询指定索引的所有文档

类似数据库中的 select * from user_info;

DSL语法

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

curl -XGET "http://localhost:9200/user_info/_search" -H 'Content-Type: application/json' -d'{ "query": { "match_all": {} } }'

通过 id 查询文档

类似数据库中的 select * from user_info where id = 1;

DSL语法

GET /user_info/_doc/1

curl -XGET "http://localhost:9200/user_info/_doc/1"

模糊查找

类似数据库中的模糊查询 select * from user_info where info like '%人%';

DSL语法

GET /user_info/_search
{
    "query": { "match": { "info": "人" } }
}

通过条件查询文档

类似数据库中的 select * from user_info where username = 'username';

通过条件查询 - DSL语法

GET /user_info/_search
{
    "query": {
        "bool": {
            "must": [ { "term": { "username": "username" } } ]
        }
    }
}

curl -XGET "http://localhost:9200/user_info/_search" -H 'Content-Type: application/json' -d'{ "query": { "bool": { "must": [ { "term": { "username": "username" } } ] } } }'

范围查找

类似数据库中的范围查询 select * from user_info where age between 18 and 30;

DSL语法

GET /user_info/_search
{
    "query": {
        "range": {
            "age": {
                "gt": 18,
                "lt": 30
            }
        }
    }
}

curl -XGET "http://localhost:9200/user_info/_search" -H 'Content-Type: application/json' -d'{ "query": { "range": { "age": { "gt": 18, "lt": 30 } } } }'

and 查询

类似数据库中的 and 查询 select * from user_info where age > 18 and sex = '男';

DSL语法

GET /user_info/_search  
{  
  "query": {
    "bool": {
      "must": [
        { "range": { "age": { "gt": 18 } } },
        { "term": { "sex": "男" } }
      ]   
    }
  }
}

curl -XGET "http://localhost:9200/user_info/_search" -H 'Content-Type: application/json' -d'{ "query": { "bool": { "must": [ { "range": { "age": { "gt": 17 } } }, { "term": { "sex": "男" } } ] } } }'

limit 查找

类似数据库中的 limit 查询 select * from user_info limit 10;

DSL语法

GET /user_info/_search  
{  
    "size": 10,  
    "query": {  
        "match_all": {}     
    }
}

curl -XGET "http://localhost:9200/user_info/_search" -H 'Content-Type: application/json' -d'{ "size": 1, "query": { "match_all": {} } }'

limit offset 查找

类似数据库中的 limit 查询 select * from user_info limit 0,10;

DSL语法

GET /user_info/_search  
{  
    "size": 2,  
    "from": 1,  
    "query": {  
        "match_all": {}  
    }  
}

curl -XGET "http://localhost:9200/user_info/_search" -H 'Content-Type: application/json' -d'{ "size": 2, "from": 1, "query": { "match_all": {} } }'

参数说明

or 查询

类似数据库中的 or 查询 select * from user_info where age > 18 or sex = '男';

DSL语法

GET /user_info/_search
{
    "query": {
        "bool": {
        "should": [
            {
                "range": {
                    "age": { "gt": 18 }
                }
            },
            {
                "term": { "sex": "男" }
            }
        ]
        }
    }
}

curl -XGET "http://localhost:9200/user_info/_search" -H 'Content-Type: application/json' -d'{ "query": { "bool": { "should": [ { "range": { "age": { "gt": 18 } } }, { "term": { "sex": "男" } } ] } } }'

删除文档

删除指定 id

类似数据库中的 delete 查询 delete from user_info where id = 3;

DSL语法

# 删除文档
DELETE /user_info/_doc/3

# 删除文档
curl -XDELETE "http://localhost:9200/user_info/_doc/3"

删除指定条件

类似数据库中的 delete 查询 delete from user_info where age > 18;

DSL语法

POST /user_info/_delete_by_query
{
    "query": {
        "range": { "age": { "gt": 18 } }
    }
}

curl -XPOST "http://localhost:9200/user_info/_delete_by_query" -H 'Content-Type: application/json' -d'{"query":{"range":{"age":{"gt":18}}}}'