欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ElasticSearch入门学习

程序员文章站 2022-05-31 23:31:53
...
公司ES链接地址:http://10.202.77.206:5601/app/kibana#/dev_tools/console?_g=()

-- 创建一个index
put /customer_zhanglu?pretty
/*
成功会提示:{
  "acknowledged": true,
  "shards_acknowledged": true
}
*/

--查看所有索引
GET /_cat/indices?v
/*
health     status     index            uuid                    pri      rep      docs.count     docs.deleted     store.size     pri.store.size
green      open      icdstags_v1      EU7zRqqjSUWK3Sd8j7CfaA   5        1         21              0              250.5kb        125.2kb
green      open      bdp_css_waybill  m51I5keSQw2L_UlpxzISwA   5        1          0              0              1.2kb          650b
green      open      customer_zhanglu Qa-T7S97SymryXFHqAV35A   5        1          0              0              1.2kb          650b
*/

-- 插入一条记录,记录id手动指定
PUT /customer_zhanglu/table1/1?pretty
{
  "name": "John Doe"
}
/*
{
  "_index": "customer_zhanglu",
  "_type": "table1",
  "_id": "1",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 2,
    "failed": 0
  },
  "created": true
}
*/
-- 插入一条记录,记录id自动生成
POST /customer_zhanglu/table1?pretty
{
  "name": "table1_autoidtest",
  "age": 20,
  "servingStaffFlag": "no",
  "provinctCityName": "深圳"
}
/*
{
  "_index": "customer_zhanglu",
  "_type": "table1",
  "_id": "AWoGwpGBJUnalh9s_9hq",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 2,
    "failed": 0
  },
  "created": true
}
*/

-- _source定制返回的结果,指定_source中,返回哪些field
GET customer_zhanglu/table1/1?pretty
/* 返回结果:
{
  "_index": "customer_zhanglu",
  "_type": "table1",
  "_id": "1",
  "_version": 1,
  "found": true,
  "_source": {
    "name": "table1_1",
    "age": 20,
    "servingStaffFlag": "yes",
    "provinctCityName": "深圳"
  }
}
*/

GET customer_zhanglu/table1/1?_source=name,age  -- 返回指定的字段
/*
{
  "_index": "customer_zhanglu",
  "_type": "table1",
  "_id": "1",
  "_version": 1,
  "found": true,
  "_source": {
    "name": "table1_1",
    "age": 20
  }
}
*/
GET customer_zhanglu/table1/1?_source_include=name,age   -- 未找到与_source的区别

GET customer_zhanglu/table1/1?_source_exclude=name,age  -- 返回不包含指定字段的其余所有字段
/*
{
  "_index": "customer_zhanglu",
  "_type": "table1",
  "_id": "1",
  "_version": 1,
  "found": true,
  "_source": {
    "servingStaffFlag": "yes",
    "provinctCityName": "深圳"
  }
}
*/

-- 查看结构
GET /customer_zhanglu
/*
{
  "customer_zhanglu": {
    "aliases": {},
    "mappings": {
      "table1": {
        "dynamic_templates": [
          {
            "strings_not_analyzed": {
              "match_mapping_type": "string",
              "mapping": {
                "type": "keyword"
              }
            }
          }
        ],
        "properties": {
          "name": {
            "type": "keyword"
          }
        }
      },
      "_default_": {
        "dynamic_templates": [
          {
            "strings_not_analyzed": {
              "match_mapping_type": "string",
              "mapping": {
                "type": "keyword"
              }
            }
          }
        ]
      }
    },
    "settings": {
      "index": {
        "creation_date": "1551175058672",
        "number_of_shards": "5",
        "number_of_replicas": "1",
        "uuid": "Qa-T7S97SymryXFHqAV35A",
        "version": {
          "created": "5040199"
        },
        "provided_name": "customer_zhanglu"
      }
    }
  }
}
*/

