Elasticsearch自带分词查询及使用分词器查询

索引的增删改查,及文档的增删改查

# 创建一个索引库
PUT /my_index

DELETE /my_index


PUT /my_index
{

  "settings": {
    "number_of_shards": 5, 
    "number_of_replicas": 1 
  }
}


GET /my_index


PUT /my_index1
{
  "mappings": {
    "properties": {
      "id":{
        "type": "long"
      },
      "name":{
        "type": "keyword"
      },
      "age":{
        "type": "integer"
      }
    }
  }  
}



#查看映射
GET /my_index1/_mapping
#文档设置值
PUT /my_index1/_doc/1
{
  "id":1,
  "name":"dafei",
  "age":18
}

PUT /my_index1/_doc/2
{
  "id":2,
  "name":"xiaofei",
  "age":20
}
PUT /my_index1/_doc/1
{
  "id":1,
  "name":"feifei",
  "age":28
}


#修改部门字段
POST /my_index1/_update/1/
{
  "doc":{
  "age":18
  }
}
#查看
GET /my_index1/_doc/1

GET /my_index1/_doc/_search


DELETE /my_index1/_doc/1

PUT /my_index1/_doc/1
{
  "id":1,
  "name":"feifei",
  "age":28
}

Elasticsearch自带分词查询及使用分词器查询

#创建索引
PUT /product
{
  "mappings": {
      "properties": {
        "id": {
	  "type": "long"
	},
        "title": {
          "type": "text", 
          "analyzer": "ik_smart", 
          "search_analyzer": "ik_smart",
          "fields": {
          "keyword": {
          "type": "keyword",
            "ignore_above": 256
            }  
          }
        },
        "price": {"type": "integer"},
        "intro": {
          "type": "text", 
          "analyzer": "ik_smart", 
          "search_analyzer": "ik_smart",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }  
          }
        },
      	"brand": {
      	  "type": "keyword" 
      	}
      }
  }
}


