ElasticSearch入门篇(2):DSL开发(增删改查、插入数据)

ElasticSearch入门篇(2):DSL开发(增删改查、插入数据),第1张

ElasticSearch入门篇(2):DSL开发(增删改查、插入数据

  上一篇分享了如何在windows下搭建了一个分布式ES集群,这一篇来入门下DSL开发。

  ES支持JSON格式的查询,叫做DSL(domain specific language)。
  常用数据类型:text、keyword、number、array、range、boolean、date、geo_point、ip、nested、object

类型注释text默认会进行分词,支持模糊查询(5.x之后版本string类型已废弃,请大家使用text)keyword不进行分词;keyword类型默认开启doc_values来加速聚合排序 *** 作,占用了大量磁盘io 如非必须可以禁用doc_values。number如果只有过滤场景 用不到range查询的话,使用keyword性能更佳,另外数字类型的doc_values比字符串更容易压缩。arrayES不需要显示定义数组类型,只需要在插入数据时用’[]'表示即可,元素类型需保持一致。range对数据的范围进行索引;目前支持 number range、date range 、ip rangeobject嵌套类型,不支持数组。boolean只接受true、false 也可以是字符串类型的“true”、“false”date支持毫秒、根据指定的format解析对应的日期格式,内部以long类型存储。

  开发工具
利用本地部署的kibana上自带的Dev Tools。启动Kibana,浏览器进入 http://localhost:5601/app/dev_tools#/console

#############基础查询#################
#查看集群健康情况
GET /_cat/health?v

#查看集群的系统索引及数据
GET _search
{
  "query": {
    "match_all": {}
  }
  , "size": 2 #指定返回结果的条数
}


#查看所有的索引
GET _cat/indices

#查看某个索引的条数
GET kibana_sample_data_ecommerce/_count
#查看某个索引的全部数据
GET kibana_sample_data_ecommerce/_search


#插入(创建)users索引,并创建id为1的文档,如果存在则报错
POST users/_doc/1
{ "firstname": "will", "lastname": "smith"}

#创建id为2的文档
POST users/_create/2
{
  "firstname": "zero",
  "lastname": "xu",
  "age": "25",
  "hobby": "sneakers movies"
}

#创建id为3的文档
PUT users/_create/3
{
  "firstname": "russ",
  "lastname": "westbrook",
  "hobby": "fashion"
}

#创建id为4的文档
POST users/_doc/4
{
  "firstname": "wet",
  "lastname": "keep"
}

#更新id为1的文档,并新增两个属性
POST users/_update/1
{
  "doc": {
    "hobby": "sneakers",
    "age": "25"
  }
}

POST users/_update/1
{
  "doc": {
    "age": "54",
    "type":"keyword"
  }
}

#删除索引下的某个id文档
DELETE users/_doc/4

#删除索引
DELETE users

#查询users索引的某个id文档
GET users/_doc/3
GET users/_doc/2
GET users/_doc/1
#批量查询多个指定的id的数据,也可以批量查询
GET /_mget
{
  "docs": [
    {
      "_index": "users",
      "_id": 1
    },
    {
      "_index": "users",
      "_id": 2
    }
  ]
}

#match_all
#查询所有数据,[按照id进行排序]
GET users/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "_id": {
        "order": "desc"
      }
    }
  ]
}

#泛查询,查询所有的文档,只要属性值包含zero的文档
GET users/_search?q=zero

#查询fisrtname属性包含russ的文档
GET users/_search?q=russ&df=firstname
GET users/_search?q=firstname:russ

#查询lastname中包含xu,从第0条开始,查询1条(es的计数是从0开始)
GET users/_search?q=lastname:xu&from=0&size=1

#查询hobby中包含fashion或者sneakers的文档
GET users/_search?q=hobby:fashion sneakers
GET users/_search?q=hobby:(fashion sneakers)

#查询hobby中包含"sneaker movies"串的文档(有前后顺序之分)
GET users/_search?q=hobby:"sneakers movies"

#查询hobby中既包含"sneaker"又包含"movies"的文档(无前后顺序之分,AND要大写)
GET users/_search?q=hobby:(sneakers AND movies)

#查询hobby中包含sneakers但是不包含movies的文档(NOT要大写)
GET users/_search?q=hobby:(sneakers NOT movies)

#查询年纪大于30岁的数据
GET users/_search?q=age:>=30

