微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ElasticSearch使用笔记

环境

地址

ElasticSearch

kibana

head (建议使用浏览器插件,在谷歌商店里面搜索ElasticSearch Head)

目前差不多使用这些,还有一些插件,以后再弄。

安装

首先这些都是压缩包,解压即可。我这里下载两个版本,一个最新的7.13.0和7.8.0.用7.8.0演示。

​​​​​​​启动elasticsearch:进入文件夹的bin目录下,双击elasticsearch.bat即可。

如果出现中文乱码情况,则进入config目录下的jvm.options,在最末尾添加如下代码

-Dfile.encoding=GBK

然后浏览器输入:http://localhost:9200/

这个时候点开elasticsearch head 插件是没有办法看到任何信息的,因为elasticsearch是9200端口,而elasticsearch head是9100端口,需要解决跨域问题。

同样进入elasticsearch的config目录,打开elasticsearch.yml文件,在最末尾添加

http.cors.enabled: true
http.cors.allow-origin: "*"

再重启elasticsearch,然后再打开elasticsearch head。可以看到

接下来是kibana,更简单,解压,打开就行。不过这里可以修改语言,同样进入config目录,打开kibana.yml文件修改如下:

启动kibana然后浏览器输入网址:http://localhost:5601/

 

使用

索引

创建索引

#PUT 索引

PUT test4

控制台输出

然后查看elasticsearch head ,可以看到test4索引被创建出来

查询索引

#GET 索引

GET test4

 控制台输出

{
  "test4" : {
    "aliases" : { },
    "mappings" : { },
    "settings" : {
      "index" : {
        "creation_date" : "1624021109162",
        "number_of_shards" : "1",
        "number_of_replicas" : "1",
        "uuid" : "q-rScPG4TjClHjTSaWz-kA",
        "version" : {
          "created" : "7080099"
        },
        "provided_name" : "test4"
      }
    }
  }
}

删除索引

#DELETE 索引

DELETE test4

控制台输出

 

查看 elasticsearch head,已经没有test4索引

文档

创建规则

规则可以理解为数据库里面的第一行,及规定每一列应该存放什么数据。

#创建规则
PUT lzx1
{
  "mappings": {
    "properties": {
      "name":{
        "type": "text"
      },
      "age":{
        "type": "long"
      },
      "brithday":{
        "type": "date"
      }
    }
  }
}

控制台输出

 

elasticsearch head查看lzx1

查看规则&删除规则

#查看规则信息
GET lzx1 

控制台输出

 

#删除规则
DELETE lzx1 

控制台输出

 

查看 elasticsearch head,已经没有lzx1

新增文档

#新增文档

PUT 索引/type/id标识

#新增文档
PUT test/user/1
{
  "name": "lzx",
  "age": 21,
  "dec": "镍币",
  "tags": ["指南","十分","看仔细看"]
}
PUT test/user/2
{
  "name": "张三",
  "age": 21,
  "dec": "法外狂徒",
  "tags": ["指南","十分","宿舍"]
}
PUT test/user/3
{
  "name": "李四",
  "age": 21,
  "dec": "存储",
  "tags": ["啊","文档","宿舍"]
}
PUT test/user/4
{
  "name": "张三123",
  "age": 26,
  "dec": "存储",
  "tags": ["啊","文档","宿舍"]
}

控制台输出

# PUT test/user/1
#! Deprecation: [types removal] Specifying types in document index requests is deprecated, use the typeless endpoints instead (/{index}/_doc/{id}, /{index}/_doc, or /{index}/_create/{id}).
{
  "_index" : "test",
  "_type" : "user",
  "_id" : "1",
  "_version" : 2,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "Failed" : 0
  },
  "_seq_no" : 8,
  "_primary_term" : 1
}

# PUT test/user/2
#! Deprecation: [types removal] Specifying types in document index requests is deprecated, use the typeless endpoints instead (/{index}/_doc/{id}, /{index}/_doc, or /{index}/_create/{id}).
{
  "_index" : "test",
  "_type" : "user",
  "_id" : "2",
  "_version" : 2,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "Failed" : 0
  },
  "_seq_no" : 9,
  "_primary_term" : 1
}

