文章目录
1、_index
:文档在哪存放
2、_type
:文档表示的对象类别
3、_id
:文档唯一标识
1、如果在Put数据的时候没有指定Id,那么会自动生成一个唯一的ID,如下:
POST /website/blog/
{
"title": "My second blog entry",
"text": "Still trying this out...",
"date": "2014/01/01"
}
//生成的数据如下:
{
"_index": "website",
"_type": "blog",
"_id": "AWVlzX7Ur7Pz0UFcO81u", //自动生成的Id
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}
1、ElasticSearch为了实现并发访问,每次实行更新、删除、添加之后都会为版本号自增1。
2、在删除,更新的之前会保存这个版本号,更新删除操作执行完成之后会自动比较此时的version的值,如果不同,表示被别的进程改变了,一次保证并发的安全性
1、当我们检索字段的时候,默认会为我们查询全部的文档字段,但是我们 可以使用_source
指定返回的字段,如下:
GET /website/blog/123?_source=title,text
1、使用HEAD
风格的请求方式:
HEAD /website/blog/123
1、文档的确定有三个元数据指定:index,type,id
2、创建文档的时候,如果想不覆盖原来的文档,那么需要检查文档是否存在,如果存在就不插入,有两种方式,如下:
1、使用op_type=create
,如果文档已经存在了,那么返回409状态码。如果创建成功了,那么会返回201 Created
PUT /website/blog/123?op_type=create
{
"title": "My first blog entry",
"text": "Just trying this out...",
"date": "2014/01/02"
}
2、使用_create
,如果失败返回409,如果创建成功了,那么会返回201 Created
PUT /website/blog/123/_create
{
"title": "My first blog entry",
"text": "Just trying this out...",
"date": "2014/01/02"
}
1、ElasticSearch可以批量获取多个文档中的数据,也可以批量获单个文档中的多条数据
1、使用mgetAPI
批量获取文档
1、docs
:指定了要获取的文档
2、_source
:指定需要返回的字段,不指定,默认返回全部的字段
GET /_mget
{
"docs" : [
{
"_index" : "website", //文档的索引
"_type" : "blog", //文档类型
"_id" : 2 // Id
},
{
"_index" : "website",
"_type" : "pageviews",
"_id" : 1,
"_source": "views"
}
]
}
2、响应的内容也是和请求的顺序一致,如下:
1、查询到的数据存储在docs
数组中,字段封装在_source
中
{
"docs" : [
{
"_index" : "website",
"_id" : "2",
"_type" : "blog",
"found" : true,
"_source" : {
"text" : "This is a piece of cake...",
"title" : "My first external blog entry"
},
"_version" : 10
},
{
"_index" : "website",
"_id" : "1",
"_type" : "pageviews",
"found" : true,
"_version" : 2,
"_source" : {
"views" : 2
}
}
]
}
1、如果只是获取单个文档中的多条数据,那么只需要在GET请求中指定index和type即可,此时只需要传入不同数据的Id即可,如下:
1、其中ids
是一个数组,用来封装数据的Id
GET /website/blog/_mget
{
"ids" : [ "2", "1" ]
}
2、请求成功的顺序是按照ids数组中的Id返回的,如下:
{
"docs": [
{
"_index": "website",
"_type": "blog",
"_id": "123",
"_version": 1,
"found": true,
"_source": {
"title": "My first blog entry",
"text": "Just trying this out...",
"date": "2014/01/01"
}
},
{
"_index": "website",
"_type": "blog",
"_id": "1",
"_version": 2,
"found": true,
"_source": {
"title": "My first blog entry",
"text": "Starting to get the hang of this..."
}
}
]
}
2、即使在GET请求中指定了index和type的值,但是我们也可以在下面覆盖他们的值,这样就可以查询不同文档或者不同索引中的数据了,如下:
1、不想覆盖的可以不用自定index和type的值,只需要指定的id
的值即可
GET /website/blog/_mget
{
"docs" : [
{ "_id" : 2 },
{ "_type" : "pageviews", "_id" : 1 }
]
}
1、https://elasticsearch.cn/book/elasticsearch_definitive_guide_2.x/bulk.html
1、返回集群下的所有的索引和文档,不需要指定索引、文档、id。默认只是返回前10条
GET /_search
2、返回结果中的各个值的含义参见:https://elasticsearch.cn/book/elasticsearch_definitive_guide_2.x/empty-search.html#_shards
1、/_search
:在所有的索引中搜索所有的类型
2、/gb/_search
:在 gb
索引中搜索所有的类型
3、/gb,us/_search
:在 gb
和 us
索引中搜索所有的文档
4、/g*,u*/_search
:在任何以 g
或者 u
开头的索引中搜索所有的类型
5、/gb/user/_search
:在 gb
索引中搜索 user
类型
6、/gb,us/user,tweet/_search
:在 gb
和 us
索引中搜索 user
和 tweet
类型
7、/_all/user,tweet/_search
:在所有的索引中搜索 user
和 tweet
类型
1、https://elasticsearch.cn/book/elasticsearch_definitive_guide_2.x/pagination.html#pagination
2、size
:显示应该返回的结果数量,默认是 10
3、from
:显示应该跳过的初始结果数量,默认是 0
4、实例如下:
GET /_search?size=5 //查询前5条的数据
GET /_search?size=5&from=5 //查询第2页,每页显示5条数据
GET /_search?size=5&from=10 //查询第3页的数据,每页显示5条数据
5、分布式中的深度分页的问题,可以在文档中查看详细信息。总之分页请求的数据不要超过1000条
6、在 重新索引你的数据 中解释了如何 能够 有效获取大量的文档。
0、https://elasticsearch.cn/book/elasticsearch_definitive_guide_2.x/search-lite.html#query-string-query
1、返回group索引中文档类型为employee
中的内容,筛选条件为:文档中的所有字段的值只要有等于Smith
的都返回,如下:
1、_all
:用于指定对文档中的所有字段进行筛选,只要有一个字段成立,那么这个文档就会被选中,但是返回的结果中有相关度的分析
2、如果不指定任何字段,那么默认就是使用_all
进行筛选
GET /group/employee/_search?q=Smith
//这条语句的效果和上面的语句一样
GET /_all/employee/_search?q=_all:Smith
2、查询一个文档中指定字段的值等于Smith
,如下我们查询文档中的字段last_name
这个值等于Smith的所有文档内容
GET /group/employee/_search?q=last_name:Smith
1、https://elasticsearch.cn/book/elasticsearch_definitive_guide_2.x/inverted-index.html
2、创建规范:
1、单词不区分大小写
2、不区分单复数,将单数和复数的单词一律提取出来单数即可
3、意思相近的词只提取一个
1、https://elasticsearch.cn/book/elasticsearch_definitive_guide_2.x/analysis-intro.html
2、什么时候使用分词器?
1、查找全文域的时候使用,比如使用轻量搜索中的_all
2、精确查找不使用分词器,比如Date类型的数据,默认将其看成单独的一个词条
3、测试分词器
GET /_analyze
{
"analyzer": "standard", //指定分词器
"text": "Text to analyze" //指定需要分割的字符串
}
返回结果如下:
1、token
是实际存储到索引中的词条。 position
指明词条在原始文本中出现的位置。 start_offset
和 end_offset
指明字符在原始字符串中的位置。
{
"tokens": [
{
"token": "text",
"start_offset": 0,
"end_offset": 4,
"type": "<ALPHANUM>",
"position": 0
},
{
"token": "to",
"start_offset": 5,
"end_offset": 7,
"type": "<ALPHANUM>",
"position": 1
},
{
"token": "analyze",
"start_offset": 8,
"end_offset": 15,
"type": "<ALPHANUM>",
"position": 2
}
]
}
1、https://elasticsearch.cn/book/elasticsearch_definitive_guide_2.x/mapping-intro.html
2、Elasticsearch 支持 如下简单域类型:
字符串: string
整数 : byte, short, integer, long
浮点数: float, double
布尔型: boolean
日期: date
3、如果我们在添加数据的时候使用双引号包裹起来的,那么会被动态映射为字符串类型。
1、我们在查询文本的时候可能会同时会对多个文本中的内容进行查询匹配,那么就需要对多个字段进行筛选了,比如我们需要对name和address这两个字段同时进行匹配,那么效率肯定会比对一个字段进行筛选的低,因此可以使用copy_to字段将这两个字段中的内容存储在同一个字段中,那么就可以实现对一个字段的查询等同于对两个字段的查询,如下:
PUT lib/user/_mapping
{
"properties": {
"name":{
"type": "text",
"copy_to": "name_address" //将name这个字段的文本内容复制到name_address中
},
"age":{
"type": "integer"
},
"address":{
"type": "text",
"copy_to": "name_address" // //将address这个字段的文本内容复制到name_address中
}
}
}
2、此时就可以使用query查询了,如下:查询名字为Jack,address为jiangsusheng的
GET /lib/user/_search
{
"query": {
"match": {
"name_address": "Jack,jiangsusheng" //直接使用name_address即可
}
}
}
//等同于
GET /lib/user/_search
{
"query": {
"bool": {
"must": [
{"term":{"name":"Jack"}},
{"match":{"address":"jiangsusheng"}}
]
}
}
}
text
类型,将会被分词,因此不能进行排序,但是我们知道字符串类型中有一个keyword
类型不会被分词,因此我们可以再为需要排序的字符串字段指定一个keyword
类型的字段用于排序,如下:PUT /lib4
{
"settings":{
"number_of_shards":5, //主分片的数量
"number_of_replicas":0 //副本分片的数量
},
"mappings":{
"user":{
"properties": {
"name":{
"type": "text",
"copy_to": "name_address",
"fields": { //使用fields指定另外一个字段raw
"raw":{
"type": "keyword" //类型为keyword
}
},
"fielddata": true //将fielddata指定为true
},
"age":{
"type": "integer"
},
"address":{
"type": "text",
"copy_to": "name_address"
}
}
}
}
}
//排序如下:
GET /lib4/user/_search
{
"query": {
"match": {
"age":22
}
},
"sort": [
{
"name": { //对字符串类型的name进行排序
"order": "desc"
}
}
]
}