-- 查看index为icdstags,type为tags的所有数据
GET /icdstags/tags/_search?pretty
/*
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 23,
    "max_score": 1,
    "hits": [
      {
        "_index": "icdstags_v1",
        "_type": "tags",
        "_id": "DEEQAVTg%2B%2BKxH5hiTZSLQqPeeQck8%3D",
        "_score": 1,
        "_source": {
          "phone": "DEEQAVTg%2B%2BKxH5hiTZSLQqPeeQck8%3D",
          "validFlag": "1",
          "baseMonthTag": "BM1-,BM1-SE0003-0-10,BM3-偏收有寄,BM4-1-4,BM5-1-4,BM6-1-4,BM7-5-10,BM8-5-10,BM9-11-20,BM10-0",
          "marketTag": "MK2-否,MK3-否,MK4-否,MK5-否,MK6-否,MK7-否,MK8-否,MK9-否,MK10-否,MK11-否,MK12-否,MK13-否,MK14-否,MK15-否,MK16-非用券用户",
          "uniqueid": "DEEQAVTg%2B%2BKxH5hiTZSLQqPeeQck8%3D",
          "baseDayTag": "BD1-是,BD2-有,BD5-否,BD6-201411,BD7-金牌会员,BD8-595Y,BD9-QuanZhou,BD10-595CC,BD11-595Y,BD12-QuanZhou,BD13-595CC,BD14-否,BD15-否,BD17-是,BD18-100,BD19-4,BD20-工业区"
        }
      },
      {
        "_index": "icdstags_v1",
        "_type": "tags",
        "_id": "DEEQAVTg%2B%2FYt8x%2FkcySVCcYyt3wBQ%3D",
        "_score": 1,
        "_source": {
          "phone": "DEEQAVTg%2B%2FYt8x%2FkcySVCcYyt3wBQ%3D",
          "validFlag": "1",
          "baseMonthTag": "BM1-,BM1-SE0003-0-10,BM3-偏收有寄,BM4-1-4,BM5-1-4,BM6-1-4,BM7-5-10,BM8-5-10,BM9-11-20,BM10-0",
          "marketTag": "MK2-否,MK3-否,MK4-否,MK5-否,MK6-否,MK7-否,MK8-否,MK9-否,MK10-否,MK11-否,MK12-否,MK13-否,MK14-否,MK15-否,MK16-非用券用户",
          "uniqueid": "DEEQAVTg%2B%2FYt8x%2FkcySVCcYyt3wBQ%3D",
          "baseDayTag": "BD1-是,BD2-有,BD5-否,BD6-201411,BD7-金牌会员,BD8-595Y,BD9-QuanZhou,BD10-595CC,BD11-595Y,BD12-QuanZhou,BD13-595CC,BD14-否,BD15-否,BD17-否,BD18-50,BD19-1,BD20-商业区"
        }
      }
    ]
  }
}
*/

--查看index为icdstags,type为tags下的记录总数
GET /icdstags/tags/_count?pretty
/*
{
  "count": 23,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  }
}
*/

-- 查看index下的所有数据,按照name升序排列,排列字段必须,index下如果有多个type,sort的字段必须是每个type中都存在的
GET /customer_zhanglu/_search?q=*&sort=name:asc&pretty

GET /customer_zhanglu/_search
{
  "query": { "match_all": {} },
  "sort": [
    {"name": "asc"}
    ]
}

/*
{
  "took": 1,       -- 搜索花费的时间,但是是毫秒
  "timed_out": false,  -- 是否超时
  "_shards": {      -- 分片
    "total": 5,     -- 搜索的分片个数
    "successful": 5,  -- 搜索成功的分片个数
    "failed": 0       -- 搜索失败的分片个数
  },
  "hits": {        -- 搜索的结果
    "total": 3,    -- 符合条件的记录个数,es中的记录叫document
    "max_score": null,
    "hits": [     -- 搜索到的数据,默认显示前10条
      {
        "_index": "customer_zhanglu",
        "_type": "table1",
        "_id": "1",
        "_score": null,
        "_source": {
          "name": "hello1"
        },
        "sort": [    -- 排序的字段值,如果使用score排序,则不显示sort
          "hello1"
        ]
      },
      {
        "_index": "customer_zhanglu",
        "_type": "table2",
        "_id": "2",
        "_score": null,
        "_source": {
          "doc": {
            "name": "Jane",
            "age": 22
          }
        },
        "sort": [
          null
        ]
      },
      {
        "_index": "customer_zhanglu",
        "_type": "table2",
        "_id": "1",
        "_score": null,
        "_source": {
          "doc": {
            "name": "Jane Doe",
            "age": 20
          }
        },
        "sort": [
          null
        ]
      }
    ]
  }
}
*/

-- 增加字段
POST customer_zhanglu/table1/_mapping
{
  "properties": {
    "servingStaffFlag":{"type": "keyword"},
    "provinctCityName":{"type": "keyword"}
  }
}
-- 上述命令转换为curl
curl -XPOST "http://10.202.116.33:9200/customer_zhanglu/table1/_mapping" -H 'Content-Type: application/json' -d'
{
  "properties": {
    "servingStaffFlag":{"type": "keyword"},
    "provinctCityName":{"type": "keyword"}
  }
}'


-- 按照某字段的特定值查询
GET /customer_zhanglu/table1/_search?pretty
{
  "query":{
    "term" : {"age":20}
  }
}
/*
curl -XGET "http://10.202.116.33:9200/customer_zhanglu/table1/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query":{
    "term" : {"age":20}
  }
}'
*/

GET /customer_zhanglu/table1/_search?pretty
{
  "query":{
    "term" : {"servingStaffFlag":"yes"}
  }
}
相关标签: es