ES官方文档中文版:Elasticsearch: 权威指南 | Elastic
Elasticsearch是面向文档的,意味着它存储整个对象或文档。Elasticsearch 不仅存储文档,而且索引每个文档的内容,使之可以被检索。在Elasticsearch中,我们对文档进行索引、检索、排序和过滤,而不是对行列数据。这是一种完全不同的思考数据的方式,也是 Elasticsearch 能支持复杂全文检索的原因。
docker es限制内存启动命令:
docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9200:9200 -p 9300:9300
使用Postman向ES发送put请求进行文档的存储/更新:https://ip:9200/索引/类别/文档
使用Postman向ES发送get请求进行文档的获取:https://ip:9200/索引/类别/文档
使用Postman向ES发送head请求进行文档是否存在,200存在,404不存在:https://ip:9200/索引/类别/文档
使用Postman向ES发送delete请求进行文档的删除:https://ip:9200/索引/类别/文档
查询所有文档所有 get方法:https://ip:9200/索引/类别/_search
查询满足条件的文档 get方法:https://ip:9200/索引/类别/_search?q=键:值
使用查询表达式搜索满足条件的文档 post方法:https://ip:9200/索引/类别/_search
加上一个表达式的json
全文搜索,match
match会进行分词查找!!!
{
"query" : {
"match" : {"键" : "值"}
}
}
短语搜索,match_phrase
精确匹配一系列单词或者短语
{
"query" : {
"match_phrase" : {"键" : "值"}
}
}
但是只能单个条件匹配,如果match里写了多对匹配规则会报错:
"reason": "[match] query doesn't support multiple fields, found [键1] and [键2]"
如果多条件需要使用复合查询bool(must,must_not,should,filter):
条件匹配,must
{
"query" : {
"bool":{
"must":{"match" : {"键1": "值1"}},
"must":{"match" : {"键2": "值2"}}
}
}
}
条件排除,must_not
{
"query" : {
"bool":{
"must":{"match" : {"键1": "值1"}},
"must_not":{"match" : {"键2": "值2"}}
}
}
}
条件匹配到了就展示,否则就不展示,should(如果匹配上得分会有不同)
当使用should查询时,如果包含了must或者filter查询,那么should的查询语句就不是或者的意思了,而是有或者没有都行的含义
{
"query" : {
"bool":{
"must":{"match" : {"键1": "值1"}},
"should":{"match" : {"键2": "值2"}}
}
}
}
如果没有filter和must查询的话,那么必须满足一个should中的条件,表示或者
{
"query" : {
"bool":{
"should":{"match" : {"键1": "值1"}},
"should":{"match" : {"键2": "值2"}
}
}
}
}
精确匹配,term/terms
单个匹配是term;多个是terms
term/terms是不分词查找!!!
{
"query" : {
"bool":{
"must":{"terms" : {"键1": ["值11","值12"]}},
"should":{"term" : {"键2": "值2"}
}
}
}
}
存在match搜索可以搜索到但term搜索不到的问题,原因是表中关键词存储的格式是text类型存储的,text类型数据默认是分词的,所以精确匹配匹配不到。将text类型改成keyword类型即可实现功能。
范围过滤,range
大于:gt
小于:lt
大于等于:gae
小于等于:lte
{
"query" : {
"bool":{
"must":{"range" : {"键": {"gt":"值1","lt":"值2"}}
}
}
}
}
文档中是否包含某个字段或者是没有某个字段,exists
{
"query" : {
"bool":{
"must":{"exists":{"field": "字段"}}
}
}
}
}
{
"query" : {
"bool":{
"must_not":{"exists":{"field": "字段"}}
}
}
}
}
过滤条件,filter
{
"query" : {
"bool": {
"must": {"match" : {"键" : "值"}},
"filter": {"range" : {"条件" : { "gt" : 范围 }}
}
}
}
}
高亮搜索,highlight
{
"query" : {
"match_phrase" : {"键" : "值"}
},
"highlight": {
"fields" : {"键" : {}}
}
}
来源:CSDN
作者:是我弄得嘛
链接:https://blog.csdn.net/qq_38425719/article/details/104473828