天天看点

Elasticsearch API 基本操作Elasticsearch API 基本操作

Elasticsearch API 基本操作

文章目录

  • Elasticsearch API 基本操作
    • 1、与关系型数据库的对应关系
    • 2、索引
      • **创建索引**
      • **删除索引**
    • 3、mapping
      • 创建索引index和映射mapping
      • 创建索引后设置Mapping
    • 4、文档
      • 4.1新增文档
      • 4.2修改文档document
      • 4.3删除文档document
      • 4.4文档查询
        • 4.4.1 根据id查找
        • 4.4.2 querystring查询
        • 4.4.2 term查询

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": "搜索"
        }
    }
}

           

继续阅读