本篇文章相对ES学习,直接跳过了ES介绍来到如何用curl来操作包括,整个流程根据创建到使用查询。本篇文章使用的ES版本为6.8。如果你是使用7.x的那么可能有些不适用,在文章中也会有标明。

一、用curl查看ES版本

curl -XGET -u "账号:密码"  -H "Content-Type: application/json" 'ip地址:端口'

执行结果:可查看当前ES版本

 

 

二、用curl创建索引

创建索引前先说明想要使用ES要知道ES的索引由哪几部分组成。索引index包括settings、mapping、具体数据,其中settings用于配置分词器,分片数,备份数等配置,mapping用于配置索引的具体结构相当于mysql数据库的表结构,创建完index需要配置好settings,mapping之后才能往index里插入数据。

创建索引,在创建索引时直接配置好settings语句如下

curl -XPUT -u "账号:密码"  -H "Content-Type: application/json" 'ip:端口/索引名' -d '
{
    "settings":{
        "refresh_interval":"300s",
        "number_of_shards":"5",
        "merge":{
            "scheduler":{
                "max_thread_count":"100"
            },
            "policy":{
                "segments_per_tier":"5",
                "max_merged_segment":"100mb"
            }
        },
        "max_result_window":"50000",
        "analysis":{
            "analyzer":{
                "ngram_tokenizer_analyzer":{
                    "filter":[
                        "lowercase"
                    ],
                    "type":"custom",
                    "tokenizer":"ngram_tokenizer"
                }
            },
            "tokenizer":{
                "ngram_tokenizer":{
                    "type":"nGram",
                    "min_gram":"1",
                    "max_gram":"255"
                }
            }
        },
        "number_of_replicas":"1"
    }
}'

执行成功后提示:{"acknowledged":true}

其中analysis分词器配置的ngram_tokenizer_analyzer为自定义分词器,可以不进行设置使用内置ik分词器。number_of_shards分片数

三、用curl查看索引Settings配置

curl -XGET -u "账号:密码"  -H "Content-Type: application/json" 'ip地址:端口/索引名/_settings'

执行结果:

 

 

四、用curl创建索引的mapping

此处注意'ip:端口/索引名/_mapping/doc' 这里doc是指es里的type,要注意es版本,es7.0之后这里如果不写doc,默认会建一个_doc的type。看你需求是用doc还是用_doc

curl -XPOST -u "账号:密码"  -H "Content-Type: application/json" 'ip:端口/索引名/_mapping/doc' -d '{    
      "dynamic": "true",
      "properties": {
        "available": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "birthday": {
          "type": "long"
        },
        "class_code": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "company_code": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        }
        "unique_code": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        }
      }
}' 

执行成功后提示:{"acknowledged":true}

五、用curl查看索引mapping结构

curl -XGET -u "账号:密码"  -H "Content-Type: application/json" 'ip地址:端口/索引名'

执行结果

六、用curl查看索引数据

_source代表查询的字段,term里写入查询的where字段及查询值

curl -XGET -u "账号:密码"  -H "Content-Type: application/json" 'ip地址:端口/索引名/_search' -d '{
  "query": {
    "bool": {
      "should": [
        {"term": {
          "employee_ldap": {
            "value": "guilhermepacheco1"
          }
        }}
      ]
    }
  },
  "from": 0,
  "size": 20,
  "_source": ["_id","data_source","mdata_create_time","employee_ldap","hr_status","employee_name","employee_ldap"]
}'

执行结果

以上是学习ES的curl操作内容,感谢观看 

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