#1.1 创建索引库
PUT /dean1

#1.2 查看索引库
GET /dean1
GET /dean2

#1.3 删除索引库
DELETE  /dean1

#2.1 添加类型&&字段
PUT /heima
PUT /heima/_mapping/goods
{
  "properties": {
    "title": {
      "type": "text",
      "analyzer": "ik_max_word"
    },
    "subtitle": {
      "type": "text",
      "analyzer": "ik_max_word"
    },
    "images": {
      "type": "keyword",
      "index": "false"
    },
    "price": {
      "type": "float"
    }
  }
}
GET /heima

#2.2 查看类型
GET /heima/_mapping/goods

#2.3 一步创建索引库&&类型&&字段
PUT /heima2
{
  "settings": {},
  "mappings": {
    "goods": {
      "properties": {
        "title": {
          "type": "text",
          "analyzer": "ik_max_word"
        }
      }
    }
  }
}
GET /heima2/_mapping/goods

#3.1 新增文档
POST /heima/goods
{
  "title":"xiaomi",
  "images":"www.xiaomi.com",
  "price":2399.00
}

#3.2 查询数据
GET /heima/goods/WvjnTHcB7Ta1Ug1IyHwg
GET /heima/goods/1
GET /heima/goods/2

#3.3 新增文档 /id
POST /heima/goods/1
{
  "title":"huawei",
  "images":"www.huawei.com",
  "price":2999.00
}

#3.4 修改文档
POST /heima/goods/2
{
  "title":"apple",
  "images":"www.apple.com",
  "price":5999.00
}

#3.5 删除文档
DELETE /heima/goods/WvjnTHcB7Ta1Ug1IyHwg
DELETE /heima/goods/1
DELETE /heima/goods/2

#3.6 删除所有
POST /heima/_delete_by_query
{
  "query":{
    "match_all":{}
  }
}

# 批量新增
POST /heima/goods/_bulk
{"index":{}}
{"title":"大米手机","images":"http://image.leyou.com/12479122.jpg","price":3288}
{"index":{}}
{"title":"小米手机","images":"http://image.leyou.com/12479122.jpg","price":2699}
{"index":{}}
{"title":"小米电视4A","images":"http://image.leyou.com/12479122.jpg","price":4288}

#4.1 查询所有
POST /heima/_search
{
  "query": {
    "match_all": {}
  }
}

#4.2 模糊搜索 ik分词
POST /heima/_search
{
  "query": {
    "match": {
      "title": "小米"
    }
  }
}

#4.3 多字段查询
# 添加新字段
POST /heima/goods
{
  "title": "华为手机",
  "images": "http://image.leyou.com/12479122.jpg",
  "price": 5288,
  "subtitle": "小米"
}
POST /heima/_search
{
  "query": {
    "multi_match": {
      "query": "小米",
      "fields": [
        "title",
        "subtitle"
      ]
    }
  }
}

#4.4 term精确查询 查询时不分词 
POST /heima/_search
{
  "query": {
    "term": {
      "price": {
        "value": "4288"
      }
    }
  }
}
# 词库中没有 “小米电视4A” 查询结果为null
POST /heima/_search
{
  "query": {
    "term": {
      "title": {
        "value": "小米电视4A"
      }
    }
  }
}

#4.5 terms多条件精确查询 查询时不分词
POST /heima/_search
{
  "query": {
    "terms": {
      "price": [
        "3288",
        "5288"
      ]
    }
  }
}

#5.1 _source结果过滤 忽略null
POST /heima/_search
{
  "query": {
    "match_all": {}
  },
  "_source": [
    "title",
    "subtitle",
    "price"
  ]
}
# excludes不显示字段
POST /heima/_search
{
  "query": {
    "match_all": {}
  },
  "_source": {
    "excludes": "title"
  }
}

#6.1 布尔组合
GET /heima/_search
{
  "query": {
    "bool": {
      "must": {
        "match": {
          "title": "小米"
        }
      },
      "must_not": {
        "match": {
          "title": "电视"
        }
      },
      "should": {
        "match": {
          "title": "手机"
        }
      }
    }
  }
}

#6.2 范围查询
POST /heima/_search
{
  "query": {
    "range": {
      "price": {
        "gte": 2688,
        "lte": 3288
      }
    }
  }
}
#6.3 模糊查询
# 添加一条数据
POST /heima/goods/4
{
    "title":"apple手机",
    "images":"http://image.leyou.com/12479122.jpg",
    "price":5899.00
}
POST /heima/_search
{
  "query": {
    "fuzzy": {
      "title": {
        "value": "applaa",
        "fuzziness": 3
      }
    }
  }
}

#7.1 分页
POST /heima/_search
{
  "query": {
    "match_all": {}
  },
  "size": 2,
  "from": 0
}

#7.2 排序
POST /heima/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "price": {
        "order": "desc"
      }
    }
  ],
  "size": 2,
  "from": 0
}

#7.3 高亮

POST /heima/_search
{
  "query": {
    "match": {
      "title": "电视"
    }
  },
  "highlight": {
    "pre_tags": "<font color='pink'>",
    "post_tags": "</font>",
    "fields": {
      "title": {}
    }
  }
}
Last modification:February 21st, 2021 at 09:58 pm
我从来都不喜欢钱,我也没碰过钱,我对钱没兴趣