Elasticsearch基本操作 DSL高级查询 进阶查询

Elasticsearch 基本操作 DSL高级查询 进阶查询


#解析案列demo
POST _analyze
{
  "analyzer": "standard",
  "text": "我是中国人"
}
#查询所有索引
GET _search
{
  "query": {
    "match_all": {}
  }
}

#创建索引
PUT /my_name/

#查询所有索引
GET /_cat/indices?v

#查询单个索引
GET /my_name

#删除索引
DELETE /my_name

#创建文档
PUT /my_index/_doc/1
{
  "title":"小米手机",
  "category":"北京",
  "price":9999
}

#查看文档
GET /my_index/_doc/1

#修改文档

PUT /my_index/_doc/1
{
  "title":"华为手机",
  "category":"北京公司",
  "price":99990.0
}

#修改局部属性
POST /my_index/_update/1
{
  "doc":{
  "price":88888
  }
}
POST /my_index/_update/1
{
  "doc":{
    "price":4232
  }
}

#删除文档
DELETE /my_index/_doc/1

#批量添加
POST _bulk
{"create":{"_index":"my_index","_id":1}}
{"title":"小米","price":999}
{"create":{"_index":"my_hua","_id":2}}
{"title":"华为","price":8888}

GET /my_index/_doc/1
GET /my_hua/_doc/2

#批量删除
POST _bulk
{"delete":{"_index":"my_index","_id":1}}
{"delete":{"_index":"my_hua","_id":2}}


#查看映射
GET /my_index/_mapping

#动态映射

#静态映射
PUT /my_index
{
  "mappings": {
    "properties": {
      "title":{
        "type": "text",
        "index": true,
        "store": true,
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_smart"
      },
      "category":{
        "type": "keyword",
        "index": true,
        "store": true
      },
      "image":{
        "type": "keyword",
        "index": true,
        "store": true
      },
      "price":{
        "type": "long",
        "index": true,
        "store": true
      }
    }
  }
}
#查看创建的映射
GET /my_index/_mapping
#删除索引
DELETE /my_index



#DSL高级查询

POST _bulk
{"create":{"_index":"my_index","_id":1}}
{"id":1,"title":"华为笔记本电脑","category":"华为","images":"http://www.gulixueyuan.com/xm.jpg","price":5388}
{"create":{"_index":"my_index","_id":2}}
{"id":2,"title":"华为手机","category":"华为","images":"http://www.gulixueyuan.com/xm.jpg","price":5500}
{"create":{"_index":"my_index","_id":3}}
{"id":3,"title":"VIVO手机","category":"vivo","images":"http://www.gulixueyuan.com/xm.jpg","price":3600}

GET /my_index/_doc/3


#分析倒排索引
POST _analyze
{
  "analyzer": "ik_smart",
  "text": "华为笔记本电脑"
}
POST _analyze
{
  "analyzer": "ik_smart",
  "text": "华为手机"
}
POST _analyze
{
  "analyzer": "ik_smart",
  "text": "VIVO手机"
}
#华为 笔记本电脑
#华为 手机
#vivo 手机

#查询所有文档
GET /my_index/_search
{
  "query": {
    "match_all": {}
  }
}
#匹配查询
GET /my_index/_search
{
  "query": {
    "match": {
      "title": "华为智能手机"
    }
  }
}

#条件删除
POST /my_index/_delete_by_query
{
  "query":{
    "match":{
      "title":"vivo"
    }
  }
}

#多字段匹配
#multi_match
POST /my_index/_search
{
  "query": {
    "multi_match": {
      "query":"华为智能手机",
      "fields":["title","category"]
    }
  }
}
#前缀匹配
POST /my_index/_search
{
  "query": {
    "prefix": {
      "category": {
        "value": "华为"
      }
    }
  }
}

#关键字精确查询
#term关键字不会进行分词
POST /my_index/_search
{
  "query": {
    "term": {
      "title": {
        "value": "华为笔记本电脑"
      }
    }
  }
}

