elasticsearch 7.x 增删改查

elasticsearch 7.x 增删改查,第1张

elasticsearch 7.x 增删改查 索引shema  

新增索引

PUT /student
{
  "settings":
  {
     "number_of_shards" : 5,
     "number_of_replicas" : 1
  }
  , "mappings":
  {
    "properties": 
    {
      "title":{"type":"text"},
      "name":{"type":"text"},
      "age":{"type":"integer"}
    }
  }
}

修改索引

索引修改只能新增字段,无法修改类型

PUT /student/_mapping
{
 
    "properties": 
    {

      "tag":{"type":"text"}
    }
  
}

删除索引

DELETE student

错误mapping一旦设定将不能修改

PUT /student/_mapping
{
 
    "properties": 
    {

      "age":{"type":"long"}
    }
  
}

 索引document 增删改查

#修改或添加(存在则修改)
POST student/_doc/1
{
  "id":1,
  "name":"zyw",
  "age":50
  
}
POST student/_doc/3
{
  "id":3,
  "name":"ssh",
  "age":32
  
}
#添加
POST student/_doc/1
{
  "id":1,
  "name":"ssh",
  "age":20
  
}
#修改部分字段内容
POST student/_update/2
{
 "doc": {
   "age":40
 }
}
#添加
POST student/_create/2
{
  "id":2,
  "name":"ly",
  "age":30
}
#删除
DELETE student/_doc/2
GET student/_doc/1




#range 查询
GET /food/_search
{
   "query":{
      "range": {
        "price": {
            "gte":  1.0,
            "lt":   7
        }
     }     
   }

}
#multi_match
GET /food/_search
{
   "query":{
      "multi_match": {
        "query":    "白菜",
        "fields":   [ "name", "desc" ]
    }
   }
}
#match_all 查询
#match_all 查询简单的匹配所有文档。在没有指定查询方式时,它是默认的查询:
GET /food/_search
{
  "query":
    { "match_all": {}}
}
#term 查询
#term 查询被用于精确值匹配,这些精确值可能是数字、时间、布尔或者那些 not_analyzed 的字符串:
GET /food/_search
{
  "query": {
    "term": {
      "price": 7
    }
  }
}
#terms 查询和 term 查询一样,但它允许你指定多值进行匹配。如果这个字段包含了指定值中的任何一个值,那么这个文档满足条件
GET /food/_search
{
  "query":{
     "terms": { "desc": [ "白菜", "辣椒", "菠菜" ] }
  }
}
#布尔过滤器复合查询
GET /food/_search
{
   "query" : {
            "bool" : {
              "should" : [
                 { "term" : {"price" : 2}}, 
                 { "term" : {"name" : "bo cai"}} 
              ],
              "must_not" : {
                 "term" : {"price" : 30} 
              }
           }
        
      
   }
}
#布尔过滤器嵌套
GET /food/_search
{
  "query":{
        "bool":{
              "should" : [
                { "term" : {"price": "2"}}, 
                { "bool" : { 
                  "must" : [
                    { "term" : {"name" : "bo cai"}}, 
                    { "term" : {"price" : 7}} 
                  ]
                }}
              ]
           }
  }
}
# constant_score不会算分同时缓存,只能使用term查询
GET /food/_search
{
    "query" : {
        "constant_score" : {
            "filter" : {
                "terms" : { 
                    "price" : [2, 7]
                }
            }
        }
    }
}
#simple_query_string 针对text keyword 无效
GET food/_search
{
  "query": {
    "simple_query_string": {
      "query": "鹦鹉菜 菠菜",
      "fields": ["name","desc"],
      "default_operator": "AND"
    }
  }
}
#multi_match 多字段
GET food/_search
{
  "query": {
    "multi_match": {
        "query":                "菠菜",
        "type":                 "best_fields", 
        "fields":               [ "name", "desc" ],
        "tie_breaker":          0.3,
        "minimum_should_match": "30%" 
    } 
  }
}
#多索引_all 搜索
GET student,food/_search?q=bo cai
#高亮色
GET food/_search
{
  "query":{
    "match": {
      "name": "bo cai"
    }
  } , 
  "highlight": {
    "pre_tags": "",
    "post_tags": "",
    "fields": {
      "name": {}
    }
  
  }
}
#建议搜索单词拼写错误
POST food/_search
{
 
  "suggest" : {
    "my-suggestion" : {
      "text" : "cai",
      "term" : { 
        "field" : "desc" 
      }
    }
  }
}
DELETE food
#自动补全
PUT /food
{
  "mappings":{
     "properties": 
    {
      "id":{"type":"integer"},
      "name":{"type":"keyword"},
      "price":{"type":"double"},
      "desc":{"type":"text","analyzer": "ik_smart"},
      "address":{"type":"completion"}
    }
  }
}
POST /food/_bulk
{ "index": { "_id": 5 }}
{ "price":10,"name":"huang gua","desc":"黄瓜(学名:Cucumis sativus L.)葫芦科一年生蔓生或攀援草本植物。茎、枝伸长,有棱沟,被白色的糙硬毛。卷须细。叶柄稍粗糙,有糙硬毛;叶片宽卵状心形,膜质,裂片三角形,有齿。雌雄同株。雄花:常数朵在叶腋簇生;花梗纤细,被微柔毛;花冠黄白色,花冠裂片长圆状披针形。雌花:单生或稀簇生;花梗粗壮,被柔毛;子房粗糙。果实长圆形或圆柱形,熟时黄绿色,表面粗糙。种子小,狭卵形,白色,无边缘,两端近急尖。花果期夏季","address":"新疆维吾尔族自治区" }
{ "index": { "_id": 6 }}
{ "price":12,"name":"hong shu","desc":"番薯是一种高产而适应性强的粮食作物,与工农业生产和人民生活关系密切。块根除作主粮外,也是食品加工、淀粉和酒精制造工业的重要原料,根、茎、叶又是优良的饲料","address":"大和民族" }
{ "index": { "_id": 7 }}
{ "price":13,"name":"fan qie","desc":"番茄原产南美洲,中国南北方广泛栽培。番茄的果实营养丰富,具特殊风味。可以生食、煮食、加工番茄酱、汁或整果罐藏","address":"大国风范" }
GET /food/_search
{
    "suggest": {
     "index-suggest" : {
        "prefix" : "大",
        "completion" : {
            "field" : "address"
        }
    }
    }
   
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5704970.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)