ElasticSerch查询语句

#新增索引库
PUT /heima
#查询索引库
GET /heima
#删除索引库
DELETE /heima

#创建映射
PUT /heima/_mapping
{
“properties”:{
“title”:{
“type”:“text”,
“analyzer”:“ik_smart”
},
“images”:{
“type”:“keyword”,
“index”:“false”
},
“price”:{
“type”:“float”
}
}
}

#创建索引库和映射
PUT /heima2
{
“mappings”: {
“properties”: {
“title”:{
“type”: “text”,
“analyzer”: “ik_max_word”
},
“images”:{
“type”: “keyword”,
“index”:“false”
},
“price”:{
“type”: “float”
}
}
}
}

#查看索引库的名称
GET /heima/_mapping
GET /heima2/_mapping

#新增文档
POST /heima/_doc
{
“title”:“小米手机”,
“images”:“http://image.leyou.con/11222.jpg”,
“price”:2699.00
}

#新增文档并指定id
POST /heima/_doc/2
{
“title”:“小米手机”,
“images”:“http://image.leyou.con/11222.jpg”,
“price”:2699.00
}

#查看文档
GET /heima/_doc/3

#修改文档
PUT /heima/_doc/1
{
“title”:“华为手机”,
“images”:“http://image.leyou.con/11222.jpg”,
“price”:2899.00
}

PUT /heima/_doc/3
{
“title”:“大米手机pro”,
“images”:“http://image.leuoi.jpg”,
“price”:3099.0
}

#删除文档
DELETE /heima/_doc/3

#新增未映射的字段
POST /heima/_doc/4
{
“title”:“超大幂手机”,
“images”:“http://leyou.com/12334”,
“price”:3999.00,
“stock”:200,
“saleable”:true,
“subtitle”:“超级双摄,以万像素”
}

#索引库的映射关系:
GET /heima

#动态模板
PUT heima03
{
“mappings”: {
“properties”: {
“title”: {
“type”: “text”,
“analyzer”: “ik_max_word”
}
},
“dynamic_templates”: [
{
“string”:{
“match_mapping_type”:“string”,
“mapping”:{
“type”:“keyworld”
}
}
}
]
}
}

#新增一条数据
POST /heima3/_doc/1
{
“title”:“超大米手机”,
“images”:“http://image.leyou.com/12479122.jpg”,
“price”:3299.00
}

#查看映射
GET /heima/_mapping

#插入两条数据
PUT /heima/_doc/5
{
“title”:“小米电视4A”,
“images”:“http://images.leyou.com/1111”,
“price”:3899.00
}

PUT /heima/_doc/6
{
“title”:“乐视电视4X”,
“images”:“http://images.leyou/1222”,
“price”:2499.00
}

#查询所有
GET /heima/_search
{
“query”:{
“match_all”: {}
}
}

#分词查询match
GET /heima/_search
{
“query”:{
“match”:{
“title”:“小米电视”
}
}
}

#精确查找,小米和电视
GET /heima/_search
{
“query”:{
“match”: {
“title”: {“query”:“小米电视”,“operator”: “and”}
}
}
}

GET /heima/_mapping
#词条匹配,查询的是一个词条,不会被分词
GET /heima/_search
{
“query”:{
“term”:{
“price”:2699.00
}
}
}

#fluzzy模糊查询

模糊查询 fuzzy

GET /heima/_search
{
“query”: {
“fuzzy”: {
“title”: {“value”: “小米”, “fuzziness”: 1}
}
}
}

#范围查询
GET /heima/_search
{
“query”: {
“range”: {
“price”: {
“gte”: 1000,
“lte”: 3000
}
}
}
}

#布尔查询
GET /heima/_search
{
“query”: {
“bool”: {
“must”: {
“match”: {
“title”: “小米”
}
},

"must_not": {
  "match": {
    "title": "电视"
  }
  }
}

}
}

#排序由
GET /heima/_search
{
“query”: {
“match”: {
“title”: “小米电视”
}
},
“sort”: [
{
“price”: {
“order”: “desc”
}
}
]
}

GET /heima/_search
{
“query”:{
“match”:{
“title”:“手机”
}
},
“highlight”: {
“pre_tags”: “”,
“post_tags”: “
”,
“fields”: {
“title”: {}
}
}
}

GET /heima/_search
{
“query”: {
“match_all”: {}
},
“sort”: [
{
“price”: {
“order”: “asc”
}
}
],
“from”:0,
“size”: 20
}

#filter过滤

GET /heima/_search
{
“query”: {
“bool”: {
“must”: [
{
“match”: {
“title”: “小米手机”
}
},
{
“range”: {
“price”: {
“gte”: 2000,
“lte”: 3200
}
}
}
]
}
}
}

GET /heima/_search
{
“query”: {
“bool”: {
“must”: {
“match”: {
“title”: “小米手机”
}
},
“filter”: [
{
“range”:{
“price”:{
“gt”:2000,
“lt”:3200
}
}
}
]
}
}
}

#source 筛选
GET /heima/_search
{
“_source”: [“title”,“price”],
“query”: {
“term”: {
“price”: 2699
}
}
}

#指定includes和excludes

GET /heima/_search
{
“_source”: {
“includes”: [
“title”,
“price”
]
},
“query”: {
“term”: {
“price”: 2699
}
}
}

GET /heima/_search
{
“_source”: {
“excludes”: [“images”]
},
“query”: {
“term”: {
“price”: {
“value”: “2699”
}
}
}
}

Elasticsearch是一个开源的分布式搜索和分析引擎,它提供了丰富的查询语句来进行数据检索和分析。下面是一些常用的Elasticsearch查询语句的介绍: 1. Match查询:用于执行全文搜索,它会将查询字符串与指定字段进行匹配。例如: ``` GET /index/_search { "query": { "match": { "field": "query_string" } } } ``` 2. Term查询:用于精确匹配某个字段的值。例如: ``` GET /index/_search { "query": { "term": { "field": "value" } } } ``` 3. Range查询:用于匹配指定范围内的值。例如: ``` GET /index/_search { "query": { "range": { "field": { "gte": "start_value", "lte": "end_value" } } } } ``` 4. Bool查询:用于组合多个查询条件,支持must、must_not、should等逻辑操作符。例如: ``` GET /index/_search { "query": { "bool": { "must": [ { "match": { "field1": "value1" } }, { "match": { "field2": "value2" } } ], "must_not": [ { "term": { "field3": "value3" } } ], "should": [ { "term": { "field4": "value4" } } ] } } } ``` 5. Aggregation聚合查询:用于对数据进行分组和统计分析。例如: ``` GET /index/_search { "aggs": { "group_by_field": { "terms": { "field": "field" }, "aggs": { "stats": { "stats": { "field": "numeric_field" } } } } } } ``` 这些只是Elasticsearch查询语句的一小部分,Elasticsearch还提供了更多的查询语句和功能,如模糊查询、通配符查询、正则表达式查询、地理位置查询等。你可以根据具体的需求选择合适的查询语句来进行数据检索和分析。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值