#多关键字查询
POST /my_index/_search
{
  "query": {
    "terms": {
      "title": [
        "华为",
        "华为手机"
      ]
    }
  }
}

#范围查询
POST /my_index/_search
{
  "query": {
    "range": {
      "price": {
        "gte": 3000,
        "lte": 6000
      }
    }
  }
}

#指定返回字段查询
POST /my_index/_search
{
  "query": {
    "range": {
      "price": {
        "gte": 1000,
        "lte": 20000
      }
    }
  },
  "_source": ["title","category","price"]
}

#组合查询
#must
POST /my_index/_search
{
  "query": {
    "bool": {
      "must": [
        {"match": {
          "title": "华为"
        }},
        {
          "range": {
            "price": {
              "gte": 5500,
              "lte": 6000
            }
          }
        }
      ]
    }
  }
}

#组合查询
#should
POST /my_index/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "title": "华为"
          }
        },
        {
          "range": {
            "price": {
              "gte": 1000,
              "lte": 5000
            }
          }
        }
      ]
    }
  }
}

#组合查询
#should and must
POST /my_index/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "title": "华为"
          }
        },
        {
          "range": {
            "price": {
              "gte": 100,
              "lte": 20000
            }
          }
        }
      ],
      "must": [
        {
          "match": {
            "title": "华为"
          }
        },
        {
          "range": {
            "price": {
              "gte": 5500,
              "lte": 6000
            }
          }
        }
      ]
    }
  }
}

#组合查询
#must_not
POST /my_index/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "match": {
            "title": "华为"
          }
        }
      ]
    }
  }
}

#组合查询
#filter
POST /my_index/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "match":{
            "title":"华为"
          }
        }
      ]
    }
  }
}
#聚合查询 
#max
POST /my_index/_search
{
  "query": {
    "match_all": {}
  },
  "size": 0,
  "from": 0,
  "aggs": {
    "max_price": {
      "max": {
        "field": "price"
      }
    }
  }
}
#min
POST /my_index/_search
{
  "query": {
    "match_all": {}
  },
  "size": 0,
  "from": 0,
  "aggs": {
    "min_price": {
      "min": {
        "field": "price"
      }
    }
  }
}
#avg
POST /my_index/_search
{
  "query": {
    "match_all": {}
  },
  "size": 0,
  "from": 0,
  "aggs": {
    "avg_price": {
      "avg": {
        "field": "price"
      }
    }
  }
}
#sum
POST /my_index/_search
{
  "query": {
    "match_all": {}
  },
  "size": 0,
  "from": 0,
  "aggs": {
    "sum_price": {
      "sum": {
        "field": "price"
      }
    }
  }
}
#stats
POST /my_index/_search
{
  "query": {
    "match_all": {}
  },
  "size": 0,
  "from": 0,
  "aggs": {
    "stats_price": {
      "stats": {
        "field": "price"
      }
    }
  }
}
#terms 桶聚合相当于group by
POST /my_index/_search
{
  "query": {
    "match_all": {}
  },
  "size": 0,
  "aggs": {
    "groupby_category": {
      "terms": {
        "field": "category",
        "size": 10
      }
    }
  }
}
#进阶查询
POST /my_index/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "price": {
        "order": "desc"
      }
    }
  ]
}
#分页查询
POST /my_index/_search
{
  "query": {
    "match_all": {}
  },
  "from": 1,
  "size": 2
}
#高亮查询
POST /my_index/_search
{
  "query": {
    "match_all": {}
  },
  "highlight": {
    "pre_tags": "<b style='color:red'>",
     "post_tags": "</b>",
     "fields": {
       "title": {}
     }
  }

}
#近似查询

PUT /test
PUT /test/_doc/1
{
  "title":"hello world"
}

#fuzzy查询
GET /test/_search
{
  "query": {
    "fuzzy": {
      "title": {
        "value": "word"
      }
    }
  }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值