欢迎光临
感受代码之美

Elasticsearch常用操作

elasticsearch.jpg

这里以索引名为data_index为例,elasticsearch的地址为http://10.47.0.96:9200,版本为Elasticsearch 7.1.1

索引

1.创建索引

1)创建名为data_index的索引

curl -XPUT 'http://10.47.0.96:9200/data_index'

2)创建索引同时对索引进行设置、创建映射

curl -H  'Content-Type: application/json' -XPUT 'http://10.47.0.96:9200/data_index' -d '
{
  "settings": {
    "analysis": {
      "analyzer": {
        "dataArchives_analyzer": {
          "tokenizer": "dataArchives_tokenizer"
        }
      },
      "tokenizer": {
        "dataArchives_tokenizer": {
          "type": "ngram",
          "min_gram": 3,
          "max_gram": 3,
          "token_chars": [
            "letter",
            "digit"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "id_number": {
        "type": "text",
        "analyzer": "dataArchives_analyzer",
        "search_analyzer": "dataArchives_analyzer"
      },
      "name": {
        "type": "text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_max_word"
      },
      "birthday": {
        "type": "text"
      }
    }
  }
}'

2.删除索引

1)删除

curl -XDELETE 'http://10.47.0.96:9200/data_index'

2) 删除多个指定索引

curl -XDELETE 'http://10.47.0.96:9200/data_index,data_index1,data_index2'

3)删除所有索引

curl -XDELETE  'http://10.47.0.96:9200/_all'

3.打开索引

curl -XPOST 'http://10.47.0.96:9200/data_index/_open'

4.关闭索引

curl -XPOST 'http://10.47.0.96:9200/data_index/_close' 

映射

1.创建映射

curl -H  'Content-Type: application/json' -XPUT 'http://10.47.0.96:9200/data_index/_mapping' -d'
{
    "properties": {
        "attachment.content": {
            "type": "text",
            "analyzer": "ik_max_word",
            "search_analyzer": "ik_smart"
        },
        "fileName": {
            "type": "text",
            "analyzer": "ik_max_word",
            "search_analyzer": "ik_smart"
        }
    }
}'

2.查看映射

curl -H  'Content-Type: application/json' -XGET 'http://10.47.0.96:9200/data_index/_mapping?pretty'

3.删除映射

映射创建后,正常是无法删除的,详见:Elasticsearch Reference [7.1] » Deleted pages » Delete Mapping

设置

如果要对索引进行设置,可以参考上面索引->创建索引-创建索引同时对索引进行设置、创建映射部分,在创建索引时进行设置,否则需要先close索引,再进行配置:

curl -H  'Content-Type: application/json' -XPUT 'http://10.47.0.96:9200/data_archives_jw_reporter_info/_settings' -d'
{
  "analysis": {
    "analyzer": {
      "dataArchives_analyzer": {
        "tokenizer": "dataArchives_tokenizer"
      }
    },
    "tokenizer": {
      "dataArchives_tokenizer": {
        "type": "ngram",
        "min_gram": 3,
        "max_gram": 3,
        "token_chars": [
          "letter",
          "digit"
        ]
      }
    }
  }
}'

上面是设置ngram分词。

缓存

1) 清空全部缓存

curl localhost:9200/_cache/clear?pretty
{
  "_shards" : {
    "total" : 72,
    "successful" : 72,
    "failed" : 0
  }
}

2) 清除单一索引缓存

curl localhost:9200/index/_cache/clear?pretty
{
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  }
}

3) 清除多索引缓存

curl localhost:9200/index1,index2,index3/_cache/clear?pretty
{
  "_shards" : {
    "total" : 12,
    "successful" : 12,
    "failed" : 0
  }
}
转载请注明来源:四个空格 » Elasticsearch常用操作

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址