zoukankan      html  css  js  c++  java
  • ElasticSearch初试(一)

    测试环境:debian 9
    官网提供了 deb,rpm,源码下载

    官方下载地址:https://www.elastic.co/downloads/elasticsearch

    通过源码安装会遇到一些小问题,为了方便,我直接下载deb安装(需要提前安装jdk)。

    可以通过 service elasticsearch start/stop 启动关闭服务,默认监听了 9200端口,可以更改配置文件

    通过deb安装的配置文件在:/etc/elasticsearch/elasticsearch.yml

    如果要在localhost外连接elasticsearch ,更改配置文件中的 network.host:0.0.0.0

    如果一起顺利就可以开始测试了

    查看es基本信息
    curl localhost:9200
    
    列出所有的Index
    curl -X GET 'http://localhost:9200/_cat/indices?v'
    
    列举每个Index下的Type
    curl 'localhost:9200/_mapping?pretty=true'
    
    添加Index
    curl -X PUT 'localhost:9200/weather'
    
    删除Index
    curl -X DELETE 'localhost:9200/weather'
    
    安装中文分词插件ik (安装完需要重启es)
    elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.5.4/elasticsearch-analysis-ik-6.5.4.zip
    
    创建一个Index,并设置其结构和分词
    curl -X PUT -H 'Content-Type: application/json' 'localhost:9200/accounts' -d '
    {
      "mappings": {
        "person": {
          "properties": {
            "user": {
              "type": "text",
              "analyzer": "ik_max_word",
              "search_analyzer": "ik_max_word"
            },
            "title": {
              "type": "text",
              "analyzer": "ik_max_word",
              "search_analyzer": "ik_max_word"
            }
          }
        }
      }
    }'
    
    向Index增加记录
    PUT方式
    curl -X PUT -H 'Content-Type: application/json' 'localhost:9200/accounts/person/1' -d '
    {
      "user": "张三",
      "title": "工程师"
    }' 
    
    POST方式(POST方式不需要传id,id随机生成)
    curl -X POST -H 'Content-Type: application/json' 'localhost:9200/accounts/person' -d '
    {
      "user": "李四",
      "title": "工程师"
    }'
    注意:如果没有先创建 Index(这个例子是accounts),直接执行上面的命令,Elastic 也不会报错,而是直接生成指定的 Index。所以,打字的时候要小心,不要写错 Index 的名称。
    
    查看指定条目的记录
    curl 'localhost:9200/accounts/person/1?pretty=true'
    
    删除一条记录
    curl -X DELETE 'localhost:9200/accounts/person/1'
    
    更新一条记录
    curl -X PUT -H 'Content-Type: application/json' 'localhost:9200/accounts/person/1' -d '
    {
        "user" : "张三",
        "title" : "软件开发"
    }' 
    
    查询所有记录
    curl 'localhost:9200/accounts/person/_search?pretty=true'
    
    简单查询
    curl -H 'Content-Type: application/json' 'localhost:9200/accounts/person/_search?pretty=true'  -d '
    {
      "query" : { "match" : { "title" : "工程" }},
      "from": 1, #0开始
      "size": 1, #返回几条数据
    }'
    
    OR查询
    curl -H 'Content-Type: application/json' 'localhost:9200/accounts/person/_search?pretty=true'  -d '
    {
      "query" : { "match" : { "title" : "工程 哈哈" }}
    }'
    
    AND查询
    curl -H 'Content-Type: application/json' 'localhost:9200/accounts/person/_search?pretty=true'  -d '
    {
      "query": {
        "bool": {
          "must": [
            { "match": { "title": "工程" } },
            { "match": { "title": "哈哈" } }
          ]
        }
      }
    }'
    

      原文链接

  • 相关阅读:
    Java快速教程
    让我们来了解一下:操作系统和平台相关性
    初窥Linux 之 我最常用的20条命令
    ES6学习笔记一
    Data时间管理大全
    generator多返回值写法
    箭头函数=>
    闭包
    高阶函数:map/reduce
    函数方法that与apply
  • 原文地址:https://www.cnblogs.com/hanybblog/p/13454262.html
Copyright © 2011-2022 走看看