Elasticsearch---学习记录(1)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/D_iRe_Wol_F/article/details/83090154

仅供自己作学习笔记,详情请移步es官方文档

1.问题------windows的cmder使用

curl -XPUT http://172.16.150.149:29200/facebook -d {"setting":{ "index":{ "number_of_shards":3,"number_of_replicas":2}}}

报错:

{"error":{"root_cause":
[{"type":"parse_exception",
"reason":"failed to parse source for create index"}],
"type":"parse_exception",
"reason":"failed to parse source for create index",
"caused_by":{"type":"json_parse_exception",
"reason":"Unexpected end-of-input: expected close marker for OBJECT (from [Source: [B@14f7b2cb; line: 1, column: 1])\n 
at [Source: [B@14f7b2cb; line: 1, column: 21]"}},"status":400}curl: (3) [globbing] unmatched brace in column 7
curl: (3) [globbing] unmatched close brace/bracket in column 40

解决:

windows下使用 -d "{}"(双引号)

ps:检查json格式网页工具

2.问题------windows下的双引号值

curl -XPUT http://172.16.150.149:29200/facebook -d "{ "settings":
{"number_of_shards":1},
"mappings":{"type1":{"_source":{" enabled":false},
"preperties":{"field1":{""type"":""string"","index":"not_analyzed"}}}}}"

{"error":{"root_cause":[{"type":"parse_exception",
"reason":"failed to parse source for create index"}],
"type":"parse_exception",
"reason":"failed to parse source for create index",
"caused_by":{"type":"json_parse_exception","reason":"Unrecognized token 'not_analyzed':
was expecting 'null', 'true', 'false' or NaN\n at [Source: [B@4cff3d19; line: 1, column: 129]"}},"status":400}

总是显示只能是true,null,false,但是这里就需要自己手动设置.

解决:

参考以后,发现windows真的坑,value里面如果是自定义的情况,比如type:string这种,就需要{""type"":""string""}形式.

3.记录------为index创建_type,_id

跟着教程走,直接在创建索引的时候就进行设置.
这里直接在URL里写入/blog/123就直接在创建的时候,分别代表了_type,_id字段

Win下创建:

curl -XPUT http://172.16.150.149:29200/facebook/blog/123 -d"{""title"":""website"",""text"":""blog is making"",""date"": ""2018/1016""}"


{"_index":"facebook","_type":"blog","_id":"123","_version":1,"_shards":{"total":3,"successful":1,"failed":0},"created":true}

Ubuntu下创建:

curl -XPUT http://172.16.150.149:29200/twitter/big/777 -d '{"title":"bigweb","text":"nothing","date":"2018/10/18"}'
{"_index":"twitter","_type":"big","_id":"777","_version":1,"_shards":{"total":1,"successful":1,"failed":0},"created":true}

再用pretty查看一下结构

curl -XGET http://172.16.150.149:29200/facebook/blog/123?pretty
{
  "_index" : "facebook",
  "_type" : "blog",
  "_id" : "123",
  "_version" : 1,
  "found" : true,
  "_source" : {
"title" : "website",
"text" : "blog is making",
"date" : "2018/1016"
  }
}

4.记录------查看_source字段中的数据

  • 不需要元数据,查找整个_source端点:

    curl -i -XGET http://172.16.150.149:29200/facebook/blog/123/_source
    HTTP/1.1 200 OK
    Content-Type: application/json; charset=UTF-8
    Content-Length: 62
    
    {"title":"website","text":"blog is making","date":"2018/1016"}
    
  • 查找某个元数据:

    curl -i -XGET http://172.16.150.149:29200/facebook/blog/123?_source=text
    HTTP/1.1 200 OK
    Content-Type: application/json; charset=UTF-8
    Content-Length: 110
    
    {"_index":"facebook","_type":"blog","_id":"123","_version":1,"found":true,"_source":{"text":"blog is making"}}
    

5.记录------文档标识

_index,_type,_id的组合可以唯一标识一个文档

6.记录—删除文档

正如已经在更新整个文档中提到的,删除文档不会立即将文档从磁盘中删除,只是将文档标记为已删除状态。随着你不断的索引更多的数据,Elasticsearch 将会在后台清理标记为已删除的文档。

7.记录------乐观并发控制

es内部修改

当我们之前讨论 index , GET 和 delete 请求时,我们指出每个文档都有一个 _version (版本)号,当文档被修改时版本号递增。 Elasticsearch 使用这个 _version 号来确保变更以正确顺序得到执行。如果旧版本的文档在新版本之后到达,它可以被简单的忽略。

个人理解:就是加一个tag,然后不同的节点修改相同Document, 提交,都设置version=相同值

举个例子,version=13

 curl -i -XPOST http://172.16.150.149:29200/facebook/blog/123?version=13 -d "{""title"":""change version num"",""text"":""cha nging...""}"
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Content-Length: 126

{"_index":"facebook","_type":"blog","_id":"123","_version":14,"_shards":{"total":3,"successful":1,"failed":0},"created":false}

再做一次修改的时候,各个节点怎么才让自己的是最新的呢?

一般的猜测都是直接尽可能设置version值越大越好,但那是不可能的.

curl -i -XPOST http://172.16.150.149:29200/facebook/blog/123?version=20 -d "{""title"":""change version num"",""text"":""cha nging...""}"
HTTP/1.1 409 Conflict
Content-Type: application/json; charset=UTF-8
Content-Length: 329

{"error":{"root_cause":[{"type":"version_conflict_engine_exception",
"reason":"[blog][123]:version conflict, current [14], provide[20]","shard":"2","index":"facebook"}],
"type":"version_conflict_engine_exception",
"reason":"[blog][123]: version conflict, current [14], provided [20]","shard":"2","index":"facebook"},"status":409}

只能请求当前版本号,小于version和大于version都不行,那么就如官方文档所说,只能看哪个节点先更改好了,后到就冲突.

外部系统修改

Elasticsearch 不是检查当前 _version 和请求中指定的版本号是否相同, 而是检查当前 _version 是否 小于 指定的版本号。 如果请求成功,外部的版本号作为文档的新 _version 进行存储。

使用version_type,又报了错…

curl -i -XPUT http://172.16.150.149:29200/facebook/blog/123?version=14&version_type=external -d "{""title"":""change version num"",""text"":""changing...""}"
HTTP/1.1 400 Bad Request
Content-Type: application/json; charset=UTF-8
Content-Length: 201

{"error":{"root_cause":[{"type":"mapper_parsing_exception","reason":"failed to parse, document is empty"}],"type":"mapper_parsing_exception","reason":"failed to parse, document is empty"},"status":400}'version_type' 不是内部或外部命令,也不是可运行的程序或批处理文件。

解决:

url又要要求双引号了,猜测是不是还是因为前面所说的参数问题.

curl -i -XPUT "http://172.16.150.149:29200/facebook/blog/123?version=120&version_type=external" -d "{""title"" :""change version num"",""text"":""changing...""}"
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Content-Length: 127

{"_index":"facebook","_type":"blog","_id":"123","_version":120,"_shards":{"total":3,"successful":1,"failed":0},"created":false}

8.记录—update部分更新

文档是不可变的:他们不能被修改,只能被替换。 update API 必须遵循同样的规则。 从外部来看,我们在一个文档的某个位置进行部分更新。然而在内部, update API 简单使用与之前描述相同的 检索-修改-重建索引 的处理过程。 区别在于这个过程发生在分片内部,这样就避免了多次请求的网络开销。通过减少检索和重建索引步骤之间的时间,我们也减少了其他进程的变更带来冲突的可能性。

curl -i -XPOST "http://172.16.150.149:29200/facebook/blog/123/_update" -d "{"doc":{""tags"":[""testing""],"vie ws":0}}"
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Content-Length: 111

{"_index":"facebook","_type":"blog","_id":"123","_version":121,"_shards":{"total":3,"successful":1,"failed":0}}

猜你喜欢

转载自blog.csdn.net/D_iRe_Wol_F/article/details/83090154