Elasticsearch7.x 常用操作

258 阅读1分钟
ik分词器插件,直接下载releases版本到plugin后解压,重启es生效
https://github.com/medcl/elasticsearch-analysis-ik/releases

#index
$ curl -XPUT -H 'content-Type:application/json' 'http://localhost:9200/spu_v2' -d '
{
    "settings": {
        "number_of_shards": "3",
        "number_of_replicas": "1",
        "analysis": {
            "filter": {
                "share_synonym": {
                    "type": "synonym",
                    "synonyms_path": "synonym/share_synonym.txt"
                },
                "share_stopwords": {
                    "type": "stop",
                    "stopwords_path": "stopwords/share_stopwords.txt"
                }
            },
            "analyzer": {
                "ik_analyzer": {
                    "filter": [
                        "share_stopwords",
                        "share_synonym"
                    ],
                    "char_filter": "html_strip",
                    "type": "custom",
                    "tokenizer": "ik_max_word"
                }
            },
            "keyword_analyzer": {
                "filter": [
                    "asciifolding",
                    "share_stopwords",
                    "share_synonym"
                ],
                "char_filter": "html_strip",
                "type": "custom",
                "tokenizer": "keyword"
            }
        }
    }
}'

#查看settings
$ curl -XGET 'localhost:9200/spu_v2/_settings?pretty'

#修改settings
$ curl -XPOST 'localhost:9200/spu_v2/_close'
$ curl -XPUT -H 'content-Type:application/json' 'http://localhost:9200/spu_v2/_settings' -d '
{
    "settings": {
        "analysis": {
            "filter": {
                "share_synonym": {
                    "type": "synonym",
                    "synonyms_path": "synonym/share_synonym.txt"
                },
                "share_stopwords": {
                    "type": "stop",
                    "stopwords_path": "stopwords/share_stopwords.txt"
                }
            },
            "analyzer": {
                "ik_analyzer": {
                    "filter": [
                    	 "asciifolding",
                        "share_stopwords",
                        "share_synonym"
                    ],
                    "char_filter": "html_strip",
                    "type": "custom",
                    "tokenizer": "ik_max_word"
                }
            },
            "keyword_analyzer": {
                "filter": [
                    "asciifolding",
                    "share_stopwords",
                    "share_synonym"
                ],
                "char_filter": "html_strip",
                "type": "custom",
                "tokenizer": "keyword"
            }
        }
    }
}'
$ curl -XPOST 'localhost:9200/spu_v2/_open'

#mapping
$ curl -XPOST -H 'content-Type:application/json' 'http://localhost:9200/spu_v2/_mapping' -d '
{
    "dynamic": "strict",
    "properties": {
        "id": {
            "type": "integer"
        },
        "goods_name": {
        	  "type": "text",
            "analyzer": "ik_analyzer",
            "search_analyzer": "ik_analyzer"
        },
        "cat_id": {
            "type": "short"
        },
        "cat_name": {
            "type": "text",
            "analyzer": "ik_analyzer",
            "search_analyzer": "ik_analyzer"
        },
        "brand_id": {
            "type": "integer"
        },
        "brand_name": {
            "type": "text",
            "analyzer": "ik_analyzer",
            "search_analyzer": "ik_analyzer"
        },
        "created_at": {
            "type": "integer"
        },
        "updated_at": {
            "type": "integer"
        }
    }
}'

#_reindex
curl -XPOST -H 'content-Type:application/json' 'http://localhost:9200/_reindex?pretty' -d '
{
    "source": {
        "index": "spu_v1"
    },
    "dest": {
        "index": "spu_v2"
    }
}'

#_reindex(指定_source)
curl -XPOST -H 'content-Type:application/json' 'http://localhost:9200/_reindex?pretty' -d '
{
    "source": {
        "index": "spu_v1",
        "_source":["brand_id","brand_name","cat_id","cat_name","created_at","updated_at"]
    },
    "dest": {
        "index": "spu_v2"
    }
}'

#_aliases
$ curl -XPOST -H 'content-Type:application/json' 'http://localhost:9200/_aliases' -d '
{
    "actions": [
        {
            "remove": {
                "index": "spu_v1",
                "alias": "spu"
            }
        },
        {
            "add": {
                "index": "spu_v2",
                "alias": "spu"
            }
        }
    ]
}'




#删除index
$ curl -XDELETE 'http://localhost:9200/spu_v1'


#分词测试
$ curl -XGET -H 'content-Type:application/json' 'http://localhost:9200/spu_v2/_analyze' -d '
{
  "analyzer": "ik_analyzer",
  "text": "六味地黄丸软胶囊"
}'

#查看数据总条数
$ curl -XGET 'http://10.180.18.30:9200/spu_v2/_doc/_count'

#简单查询
$ curl -XGET 'http://10.180.18.30:9200/spu_v2/_search'
$ curl -XGET 'http://10.180.18.30:9200/spu_v2/_doc/659'
$ curl -XGET -H 'content-Type:application/json' 'http://localhost:9200/spu_v2/_doc/_search?pretty' -d '
{
  "query": {
        "match": {
            "goods_name": "兰蔻"
        }
    }
}'

#删除文档
$ curl -XDELETE -H 'content-Type:application/json' 'http://localhost:9200/spu_v2/_doc/20'

#新增字段
$ curl -XPUT -H 'content-Type:application/json' 'http://localhost:9200/spu_v2/_mapping' -d '
{
  "properties": {
    "support_sell": {
      "type": "byte"
    },
    "weights": {
      "type": "integer"
    }
  }
}'


# 通过script批量更新字段的值
> 全部更新
$ curl -XPOST -H 'content-Type:application/json' 'http://localhost:9200/spu_v2/_doc/_update_by_query' -d '
{
    "script": {
        "source": "ctx._source.support_sell = 0"
    }
}'

> 使用带搜索条件的批量操作
$ curl -XPOST -H 'content-Type:application/json' 'http://localhost:9200/spu_v2/_doc/_update_by_query' -d '
{
    "query": {
    "bool": {
            "must": {
                "terms": {"id": [26, 43]}
            }
        }
    },
    "script": {
        "source": "ctx._source.support_sell = 0"
    }
}'

#更新单个文档的值
$ curl -XPOST -H 'content-Type:application/json' 'http://localhost:9200/spu_v2/_doc/43/_update' -d '
{
    "doc": {
        "support_sell": "1",
        "weights": "1"
    }
}'

> 使用脚本更新单个文档的值
$ curl -XPOST -H 'content-Type:application/json' 'http://localhost:9200/spu_v2/_doc/43/_update' -d '
{
    "script": {
        "source": "ctx._source.support_sell = 0;ctx._source.weights = 0"
    }
}'

> 脚本中传递参数
curl -XPOST -H 'content-Type:application/json' 'http://localhost:9200/spu_v2/_doc/43/_update' -d '
{
    "script": {
        "source": "ctx._source.support_sell = params.support_sell",
        "params": {
            "support_sell": 1
        }
    }
}'