Elasticsearch API 基本操作

Elasticsearch API 基本操作

1、与关系型数据库的对应关系

关系型数据库Elasticsearch
数据库 Database索引库 Index
表 Table类型 Type
数据行 Row文档 Document
数据列 Column字段 Field
模式 Schema映像 Mapping

ES Restful API GET、POST、PUT、DELETE、HEAD含义:
1)GET:获取请求对象的当前状态。
2)POST:改变对象的当前状态。
3)PUT:创建一个对象。
4)DELETE:销毁对象。
5)HEAD:请求获取对象的基础信息。

2、索引

相当于数据库

创建索引

http://localhost:9200/{index}
PUT              http://localhost:9200/hello

删除索引

http://localhost:9200/{index}
DELETE              http://localhost:9200/hello

3、mapping

创建索引index和映射mapping

http://localhost:9200/{index}

put:http://localhost:9200/hello2
{
    "mappings": {
        "article": {
            "properties": {
                "id": {
                	"type": "long",
                    "store": true,
                    "index":"true"
                },
                "title": {
                	"type": "text",
                    "store": true,
                    "index":"true",
                    "analyzer":"standard"
                },
                "content": {
                	"type": "text",
                    "store": true,
                    "index":"true",
                    "analyzer":"standard"
                }
            }
        }
    }
}

创建索引后设置Mapping

http://localhost:9200/{index}/{type}/_mapping

POST	http://localhost:9200/hello/type_test/_mapping

{
    "hello": {
            "properties": {
                "id":{
                	"type":"long",
                	"store":true
                },
                "title":{
                	"type":"text",
                	"store":true,
                	"index":true,
                	"analyzer":"standard"
                },
                "content":{
                	"type":"text",
                	"store":true,
                	"index":true,
                	"analyzer":"standard"
                }
            }
        }
  }

4、文档

http://localhost:9200/{index}/{type}/{_id}

4.1新增文档

http://localhost:9200/{index}/{type}/{_id}

post可以不设置_id.es会自动生成,不建议
put/post  http://localhost:9200/hello/article/1

{
	"id":1,
	"title":"ElasticSearch是一个基于Lucene的搜索服务器",
	"content":"它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。"
}


4.2修改文档document

http://localhost:9200/{index}/{type}/{_id}
POST	localhost:9200/blog1/article/1
请求体:
{
	"id":1,
	"title":"【修改】ElasticSearch是一个基于Lucene的搜索服务器",
	"content":"【修改】它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。"
}

4.3删除文档document

http://localhost:9200/{index}/{type}/{_id}

DELETE	localhost:9200/blog1/article/1

4.4文档查询

4.4.1 根据id查找
http://localhost:9200/{index}/{type}/{_id}

GET	localhost:9200/blog1/article/1

4.4.2 querystring查询

先分词后查询,包含分词都会查询出来

POST	localhost:9200/blog1/article/_search

{
    "query": {
        "query_string": {
            "default_field": "title",
            "query": "搜索服务器"
        }
    }
}
4.4.2 term查询

代表完全匹配,即不进行分词器分析,文档中必须包含整个搜索的词汇

POST	localhost:9200/blog1/article/_search

{
    "query": {
        "term": {
            "title": "搜索"
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值