ElasticSearch语法练习

GET _search
{
  "query": {
    "match_all": {}
  }
}
POST _analyze
{
  "analyzer":"ik_max_word",
  "text":"歼10系列战斗机"
}
POST _analyze
{
  "analyzer":"pinyin",
  "text":"歼10系列战斗机"
}
PUT /myinde
{
  "settings": {
    "number_of_shards": 1
    , "number_of_replicas": 0
  }
}
HEAD /myinde
DELETE /myinde

PUT /myinde/_mapping
{
  "properties":{
    "name":{
      //字段设置
      "type":"text",
      //是否索引
      "index":true,
      //展示结果是否包含该内容,es中无效
      "store":true,
      //必须对应text
      "analyzer":"ik_max_word"
    },
    "age": {
      "type":"integer",
      "index":true,
      "store":true
    },
    "address":{
      "type":"text",
      "index":true,
      "store":true,
      "analyzer":"ik_max_word"
    },
    "birthday":{
      "type":"date",
      "index":true,
      "format":"yyyy-MM-dd HH:mm:ss"
    }
  }
}

#插入doc
POST /myinde/_doc
{
  "name":"性格温顺的赵六",
  "age":21,
  "address":"湖南省湘西洲凤凰县",
  "birthday":"1997-09-08 18:00:00"
}
POST /myinde/_doc
{
  "name":"脾气暴躁的王五",
  "age":19,
  "address":"山东省青岛市螺丝县",
  "birthday":"1999-09-08 18:00:00"
}

PUT /myinde/_doc/_bulk
  {"index":{"_id":1}}
  {"name":"暴躁且温顺的六耳","age":39,"address":"广州省深圳市宝安区","birthday":"1989-09-08 18:00:00"}
  {"index":{"_id":2}}
  {"name":"爱吃螺丝粉的王二麻子","age":9,"address":"北京市朝阳区中心街道","birthday":"2009-09-08 18:00:00"}

#查询文档
GET /myinde/_search

GET /myinde/_doc/1

#批量查询
GET /_mget
{
    "docs": [
        {
            "_index": "索引库名称1",
            "_type": "映射类型1",
            "_id":"查询文档id1"
        },
        {
       		"_index": "索引库名称2",
       		"_type": "映射类型2",
       		"_id":"查询文档id2"
    	}
    ]
}

#term查询
GET /myinde/_doc/_search
{
  "query":{
    "term":{
      "address":{
        "value":"青岛"
      }
    }
  }
}

#bool查询
GET /myinde/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "脾气"
          }
        }
      ],
      //可以包含也可以不包含的查询
      "should": [
        {"match": {
          "address": "北京"
        }},{
          "range": {
            "age": {
              "gte": 10,
              "lte": 20
            }
          }
        }
      ],
      //一定不能包含的查询
      "must_not": [
        {
          "range": {
            "age": {
              "gte": 0,
              "lte": 10
            }
          }
        }
      ],
      //"should": 必须匹配的最小值
      "minimum_should_match": 1
    }
  }
}

#boosting查询
GET /myinde/_search
{
  "query": {
    "boosting": {
      "positive": {
        "match": {
          "name": "的"
        }
      }, 
      //从positive的结果集再进行操作
      //进行评分修改,结果为原评分*"negative_boost" 
      "negative": {
        "range": {
          "FIELD": {
            "gte": 10,
            "lte": 20
          }
        }
      },
      "negative_boost": 0.2
    }
  }
}

#分页
GET /myinde/_search
{
  
}

#聚合
GET /myinde/_search
{
  "query": {
    "match_all": {}
  },
  "size": 0, 
  "aggs": {
    "NAME": {
      "avg": {
        "field": "age"
      }
    },
    "count":{
      "cardinality": {
        "field": "age"
      }
    },
    "satus":{
      "stats": {
        "field": "age"
      }
    }
  }
}

#范围分桶
GET /myinde/_search
{
  "query": {
    "match_all": {}
  },
  "aggs": {
    //桶
    "age_range": {
      "range": {
        "field": "age",
        "ranges": [
          {
            "from": 0,
            "to": 10
          },
          {
            "from": 10,
            "to": 50
          }
        ]
      },
      //指标
      "aggs": {
        "age_sum": {
          "sum": {
            "field": "age"
          }
        }
      }
    }
  }
}

#自定义文档相关性
GET /myinde/_search
{
  "query": {
    "function_score": {
      "query": {
        "match": {
          "name": "暴躁且温顺"
        }
      },
      "functions": [
        {
          "filter": {
            "range": {
              "age": {
                "gte": 10,
                "lte": 20
              }
            }
          },
          "weight": 2
        }
      ]
    }
  }
}

#衰减函数
GET /myinde/_search
{
  "query": {
    "function_score": {
      "query": {
        "match": {
          "name": "暴躁且温顺"
        }
      },
      "functions": [
        {
          "gauss": {
            "age": {
              "origin": "19",
              "scale": "10"
            }
          }
        }
      ],
      "boost_mode": "replace"
    }
  }
}

POST _analyze
{
  "analyzer":"ik_max_word",
  "text":"暴躁且温顺的六耳"
}

#嵌套类型
PUT /test_index

PUT /test_index/_mapping
{
  "properties": {
    "name":{
      "type":"keyword"
    },
    "age":{
      "type":"integer"
    },
    "classes":{
      //嵌套类型
      "type":"nested",
      "properties":{
        "className":{
          "type":"keyword"
        },
        "sum":{
          "type":"integer"
        }
      }
    }
  }
}

#插入方式
PUT

#查询方式
GET







  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值