# PUT test/user/3
#! Deprecation: [types removal] Specifying types in document index requests is deprecated, use the typeless endpoints instead (/{index}/_doc/{id}, /{index}/_doc, or /{index}/_create/{id}).
{
  "_index" : "test",
  "_type" : "user",
  "_id" : "3",
  "_version" : 4,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "Failed" : 0
  },
  "_seq_no" : 10,
  "_primary_term" : 1
}

# PUT test/user/4
#! Deprecation: [types removal] Specifying types in document index requests is deprecated, use the typeless endpoints instead (/{index}/_doc/{id}, /{index}/_doc, or /{index}/_create/{id}).
{
  "_index" : "test",
  "_type" : "user",
  "_id" : "4",
  "_version" : 4,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "Failed" : 0
  },
  "_seq_no" : 11,
  "_primary_term" : 1
}

 查看elasticsearch head

 

修改文档

#post更新
POST test/user/3/_update
{
  "doc":{
    "age": 22
  }
}

控制台输出

 

 查看elasticsearch head

查询文档

#查询
GET test/user/_search?q=name:lzx

控制台输出

#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "Failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.7427702,
    "hits" : [
      {
        "_index" : "test",
        "_type" : "user",
        "_id" : "1",
        "_score" : 1.7427702,
        "_source" : {
          "name" : "lzx",
          "age" : 21,
          "dec" : "镍币",
          "tags" : [
            "指南",
            "十分",
            "看仔细看"
          ]
        }
      }
    ]
  }
}

 #查询 并且只显示自己想要显示内容"_source"
GET test/user/_search
{
  "query":{ 
    "match": {
      "name": "张三"
    }
  },
  "_source":["name","dec"]
}

控制台输出

#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "Failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.7509375,
    "hits" : [
      {
        "_index" : "test",
        "_type" : "user",
        "_id" : "2",
        "_score" : 1.7509375,
        "_source" : {
          "dec" : "法外狂徒",
          "name" : "张三"
        }
      },
      {
        "_index" : "test",
        "_type" : "user",
        "_id" : "4",
        "_score" : 1.4536083,
        "_source" : {
          "dec" : "存储",
          "name" : "张三123"
        }
      }
    ]
  }
}

删除文档

#删除文档
DELETE test/user/1

控制台输出

 

 查看elasticsearch head

 

复杂查询

查询所有文档match

#匹配多个条件,多个条件使用空格隔开,只要满足一个条件即可查出,按权重排名
GET test/user/_search
{
  "query":{
    "match": {
      "tags": "宿舍 啊"
    }
  }
}

控制台输出

#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "Failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.5094115,
    "hits" : [
      {
        "_index" : "test",
        "_type" : "user",
        "_id" : "3",
        "_score" : 1.5094115,
        "_source" : {
          "name" : "李四",
          "age" : 22,
          "dec" : "存储",
          "tags" : [
            "啊",
            "文档",
            "宿舍"
          ]
        }
      },
      {
        "_index" : "test",
        "_type" : "user",
        "_id" : "4",
        "_score" : 1.5094115,
        "_source" : {
          "name" : "张三123",
          "age" : 26,
          "dec" : "存储",
          "tags" : [
            "啊",
            "文档",
            "宿舍"
          ]
        }
      },
      {
        "_index" : "test",
        "_type" : "user",
        "_id" : "2",
        "_score" : 0.7133499,
        "_source" : {
          "name" : "张三",
          "age" : 21,
          "dec" : "法外狂徒",
          "tags" : [
            "指南",
            "十分",
            "宿舍"
          ]
        }
      }
    ]
  }
}

查询所有文档term

#倒排索引 这里无法查找出来是因为keyword
GET test/user/_search
{
  "query":{
    "term": {
      "tags": {
        "value": "宿舍"
      }
    }
  }
}

 控制台输出

因此重新创建一个文档

