es数据库常用语句

查询所有索引

GET _cat/indices

查询索引结构

GET 索引名/_mapping

普通查询

GET 索引名/_search
{
  "from" : 0,
  "size" : 10,
  "post_filter" : {
    "bool" : {
      "must" : [
        {
          "term": {
            "key": "value"
          }
        },
        {
          "match": {
            "key": "value"
          }
        }
      ],
      "adjust_pure_negative" : true,
      "boost" : 1.0
    }
  }
}

使用查询模板查询

GET 索引名-*/_search/template
{
  "id": "模板名",
  "params": {
    "key":"value"
  }
}

新建索引模板

PUT /_index_template/索引模板名
{
    "index_patterns": [
        "business-record-*"
    ],
    "template": {
        "settings": {
            "refresh_interval": "1s",
            "number_of_shards": "1"
        },
        "mappings": {
            "properties": {
                "metadata.sessionId": {
                    "type": "keyword"
                },
                "metadata.fileCreateTime": {
                    "type": "date",
                    "format": "yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || yyyy/MM/dd HH:mm:ss|| yyyy/MM/dd ||epoch_millis"
                },
                "transcript": {
                    "type": "nested",
                    "properties": {
                        "text": {
                            "type": "text"
                        }
                    }
                }
            }
        }
    }
}

新建索引

PUT /索引名
{
    "settings": {
        "refresh_interval": "1s",
        "number_of_shards": "1"
    },
    "mappings": {
        "properties": {
            "metadata.sessionId": {
                "type": "keyword"
            },
            "metadata.fileCreateTime": {
                "type": "date",
                "format": "yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || yyyy/MM/dd HH:mm:ss|| yyyy/MM/dd ||epoch_millis"
            },
            "transcript": {
                "type": "nested",
                "properties": {
                    "text": {
                        "type": "text"
                    }
                }
            }
        }
    }
}

# 备注:基于索引模板建索引只需要执行  PUT /索引名

新建pipeline

PUT /_ingest/pipeline/pipeline名称
{
  "description": "描述",
  "processors": [
    {
      "date_index_name": {
        "field": "fileCreateTime",
        "index_name_prefix": "索引模板前缀",
        "date_rounding": "M" #月
      }
    }
  ]
}

新建查询模板

POST /_scripts/查询模板名
{
  "script": {
    "lang": "mustache",
    "source": "{\"from\":\"{{_offset}}{{^_offset}}0{{/_offset}}\",\"size\":\"{{pageSize}}{{^pageSize}}10{{/pageSize}}\",\"sort\":[{\"metadata.fileCreateTime\":{\"order\":\"desc\"}}],\"track_total_hits\": true,\"query\":{\"bool\":{\"filter\":[{{#sessionId}}{\"match_phrase\":{\"metadata.sessionId\":\"{{sessionId}}\"}},{{/sessionId}}{\"term\":{\"state\":\"1\"}}]}}}"
  }
}

数据迁移

POST _reindex
{
  "source": {
    "index": "原始数据索引"
  },
  "dest": {
    "index": "要迁移到这里"
  }
}

根据条件更新

POST 索引名/_doc/_update_by_query
{
  "script": {
    "inline": "ctx._source.key = params.name",
    "params": {
      "name": "key要更新值"
    },
    "lang": "painless"
  },
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "key":"value"
          }
        },
        {
          "match": {
            "key":"value"
          }
        }
      ]
    }
  }
}

你可能感兴趣的:(es数据库常用语句)