# 返回user索引下的所有数据
GET users/_search

######### 高级查询 ##############
# 分词器
GET _analyze
{
  "analyzer": "standard",
  "text": "2 Running quick brown-foxes leap over lazy dog in the summer evening"
}

#request body深入搜索:term是最小搜索单位,包含:term查询的种类有:Term Query、Range Query等。
GET users/_search
{
  "query": {
    "term": {
      "hobby": {
        "value": "sneakers"
      }
    }
  }
}

GET users/_search
{
  "query": {
    "terms": {
      "hobby": [
        "movies",
        "fashion"
      ]
    }
  }
}

#要将age转成keyword类型,否则使用默认的fileddata类型无法进行排序
GET users/_search
{
  "query": {
    "range": {
      "age.keyword": {
        "gte": 25, 
        "lte": 33
      }
    }
  },
  "sort": [
    {
      "age.keyword": {
        "order": "desc"
      }
    }
  ]
}

#constant_score不进行相关性算分,查询的数据进行缓存,提高效率
GET users/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "terms": {
          "hobby": [
            "movies",
            "fashion"
          ]
        }
      }
    }
  }
}
GET users/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "term": {
          "hobby": "movies"
          }
      }
    }
  }
}



#match
#查询hobby包含sneakers或者fashion的firstname和lastname
GET users/_search
{
  "_source": ["firstname","lastname"],
  "query": {
    "match": {
      "hobby": "sneakers fashion"
    }
  }
}

#match_pgrase 
#查询hobby中包含有 "sneakers movies" 这个短语的所有的数据
GET users/_search
{
  "query": {
    "match_phrase": {
      "hobby": "sneakers movies"
    }
  }
}

#muti_match
#组合查询:age或者hobby中包含33或者movies的数据
GET /users/_search
{
  "query": {
    "multi_match": {
      "query": "33 movies",
      "fields": ["age","hobby"]
    }
  }
}

#match_all
#查询所有数据
GET users/_search
{
  "query": {
    "match_all": {}
  }
}

#query_string和simple_query_string
GET users/_search
{
  "query": {
    "query_string": {
      "default_field": "hobby",
      "query": "sneakers OR movies"
    }
  }
}

GET users/_search
{
  "query": {
    "simple_query_string": {
      "query": "movies",
      "fields": ["hobby"]
    }
  }
}

#查询hobby中包含snearkers movies短语的数据
GET users/_search
{
  "query": {
    "simple_query_string": {
      "query": ""sneakers movies"",
      "fields": ["hobby"]
    }
  },
  "_source": ["firstname","lastname"]
}


#组合查询,组合must、should、must_not,实现多条件查询
GET users/_search
{
  "query": {
    "bool": {
      "must": [ 
        {
          "match": {
            "hobby": "movies"
          }
        },
        {
          "match": {
            "age.keyword": "25"
          }
        }
      ]
    }
  }
}

#filter不会进行相关性的算分,并且将计算出来的结果进行缓存,效率比query高
GET users/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "hobby": "sneakers"
          }
        },
        {
          "range": {
            "age.keyword": {
              "gte": 25,
              "lte": 55
            }
          }
        }
      ]
    }
  }
}


# 二、通过mapping创建索引
PUT employee
{
  "mappings": {
    "properties": {
      "id": {
        "type": "integer"
      },
      "name": {
        "type": "keyword"
      },
      "job": {
        "type": "keyword"
      },
      "age": {
        "type": "integer"
      },
      "gender": {
        "type": "keyword"
      }
    }
  }
}
 
 
 #批量导入数据
