充电复习之ES 基础语法

#创建一个索引索引
PUT /es_sms
{
  "settings": {
    "number_of_replicas": 1, #指定副本集
    "number_of_shards": 1 #指定分片数
  },
  "mappings": {
    "properties": {
      "id":{"type": "integer"},
      "phone":{"type":"keyword"},
      "content":{"type":"text","analyzer":"ik_max_word","search_analyzer":"ik_smart"}
      #指定分词器及搜索分词器  用于提供查得率和查准率
    }
    
  }
  
}


#根据索引去取创建数据

PUT /es_sms/_doc/1
{
  "phone":"1887321111",
  "content":"【苹果树】你消费了10元,还剩下100元"
  
}
#重建索引
POST _reindex
{
   "source": {
     "index": "es_sms"
   },
   "dest": {
     "index": "es_sms1"
   }
}

#重建索引 只同步部分字段
POST _reindex
{
   "source": {
     "index": "es_sms",
     "_source":["id","phone"]
   },
   "dest": {
     "index": "es_sms1"
   }
}
#重建索引 只同步部分字段 只同步满足条件的数据
POST _reindex
{
   "source": {
     "index": "es_sms",
     "_source":["id","phone"],
     "query": {
       "term": {
         "id": {
           "value": 5
         }
       }
     }
   },
   "dest": {
     "index": "es_sms1"
   }
}


GET /es_sms1/_search
#不主动指定索引 这里需要把auto_create_index 设为true
PUT /es_sms/_doc
{
  "phone":"11111111",
  "content":"【苹果树警告】您的工作还有一部分没有做完"
  
}
#查看es配置

GET /_cluster/settings
#修改es配置
PUT /_cluster/settings
{
  "persistent": {
    "action.auto_create_index":true
  }
}
#指定_create防止重复创建
PUT /es_sms/_create/3
{
  "phone":"11111333111",
  "content":"【上海警告】您的工作还有一部分没有做完1"
}
#根据id去查找数据
GET /es_sms/_doc/1
#搜索全部
GET /es_sms/_search

#修改部分字段

POST /es_sms/_update/4
{
  "doc":{
    "id":4
  }
}
#match查询 安装字段上定义的分词分析后去索引内查询
GET /es_sms/_search
{
  "explain": true, 
  "query": {
    "match": {
      "content": "我的工作"
    }
  }
}
#tf 词频 这个document文档包含了多少个这个词,包含越多则关联越多
# idf 逆文档频率 包含该词的文档总数目
#tfnorm 根据field长度做归一化,文档内出现频率越高,field越短越相关

#term查询 不进行分词,直接搜索关键字和索引内词的精确匹配 .match分词后的and和or
GET /es_sms/_search
{
  "explain": true, 
  "query": {
    "term": {
      "content": {
        "value": "我的工作"
      }
    }
  }
}
#多字段查询 multi_match
GET /es_sms/_search
{
  "explain": true, 
  "query": {
    "multi_match": {
      "query": "我的工作",
      "fields": ["content^10","phone"],
      "tie_breaker": 0.1
    }
    
  }
}

#Bool查询
#must:必须多是true
#most not:必须多是false
#should:其中一个为true即可 但true的越多得分越高

GET /es_sms/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "content": "我的工作"
           
          }
        },
        
        {
          "match": {
            "phone": "TEXT"
          }
          
        }
          
      ]
    }
  }
}
#query string查询
#方便利用 AND OR NOT
GET /es_sms/_search
{
  "query": {
    "query_string": {
      "fields": ["content"],
      "query": "a AND b"
    }
  }
}

#filter过滤查询 没有sort能力 
GET /es_sms/_search
{
  "query": {
      "bool": {
        "filter": {
          "term": {
            "content": "工作"
          }
        }
      }
  }
}
#多字段filter查询
GET /es_sms/_search
{
  "query": {
      "bool": {
        "filter": [
           {"term":
            {"content": "工作"}
          },
          
           {"term":
            {"content": "上海"}
          }
        ]
      }
  }
}
#Sort排序
GET /es_sms/_search
{
  "query": {
      "bool": {
        "filter": [
           {"term":
            {"content": "工作"}
          },
          
           {"term":
            {"content": "上海"}
          }
        ]
      }
  },
  "sort": [
    {
      "id": {
        "order": "desc"
      }
    }
  ]
}

GET _analyze?pretty
{
  "analyzer": "ik_smart",
  "text": "【上海警告】您的工作还有一部分没有做完1"
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值