ElasticSearch搭建社工库-基本操作
2016-8-19 小屿 ElasticSearch
只是记录自己所学到理解的东西,不一定正确,如果有误欢迎指正!
基本结构如下,就像普通数据库的 库-表-行-列
Elasticsearch -> Indices 索引 -> Types 类型 -> Documents 文档 -> Fields 字段
ElasticSearch的操作就是不同类型的请求,get post put delete ...
创建索引和基本设置
1.区分大小写curl -XPOST '127.0.0.1:9200/test' -d '{ "settings":{ "number_of_shards":3, "number_of_replicas":0 }, "mappings":{ "type1":{ "_source":{"enabled":true}, "properties":{ "field1":{"type":"string", "index":"not_analyzed" } } } } }'2.不区分大小写 (自定义分析器全部转换为小写)
curl -XPOST '127.0.0.1:9200/test' -d '{ "settings":{ "number_of_shards":3, "number_of_replicas":0, "analysis": { "analyzer": { "my_analyzer": { "type": "custom", "tokenizer": "standard", "filter": [ "lowercase" ] }} } }, "mappings":{ "type1":{ "_source":{"enabled":true}, "properties":{ "field1":{"type":"string", "index":"not_analyzed" } } } } }'设置索引为test 类型为type1 文档为field1
设置分片为3 副本为0
分片会占用系统资源,设置分片数量应该考虑今后的集群扩展,分片设置后无法修改,集群服务器后分片会分配到各个服务器
副本也会占用系统资源,副本设置为1即为每个分片都有一个副本,没有集群服务器即使设置了副本的也没有地方存放,副本设置后可以修改数量,比如从0修改为1:
curl -XPUT 'localhost:9200/test2/_settings' -d '{ "index" : { "number_of_replicas" : 1 } }'
例如 有2个服务器节点 设置了1份副本冗余, 两个节点内容一样,一个是分片一个是冗余, 3个节点1份冗余,将会把2份数据(分片和冗余)分散在3台机器上,3个节点2份冗余,三个节点内容一样,每个节点既有分片也有副本。
"_source":{"enabled":true}, 作用似乎是使搜索结果获得完整文档,如果设置会false即使查询到就只返回id等而不返回内容,默认不设置也为"_source":{"enabled":true}, 网上有的文章会设置“_source”:{“enabled”:false}, 不明白为什么会这样。
type指定字段类型。
"index":"not_analyzed"作用是不分词,如果不设置搜索将无法匹配到@等特殊符号。原因可能是@被当成停用词处理了。
运行以下命令可以查看设置(pretty作用是显示更美观):curl -XGET 'http://127.0.0.1:9200/_cluster/health?pretty' curl -XGET 'http://127.0.0.1:9200/_mapping?pretty'
插入数据
curl -XPUT 'http://127.0.0.1:9200/test/type1/1' -d '{ "user" : "小屿", "post_date" : "2016-11-15T14:12:12", "field1" : "小屿@gamail.com" }'
在没有自己创建索引和类型的情况下也可以直接这样PUT数据,ElasticSearch会创建Indices和Types并自动判断数据类型。
bulk插入多行数据:
curl -XPOST 'http://127.0.0.1:9200/_bulk' -d ' { "create": { "_index": "test", "_type": "type1", "_id": "1" }} { "field1" : "[email protected]", "name" : "John Smith", "username" : "@john" } { "create": { "_index": "test", "_type": "type1", "_id": "2" }} { "field1" : "[email protected]", "name" : "Mary Jones", "username" : "@mary" } { "create": { "_index": "test", "_type": "type1", "_id": "3" }} { "field1" : "[email protected]", "name" : "John Smith", "username" : "@john" } { "create": { "_index": "test", "_type": "type1", "_id": "4" }} { "field1" : "[email protected]", "name" : "Mary Jones", "username" : "@mary" } '
_index和_type也可以在post的url中指定
在插入数据过程中不指定id也会自动生成一个id,但设置id在导入数据时可以避免重复,同一类型下如果id相同是无法覆盖的。查询数据
curl -XGET 'http://127.0.0.1:9200/test/type1/1'mget查询多个数据
curl -XPOST 'http://127.0.0.1:9200/_mget?pretty' -d '{ "docs" : [ { "_index" : "test", "_type" : "type1", "_id" : 2 }, { "_index" : "test", "_type" : "type1", "_id" : 1 } ] } '_index和_type也可以在post的url中指定
curl -XPOST 'http://127.0.0.1:9200/test/type1/_mget?pretty' -d '{ "ids" : [ "1", "2" ] } '
搜索数据
curl -XGET 'http://localhost:9200/_search'默认显示10个结果
curl -XGET 'http://localhost:9200/_search?size=2&pretty'size指定结果输出个数
curl -XGET 'http://localhost:9200/_search?q=field1:[email protected]'q=指定搜索条件
curl -XGET 'http://127.0.0.1:9200/test/type1/_search?pretty' -d '{ "query": { "match_all": {}} }'
返回数据中的 took为响应时间,单位是毫秒。
_shards为参与查询的分片,成功失败的数量
time_out为超时,可在链接自定义timeout=1000ms,则只输出一秒内查询到的结果
hits中total为查询到的匹配结果数量
模糊查询
curl -XGET 'http://127.0.0.1:9200/_search?pretty' -d '{ "query": { "wildcard": { "field1": "*gamail*" } } }'
模糊查询中 ? 匹配任意字符,* 匹配0个或多个字符
_all在ElasticSearch中指代所有的意思,如果这里把field1修改成_all意为搜索所有文档的字段,但是这样会消耗更多性能,并且会使设置的not_analyzed失去效果,就匹配不到@等特殊符号了。_all也同样可以指代所有索引、类型、文档等。
正则匹配
查询替换
wildcard为regexp即可使用正则
curl -XGET 'http://127.0.0.1:9200/_search?pretty' -d '{ "query": { "regexp": { "field1": ".*?" } } }'
更新数据
指定索引类型和id直接PUT覆盖就行了,更新数据也有专门的_update参数。
删除数据
curl -XDELETE 'http://127.0.0.1:9200/_all' 删除所有索引 curl -XDELETE 'http://127.0.0.1:9200/test' 删除test索引下所有数据 curl -XDELETE 'http://127.0.0.1:9200/test/type1' 删除test索引type1类型下所有数据(这个版本已经不能删除type,可以使用插件delete-by-query) curl -XDELETE 'http://127.0.0.1:9200/test/type1/1' 删除test索引type1类型下id为1的数据
标签: ElasticSearch
你是衣冠楚楚的人 而我只是一个打满补丁的猴子
-
小博客一个,没必要伤害她
热门文章
存档
标签
最新评论
- yz
想想你喜欢什么,想做什么,找好一个自己的... - 小屿
@Jahan:testfun1024#p... - Jahan
Hello dear Xia0 i a... - brave
@万:你的手机应该是anroid7.0以... - jhsy
新版的cookie机制应该又变了. 而且... - 小屿
@janto:无兴趣 - janto
新版的这些好像不起作用了,deviceI... - hunk
正在研究,可否发一份新源码?todz$1... - miffy
请问可以加个好友咨询下吗? - vegetableChicken
@Snkrs:我也遇到和你一样的问题了,...
发表评论: