代码语言:javascript复制
# 分词器
GET _analyze
{
"analyzer":"ik_smart",
"text":"中国地图"
}
# 分词器
GET _analyze
{
"analyzer":"ik_max_word",
"text":"中国地图"
}
# 删除索引结构
DELETE goodsdata
# 添加索引结构
PUT goodsdata
{
"mappings":{
"properties": {
"goodsId":{
"type":"text"
},
"goodsName":{
"type":"text",
"analyzer": "ik_smart"
},
"goodsImg":{
"type":"text",
"index": false
},
"price":{
"type": "integer"
},
"createDate":{
"type": "date"
},
"categoryName":{
"type": "keyword"
},
"brandName":{
"type": "keyword"
},
"spec":{
"type": "object"
},
"stocksNumber":{
"type":"integer"
},
"salesNumber":{
"type": "long"
},
"commentNumber":{
"type": "long"
}
}
}
}
# 查看索引结构
GET goodsdata
# 删除索引结构
DELETE goodsdata/_doc/_query
{
"query": {
"match_all": {}
}
}
# 插入内容 _doc是默认的类型
# 语法:PUT 索引库名/类型名/id
PUT goodsdata/_doc/1
{
"goodsId":"111",
"goodsName":"小米手机8s",
"goodsImg":"111",
"price":"543300",
"createDate":"2022-01-01",
"categoryName":"手机",
"brandName":"小米",
"spec":{
"网络制式":"移动4G",
"屏幕尺寸":"5.5"
},
"stocksNumber":"10011",
"salesNumber":"146223",
"commentNumber":"124356"
}
PUT goodsdata/_doc/2
{
"goodsId":"111",
"goodsName":"小米手机8s青春版",
"goodsImg":"111",
"price":"543300",
"createDate":"2022-01-01",
"categoryName":"手机",
"brandName":"小米",
"spec":{
"网络制式":"移动4G",
"屏幕尺寸":"5.5"
},
"stocksNumber":"10011",
"salesNumber":"146223",
"commentNumber":"124356"
}
PUT goodsdata/_doc/3
{
"goodsId":"111",
"goodsName":"红米手机Note8",
"goodsImg":"111",
"price":"543300",
"createDate":"2022-01-01",
"categoryName":"手机",
"brandName":"红米",
"spec":{
"网络制式":"移动4G",
"屏幕尺寸":"5.5"
},
"stocksNumber":"10011",
"salesNumber":"146223",
"commentNumber":"124356"
}
PUT goodsdata/_doc/4
{
"goodsId":"111",
"goodsName":"华为手机",
"goodsImg":"111",
"price":"543300",
"createDate":"2022-01-01",
"categoryName":"手机",
"brandName":"华为",
"spec":{
"网络制式":"移动4G",
"屏幕尺寸":"5.5"
},
"stocksNumber":"10011",
"salesNumber":"146223",
"commentNumber":"124356"
}
PUT goodsdata/_doc/5
{
"goodsId":"111",
"goodsName":"苹果手机iphone 8p",
"goodsImg":"111",
"price":"543300",
"createDate":"2022-01-01",
"categoryName":"手机",
"brandName":"苹果",
"spec":{
"网络制式":"移动4G",
"屏幕尺寸":"5.5"
},
"stocksNumber":"10011",
"salesNumber":"146223",
"commentNumber":"124356"
}
# 查询全部
GET goodsdata/_search
# 匹配查询
# ik_smart查询商品名称包含手机的
GET goodsdata/_search
{
"query": {
"match": {
"goodsName": "手机"
}
}
}
# 匹配查询 拆词查询了
# ik_smart查询商品名称包含苹果或手机的
GET goodsdata/_search
{
"query": {
"match": {
"goodsName": "苹果手机"
}
}
}
# 匹配查询 拆词查询并且逻辑与
# ik_smart查询商品名称包含苹果且包含手机的
GET goodsdata/_search
{
"query": {
"match": {
"goodsName": {
"query": "苹果手机",
"operator": "and"
}
}
}
}
# 多字段查询
# 查询品牌或商品名称后分类中包含小米的
GET goodsdata/_search
{
"query": {
"multi_match": {
"query": "小米",
"fields": ["brandName","goodsName","categoryName"]
}
}
}
# 词条匹配 term
# 适合查询 number、date、keyword ,不适合text
GET goodsdata/_search
{
"query": {
"term": {
"price": {
"value": "599900"
}
}
}
}
# 多词条匹配 terms
# 查询价格为599900或者488800的
GET goodsdata/_search
{
"query": {
"terms": {
"price": [599900,488800]
}
}
}
# bool查询
# 把各种其他查询通过must(与) ,must_not (非),should (或)的方式组合起来的查询
GET goodsdata/_search
{
"query": {
"bool": {
"must": [
{"term": {
"brandName": "小米"
}},
{
"match": {
"goodsName": "手机"
}
}
]
}
}
}
# 过滤查询
# 过滤是针对搜索结果进行过滤,不计算文档的匹配度分值,所以过滤器性能比匹配查询高,且方便缓存。推荐使用
GET goodsdata/_search
{
"query": {
"bool": {
"filter": [
{"match":{
"goodsName": "手机"
}}
]
}
}
}
代码语言:javascript复制# 分页
GET goodsdata/_search
{
"from": 20,
"size": 20
}
# 排序
GET goodsdata/_search
{
"from": 20,
"size": 20,
"sort": [
{
"salesNumber": {
"order": "desc"
}
}
]
}
代码语言:javascript复制# 高亮查询 默认效果
GET goodsdata/_search
{
"query": {
"match": {
"goodsName": {
"query": "苹果手机",
"operator": "and"
}
}
},
"highlight": {
"fields": {
"goodsName": {}
}
}
}
# 高亮查询 指定红色字体效果
GET goodsdata/_search
{
"query": {
"match": {
"goodsName": {
"query": "苹果手机",
"operator": "and"
}
}
},
"highlight": {
"fields": {
"goodsName": {
"pre_tags": "<font style='color:red'>",
"post_tags": "</font>"
}
}
}
}
版权属于:dingzhenhua
本文链接:https://cloud.tencent.com/developer/article/2019245
转载时须注明出处及本声明