索引管理 新建索引: PUT blog
索引不能出现大写。 elasticsearch默认给一个索引设置5个分片1个副本,一个索引的分片数一旦指定不能修改,副本数能够经过命令setting 参数在索引设置初始化的时候建立: 例如:设置bolg索引 3个 分片 0个副本 PUT bolg { “settings”:{ “number_of_shards” :3, "number_of_replicas" :0 } }java
更新副本: PUT blog/_settings { "number_of_replicas" : 2 } 设置索引的读写权限: blocks.read_only:true 设置当前索引只容许读不容许写或者更新。 blocks.read:true 禁止对当前索引进行读操做。 blocks.write:true 禁止对当前索引进行写操做。python
查看索引: GET blog/_settings 查看多个索引信息 GET blog1,blog2/_settings 查看全部的索引信息 GET _all/_settings编程
删除索引 DELETE blogapp
打开和关闭索引 POST blog/_close POST blog/_openelasticsearch
若是不指定会自动生成id 可是必须使用POST POST blog/article { "id":1, "title" :"GIT 简介", “content”:“GIT是一款免费的、开源的分布式版本控制系统” }分布式
获取文档: GET blog/article/1 根据id获取多个文档 GET blog/article/_mget{ "ids" : ["1","2"] }版本控制
更新文档blog
POST test/type1/1/_update {}索引
删除文档ip
DELETE blog/article/1
建立索引
PUT books { "mappings": { "job": { "properties": { "companyName": { "type": "text", "analyzer": "ik_max_word", "fields": { "suggest" : { "type" : "completion", "analyzer": "ik_max_word" } } }, "jobTitle": { "type": "text", "analyzer": "ik_max_word", "fields": { "suggest" : { "type" : "completion", "analyzer": "ik_max_word" } } }, "companyLogo": { "type": "keyword" }, "companyId": { "type": "long" }, "address": { "type": "keyword" }, "degree": { "type": "integer" }, "minSalary": { "type": "integer" }, "maxSalary": { "type": "integer" }, "jobNature": { "type": "integer" }, "status": { "type": "integer" }, "source": { "type": "keyword" }, "modifyStamp": { "type": "long" }, "workLife": { "type": "integer" }, "industryId": { "type": "integer" }, "companyType": { "type": "integer" }, "peopleNum": { "type": "integer" } , "payType": { "type": "integer" }, "jobCategory": { "type": "integer" }, "cityNumber": { "type": "integer" }, "cityName": { "type": "keyword" }, "welfare": { "type": "text", "analyzer": "ik_max_word" }, "describe": { "type": "text", "analyzer": "ik_max_word" } } } } }
_source 要显示的字段 version 显示文档编号 min_score 最低评分
GET books/_search { “min_score”:0.6, “_source” : ["title","author"], "version":true, “query”:{ “from”:0, “size”:100, “term” :{“title”:“思想”} }, "highlight": { "fields": { "jobTitle": {} } } }
**term query **是词项查询(条件是什么,就是什么,再也不分词) ,例如 “java编程” 若某个title 是“java编程思想” 分词后是 “java”,“编程”,“思想” ,则查询不到
match query 会对查询语句进行分词,分词后查询语句中的任何一个词项被匹配,文档就会被搜索到,若想要查询匹配全部的关键词的文档,能够用and操做符链接。 GET books/_search { "query":{ "match" :{ "title" :{ "query":"java编程思想", “operator” : “and ” } } } } match_phrase query 首先会把query内容分词,分词器能够自定义,同时文档还要知足如下两个条件。 一、分词后全部词项都要出如今该字段中。 二、字段中的词项顺序要一致。 GET test/_search { "query":{ "match_phrase":{"foo":"hello word"} } } match_phrase_prefix 和 match_phrase 相似,只不过支持最后一个term前缀匹配。
GET test/_search { "query":{ "match_phrase_prefix":{"foo":"hello w"} } }
multi_match query 是match的升级,用于搜索多个字段。支持通配符。查询语句“java编程”,查询域为title和description 查询语句以下, 同时能够指定搜索字段的权重,指定关键词出如今title中的权重是出如今description的3倍。 : GET books/_search{ "query":{ "multi_match":{ "query" : "java 编程", “fields”:[“title^3”,“description”,“*_name”] } } }
**terms query **是term查询的升级,能够用来查询文档中包含多个词的文档。好比想查询title字段中包含关键词“java”或者“python”的文档,构造查询语句
GET books/_search { "query":{ "terms":{ "title":["java","python"] } } }
range query 匹配在某一范围内的数值型。 GET books/_search { "query":{ "range":{ "price":{ "gt":50, "lte":70 } } } }
exists query 查询会返回字段中至少有一个非空值的文档。 {“query”:{ "exists":{ "field":"user" } }} 查询user字段不为 null 、[]、
prefix query 前缀查询 { “query”:{ “prefix”:{ “description” :“win” } } }
wildcard query通配符查询 GET books/_search { “query”:{ “wildcard”:{ “title” :“蓝瘦*菇” } } }
** bool query **能够把任何多的简单查询组合在一块儿,使用must、should、must_not、filter must 必须匹配至关于AND should 至关于OR must_not 与 must相反、匹配该先想下的查询条件不会返回。 filter 和 must 同样,匹配filter选项下的查询条件下 才能返回,可是filter不评分,不包含虚拟机的书籍,构造bool查询语句以下: GET books/_search{ “query”:{ “bool”:{ “minimum_should_match” :1, "must":{ "match" : {"title":"java"} }, "should":{ "match":{"description":"虚拟机 "} } } } }