PUT employee/_bulk 
{"index": {"_id": 1}} 
{"id": 1, "name": "Bob", "job": "java", "age": 21, "sal": 8000, "gender": "female"} 
{"index": {"_id": 2}} 
{"id": 2, "name": "Rod", "job": "html", "age": 31, "sal": 18000, "gender": "female"} 
{"index": {"_id": 3}} 
{"id": 3, "name": "Gaving", "job": "java", "age": 24, "sal": 12000, "gender": "male"} 
{"index": {"_id": 4}} 
{"id": 4, "name": "King", "job": "dba", "age": 26, "sal": 15000, "gender": "female"} 
{"index": {"_id": 5}} 
{"id": 5, "name": "Jonhson", "job": "dba", "age": 29, "sal": 16000, "gender": "male"} 
{"index": {"_id": 6}} 
{"id": 6, "name": "Douge", "job": "java", "age": 41, "sal": 20000, "gender": "female"} 
{"index": {"_id": 7}} 
{"id": 7, "name": "cutting", "job": "dba", "age": 27, "sal": 7000, "gender": "male"} 
{"index": {"_id": 8}} 
{"id": 8, "name": "Bona", "job": "html", "age": 22, "sal": 14000, "gender": "female"}
{"index": {"_id": 9}} 
{"id": 9, "name": "Shyon", "job": "dba", "age": 20, "sal": 19000, "gender": "female"} 
{"index": {"_id": 10}} 
{"id": 10, "name": "James", "job": "html", "age": 18, "sal": 22000, "gender": "male"} 
{"index": {"_id": 11}} 
{"id": 11, "name": "Golsling", "job": "java", "age": 32, "sal": 23000, "gender": "female"} 
{"index": {"_id": 12}}
 {"id": 12, "name": "Lily", "job": "java", "age": 24, "sal": 2000, "gender": "male"} 
{"index": {"_id": 13}} 
{"id": 13, "name": "Jack", "job": "html", "age": 23, "sal": 3000, "gender": "female"} 
{"index": {"_id": 14}} 
{"id": 14, "name": "Rose", "job": "java", "age": 36, "sal": 6000, "gender": "female"} 
{"index": {"_id": 15}} 
{"id": 15, "name": "Will", "job": "dba", "age": 38, "sal": 4500, "gender": "male"} 
{"index": {"_id": 16}}
 {"id": 16, "name": "smith", "job": "java", "age": 32, "sal": 23000, "gender": "male"}


GET employee/_search/
GET employee/_doc/1

GET employee/_count

#查询工资总和
GET employee/_search
{
  "size": 0, 
  "aggs": {
    "total_sal": {
      "sum": {
        "field": "sal"
      }
    }
  }
}

#查询不同工作的薪资统计情况:sum、avg、min、max
GET employee/_search

{
  "size": 0,
  "aggs": {
    "job_inf": {
      "terms": {
        "field": "job"
      },
      "aggs": {
        "sal_info": {
          "stats": {
            "field": "sal"
          }
        }
      }
    }
  }
}

#查询不同工作,男女员工的数量。以及薪资统计情况:sum、avg、min、max
GET employee/_search
{
  "size": 0,
  "aggs": {
    "job_info": {
      "terms": {
        "field": "job"
      },
      "aggs": {
        "gender_info": {
          "terms": {
            "field": "gender"
          },
          "aggs": {
            "sal_info": {
              "stats": {
                "field": "sal"
              }
            }
          }
        }
      }
    }
  }
}

GET employee/_search
{
  "size": 0,
  "aggs": {
    "sal_info": {
      "histogram": {
        "field": "sal",
        "interval": 5000,
        "extended_bounds": {
          "min": 0,
          "max": 30000
        }
      }
    }
  }
}

#查询不同区间的员工工资统计
GET employee/_search
{
  "size": 0,
  "aggs": {
    "sal_info": {
      "range": {
        "field": "sal",
        "ranges": [
          {
            "key": "0 <= sal <= 5000",
            "from": 0,
            "to": 5000
          },
          {
            "key": "5001 <= sal <= 10000",
            "from": 5001,
            "to": 10000
          },
          {
            "key": "10001 <= sal <= 15000",
            "from": 10001,
            "to": 15000
          }
        ]
      }
    }
  }
}

#查询在不同工资区间的员工姓名
GET employee/_search
{
  "_source": ["name","job"],
  "query": {
    "range": {
      "sal": {
        "gte": 0,
        "lte": 5000
      }
    }
  }
}

GET employee/_search
{
  "size": 0,
  "aggs": {
    "sal_info": {
      "range": {
        "field": "sal",
        "ranges": [
          {
            "key": "0 <= sal <= 5000",
            "from": 0,
            "to": 5000
          },
          {
            "key": "5001 <= sal <= 10000",
            "from": 5001,
            "to": 10000
          },
          {
            "key": "10001 <= sal <= 15000",
            "from": 10001,
            "to": 15000
          }
        ]
      }
    }
  },
  "_source": ["name","job"],
  "query": {
    "match_all": {}
  }
}

GET mytest_index_2/_search
{
  "query": {
    "match_all": {}
  }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5606864.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存