#text 会被分词解析器解析 keyword不会
PUT test3
{
  "mappings": {
    "properties": {
      "name": {
        "type": "text"
      },
      "dec":{
        "type": "keyword"
      }
    }
  }
}
PUT test3/_doc/1
{
  "name":"张三啊",
  "dec": "扎心了扎心了扎心了"
}
PUT test3/_doc/2
{
  "name": "李四啊",
  "dec": "扎心了"
}

 

#搜索”啊“,因为是text,所以可以被分词解析器解析
GET test3/_search
{
  "query": {
    "term": {
      "name": {
        "value": "啊"
      }
    }
  }
}

 控制台输出

{
  "took" : 402,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "Failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.10536051,
    "hits" : [
      {
        "_index" : "test3",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.10536051,
        "_source" : {
          "name" : "张三啊",
          "dec" : "扎心了扎心了扎心了"
        }
      },
      {
        "_index" : "test3",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.10536051,
        "_source" : {
          "name" : "李四啊",
          "dec" : "扎心了"
        }
      }
    ]
  }
}
#因为是keyword,所以只能搜到一个完全对应的
GET test3/_search
{
  "query": {
    "term": {
      "dec": {
        "value": "扎心了"
      }
    }
  }
}

 控制台输出

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "Failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.2039728,
    "hits" : [
      {
        "_index" : "test3",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.2039728,
        "_source" : {
          "name" : "李四啊",
          "dec" : "扎心了"
        }
      }
    ]
  }
}

 

分页查询

#分页 from从第几个数据开始 size返回几个数据(当前页面显示的数据数量)
GET test/user/_search
{
  "query":{ 
    "match": {
      "name": "张三"
    }
  },
  "sort":[
    {
      "age":{
        "order":"desc"
      }
    }
    ],
    "from":0,
    "size":2
}

控制台输出

#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "Failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "test",
        "_type" : "user",
        "_id" : "4",
        "_score" : null,
        "_source" : {
          "name" : "张三123",
          "age" : 26,
          "dec" : "存储",
          "tags" : [
            "啊",
            "文档",
            "宿舍"
          ]
        },
        "sort" : [
          26
        ]
      },
      {
        "_index" : "test",
        "_type" : "user",
        "_id" : "2",
        "_score" : null,
        "_source" : {
          "name" : "张三",
          "age" : 21,
          "dec" : "法外狂徒",
          "tags" : [
            "指南",
            "十分",
            "宿舍"
          ]
        },
        "sort" : [
          21
        ]
      }
    ]
  }
}

 

数据排序

#排序 desc降序 asc升序
GET test/user/_search
{
  "query":{ 
    "match": {
      "name": "张三"
    }
  },
  "sort":[
    {
      "age":{
        "order":"desc"
      }
    }
    ]
}

控制台输出

#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "Failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "test",
        "_type" : "user",
        "_id" : "4",
        "_score" : null,
        "_source" : {
          "name" : "张三123",
          "age" : 26,
          "dec" : "存储",
          "tags" : [
            "啊",
            "文档",
            "宿舍"
          ]
        },
        "sort" : [
          26
        ]
      },
      {
        "_index" : "test",
        "_type" : "user",
        "_id" : "2",
        "_score" : null,
        "_source" : {
          "name" : "张三",
          "age" : 21,
          "dec" : "法外狂徒",
          "tags" : [
            "指南",
            "十分",
            "宿舍"
          ]
        },
        "sort" : [
          21
        ]
      }
    ]
  }
}

 

过滤字段

#过滤器  filter进行数据过滤  gte大于等于多少 lte小于等于多少 gt大于 lt小于
GET test/user/_search
{
  "query":{
    "bool": {
      "must": [
        {
          "match": {
            "name": "张三"
          }
        }
      ],
      "filter": [
        {
          "range": {
            "age": {
              "gte": 25
            }
          }
        }
      ]
    }
  }
}

控制台输出

#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "Failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.1508858,
    "hits" : [
      {
        "_index" : "test",
        "_type" : "user",
        "_id" : "4",
        "_score" : 1.1508858,
        "_source" : {
          "name" : "张三123",
          "age" : 26,
          "dec" : "存储",
          "tags" : [
            "啊",
            "文档",
            "宿舍"
          ]
        }
      }
    ]
  }
}

 