#添加数据
POST /product/_bulk
{"create":{"_id": 1}}
{"id":1,"title":"Apple iPhone XR (A2108) 128GB 白色 移动联通电信4G手机 双卡双待","price":5299,"intro":"【iPhoneXR限时特惠!】6.1英寸视网膜显示屏,A12仿生芯片,面容识别,无线充电,支持双卡!选【换修无忧版】获 AppleCare 原厂服务,享只换不修!更有快速换机、保值换新、轻松月付!","brand":"Apple"}
{"create":{"_id": 2}}
{"id":2,"title":"Apple 2019款 Macbook Pro 13.3【带触控栏】八代i7 18G 256G RP645显卡 深空灰 苹果笔记本电脑 轻薄本 MUHN2CH/A","price":15299,"intro":"【八月精选】Pro2019年新品上市送三重好礼,现在购买领满8000减400元优惠神劵,劵后更优惠!","brand":"Apple"}
{"create":{"_id": 3}}
{"id":3,"title":"Apple iPad Air 3 2019年新款平板电脑 10.5英寸(64G WLAN版/A12芯片/Retina显示屏/MUUL2CH/A)金色","price":3788,"intro":"8月尊享好礼!买iPad即送蓝牙耳机!领券立减!多款产品支持手写笔!【新一代iPad,总有一款适合你】选【换修无忧版】获 AppleCare 原厂服务,享只换不修!更有快速换机、保值换新、轻松月付!","brand":"Apple"}
{"create":{"_id": 4}}
{"id":4,"title":"华为HUAWEI MateBook X Pro 2019款 英特尔酷睿i5 13.9英寸全面屏轻薄笔记本电脑(i5 8G 512G 3K 触控) 灰","price":7999,"intro":"3K全面屏开启无界视野;轻薄设计灵动有型,HuaweiShare一碰传","brand":"华为"}
{"create":{"_id": 5}}
{"id":5,"title":"华为 HUAWEI Mate20 X (5G) 7nm工艺5G旗舰芯片全面屏超大广角徕卡三摄8GB+256GB翡冷翠5G双模全网通手机","price":6199,"intro":"【5G双模,支持SA/NSA网络,7.2英寸全景巨屏,石墨烯液冷散热】5G先驱,极速体验。","brand":"华为"}
{"create":{"_id": 6}}
{"id":6,"title":"华为平板 M6 10.8英寸麒麟980影音娱乐平板电脑4GB+64GB WiFi(香槟金)","price":2299,"intro":"【华为暑期购】8月2日-4日,M5青春版指定爆款型号优惠100元,AI语音控制","brand":"华为"}
{"create":{"_id": 7}}
{"id":7,"title":"荣耀20 PRO DXOMARK全球第二高分 4800万四摄 双光学防抖 麒麟980 全网通4G 8GB+128GB 蓝水翡翠 拍照手机","price":3199,"intro":"白条6期免息!麒麟980,4800万全焦段AI四摄!荣耀20系列2699起,4800万超广角AI四摄!","brand":"荣耀"}
{"create":{"_id": 8}}
{"id":8,"title":"荣耀MagicBook Pro 16.1英寸全面屏轻薄性能笔记本电脑(酷睿i7 8G 512G MX250 IPS FHD 指纹解锁)冰河银","price":6199,"intro":"16.1英寸无界全面屏金属轻薄本,100%sRGB色域,全高清IPS防眩光护眼屏,14小时长续航,指纹一健开机登录,魔法一碰传高速传输。","brand":"荣耀"}
{"create":{"_id": 9}}
{"id":9,"title":"荣耀平板5 麒麟8核芯片 GT游戏加速 4G+128G 10.1英寸全高清屏影音平板电脑 WiFi版 冰川蓝","price":1549,"intro":"【爆款平板推荐】哈曼卡顿专业调音,10.1英寸全高清大屏,双喇叭立体环绕音,配置多重护眼,值得拥有!","brand":"荣耀"}
{"create":{"_id": 10}}
{"id":10,"title":"小米9 4800万超广角三摄 6GB+128GB全息幻彩蓝 骁龙855 全网通4G 双卡双待 水滴全面屏拍照智能游戏手机","price":2799,"intro":"限时优惠200,成交价2799!索尼4800万广角微距三摄,屏下指纹解锁!","brand":"小米"}
{"create":{"_id": 11}}
{"id":11,"title":"小米(MI)Pro 2019款 15.6英寸金属轻薄笔记本(第八代英特尔酷睿i7-8550U 16G 512GSSD MX250 2G独显) 深空灰","price":6899,"intro":"【PCIE固态硬盘、72%NTSC高色域全高清屏】B面康宁玻璃覆盖、16G双通道大内存、第八代酷睿I7处理器、专业级调校MX150","brand":"小米"}
{"create":{"_id": 12}}
{"id":12,"title":"联想(Lenovo)拯救者Y7000P 2019英特尔酷睿i7 15.6英寸游戏笔记本电脑(i7 9750H 16G 1T SSD GTX1660Ti 144Hz)","price":9299,"intro":"超大1T固态,升级双通道16G内存一步到位,GTX1660Ti电竞级独显,英特尔9代i7H高性能处理器,144Hz电竞屏窄边框!","brand":"联想"}



#需求:查询商品标题中符合"游戏 手机"的字样的商品 【暂时理解为模糊查询】
GET /product/_search
{
  "query":{
    "match": {
      "title": "手机 游戏"
    }
  }
}

#需求:查询商品标题或简介中符合"蓝牙 指纹 双卡"的字样的商品
#multi_match表示可以在多个字段查询,有一个满足就符合
GET /product/_search
{
  "query":{
    "multi_match": {
      "query": "蓝牙 指纹 双卡",
      "fields": ["title","intro"]
    }
  }
}
#不加snayze,分词器,只能默认英文按单词,中文的每个字就相当于一个单词
GET /product/_analyze
{
  "text":"I am Groot"
}
GET /product/_analyze
{
  "text":"英特尔酷睿i7处理器"
}


#粗粒度分词
GET /product/_analyze
{
  "text":"I am Groot",
  "analyzer":"ik_smart"
}
#粗粒度分词
GET /product/_analyze
{
  "text":"中华人民民共和国"
  , "analyzer": "ik_smart"
}
#细粒度分词
GET /product/_analyze
{
  "text": "中华人民共和国",
  "analyzer": "ik_max_word"
}

#关键字高亮显示
GET /product/_search
{
  "query": {
    "multi_match":{
      "query":"蓝牙 指纹 双卡",
      "fields": ["title", "intro"]
    }
  },
  "highlight": {
    "fields": {
      "title": {"type": "plain"},
      "intro": {"type": "plain"}
    },
    "pre_tags": "",
    "post_tags": ""
  }
}

你可能感兴趣的:(elasticsearch)