获取索引信息
GET /heima/_mapping
向索引中写入数据
POST /heima/_doc
{
"title":"小米手机",
"images":"http://image.leyou.com/111.jpg",
"price": 2699.00
}
指定id向索引中写入数据
POST /heima/_doc/1
{
"title":"小米手机",
"images":"http://image.leyou.com/111.jpg",
"price": 2699.00
}
删除一条记录
DELETE /heima/_doc/aHJa23IBF0CXnrHompaE
aHJa23IBF0CXnrHompaE 为记录的_id
查询
查询索引数据
GET /heima/_search
{
"query": {
"match_all": {}
}
}
会分词后进行查询,列出所有的分词相关的 or 相关
GET /heima/_search
{
"query": {
"match": {
"title": "小米手机"
}
}
}
指定操作符号 and 与and 相关
GET /heima/_search
{
"query": {
"match": {
"title":{
"query": "小米手机",
"operator": "and"
}
}
}
}
根据匹配度查询
GET /heima/_search
{
"query": {
"match": {
"title":{
"query": "小米手机",
"minimum_should_match": "75%"
}
}
}
}
多字段查询
GET /heima/_search
{
"query": {
"multi_match": {
"query": "手机",
"fields": ["title", "subTitle"]
}
}
}
term 以分词作为查询,下面的"黑晓米会被搜索引擎拆分成多个分词", 一般不会有搜索结果.一般会用在一个不可分割的字段中,用term查询
GET /heima/_search
{
"query": {
"term": {
"title": {
"value": "黑晓米手机"
}
}
}
}
多词条查询
GET /heima/_search
{
"query": {
"terms": {
"FIELD": [
"华为",
"小米"
]
}
}
}
结果集过滤 排除了images字段
GET /heima/_search
{
"_source": {
"excludes": "images"
},
"query": {
"terms": {
"title": [
"手机"
]
}
}
}
bool查询 must查询 '且'
GET /heima/_search
{
"query": {
"bool": {
"must": [
{"match": {
"title": "手机"
}
},
{
"terms": {
"price": [
"2699.00"
]
}
}
]
}
}
}
bool查询 should 查询 '或'
GET /heima/_search
{
"query": {
"bool": {
"should": [
{"match": {
"title": "手机"
}
},
{
"terms": {
"price": [
"2699.00"
]
}
}
]
}
}
}
range查询
GET /heima/_search
{
"query": {
"range": {
"price": {
"gte": 1000,
"lte": 3000
}
}
}
}
模块查询
fuzziness 代表可以输入错误的个数, 默认是1, 最多可以设置为2
GET /heima/_search
{
"query": {
"fuzzy": {
"title": {
"value": "红米红",
"fuzziness": 1
}
}
}
}
过滤 filter 不影响得分
GET /heima/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "手机"
}
}
],
"filter": {
"range": {
"price": {
"gte": 1000,
"lte": 3000
}
}
}
}
}
}
排序 多字段排序
GET /heima/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "手机"
}
}
],
"filter": {
"range": {
"price": {
"gte": 1000,
"lte": 3000
}
}
}
}
},
"sort": [
{
"price": {
"order": "desc"
}
},
{
"_id": {
"order": "desc"
}
}
]
}
创建索引库
PUT /cars
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
},
"mappings": {
"properties": {
"color": {
"type": "keyword"
},
"make": {
"type": "keyword"
}
}
}
}
聚合查询
popular_color 聚合名称 通常加 size:0 不需要普通结果集, 加入度量
GET /cars/_search
{
"size": 0,
"aggs": {
"popular_color": {
"terms": {
"field": "color"
},
"aggs": {
"price_avg": {
"avg": {
"field": "price"
}
}
}
}
}
}
分阶统计 阶梯分组
GET /cars/_search
{
"size": 0,
"aggs": {
"price_historgram": {
"histogram": {
"field": "price",
"interval": 5000,
"min_doc_count": 0
}
}
}
}