Bool查询

#精准查询 bool 多条件查询 must所有条件都要符合
GET test/user/_search
{
  "query":{
    "bool": {
      "must": [
        {
          "match": {
            "name": "张三"
          }
        },
        {
          "match": {
            "age": "26"
          }
        }
      ]
    }
  }
}

控制台输出

#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "Failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 2.1508858,
    "hits" : [
      {
        "_index" : "test",
        "_type" : "user",
        "_id" : "4",
        "_score" : 2.1508858,
        "_source" : {
          "name" : "张三123",
          "age" : 26,
          "dec" : "存储",
          "tags" : [
            "啊",
            "文档",
            "宿舍"
          ]
        }
      }
    ]
  }
}

迷糊查询

#模糊查询 两个条件满足其一就行 should
GET test/user/_search
{
  "query":{
    "bool": {
      "should": [
        {
          "match": {
            "name": "张三"
          }
        },
        {
          "match": {
            "age": "26"
          }
        }
      ]
    }
  }
}

控制台输出

#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "Failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 2.1508858,
    "hits" : [
      {
        "_index" : "test",
        "_type" : "user",
        "_id" : "4",
        "_score" : 2.1508858,
        "_source" : {
          "name" : "张三123",
          "age" : 26,
          "dec" : "存储",
          "tags" : [
            "啊",
            "文档",
            "宿舍"
          ]
        }
      },
      {
        "_index" : "test",
        "_type" : "user",
        "_id" : "2",
        "_score" : 1.3862942,
        "_source" : {
          "name" : "张三",
          "age" : 21,
          "dec" : "法外狂徒",
          "tags" : [
            "指南",
            "十分",
            "宿舍"
          ]
        }
      }
    ]
  }
}
#查询不满足信息的 must_not 查询出年龄不是22的所有数据
GET test/user/_search
{
  "query":{
    "bool": {
      "must_not": [
        {
          "match": {
            "age": "22"
          }
        }
      ]
    }
  }
}

 控制台输出

#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "Failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 0.0,
    "hits" : [
      {
        "_index" : "test",
        "_type" : "user",
        "_id" : "2",
        "_score" : 0.0,
        "_source" : {
          "name" : "张三",
          "age" : 21,
          "dec" : "法外狂徒",
          "tags" : [
            "指南",
            "十分",
            "宿舍"
          ]
        }
      },
      {
        "_index" : "test",
        "_type" : "user",
        "_id" : "4",
        "_score" : 0.0,
        "_source" : {
          "name" : "张三123",
          "age" : 26,
          "dec" : "存储",
          "tags" : [
            "啊",
            "文档",
            "宿舍"
          ]
        }
      },
      {
        "_index" : "test",
        "_type" : "user",
        "_id" : "1",
        "_score" : 0.0,
        "_source" : {
          "name" : "lzx",
          "age" : 21,
          "dec" : "镍币",
          "tags" : [
            "指南",
            "十分",
            "看仔细看"
          ]
        }
      }
    ]
  }
}

 

高亮查询

#高亮查询 highlight
GET test3/_search
{
  "query": {
    "match": {
      "name": "张三"
    }
  },
  "highlight": {
    "fields": {
      "name": {}
    }
  }
}
{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "Failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.3862942,
    "hits" : [
      {
        "_index" : "test3",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.3862942,
        "_source" : {
          "name" : "张三啊",
          "dec" : "扎心了扎心了扎心了"
        },
        "highlight" : {
          "name" : [
            "<em>张</em><em>三</em>啊"
          ]
        }
      }
    ]
  }
}

自定义高亮查询

#自定义高亮条件
GET test3/_search
{
  "query": {
    "match": {
      "name": "罗志希"
    }
  },
  "highlight": {
    "pre_tags": "<p class='key' style='color:red'>",
    "post_tags": "</p>",
    "fields": {
      "name": {}
    }
  }
}
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "Failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