ElasticSearch-DSL语句使用

ELasticSearch的安装

ElasticSearchMysql
IndexTable
Type废弃Table废弃
DocumentRow
FieldColumn
MappingSchema
Everything is indexedIndex
Query DSLSQL
GET http://...select * from
POST http://...update table set ...
Aggregationsgroup by\sum\sum
cardinality去重 distinct
reindex数据迁移

 

#查询当前集群状态 --status字段显示为yellow,表示缺少副本

GET /_cat/health?v

#查询当前节点情况

GET /_cat/nodes?v

#查询索引列表

GET /_cat/indices?v

#查看user索引的分片情况

 GET /_cat/shards/user?v

#查询user索引的情况

GET /user

#删除person索引

delete /person

#创建user索引

PUT /user
{
  "mappings": {
    "properties": { 
       "name": {
        "type": "text",
    "index":true
      },
       "age": {
        "type": "integer"
      },
       "sex": {
        "type": "keyword",
        "index":false
      },
       "remark": {
        "type": "text"
      },
       "birthday": {
        "type": "date"
      },
      "parents": { 
        "properties": {
          "name": { 
            "properties": {
              "first": { "type": "text" },
              "last":  { "type": "text" }
            }
          }
        }
      }
    }
  }
}

#创建索引person

PUT /person
 {
   "mappings": {
      "properties":{
        "id":{
          "type": "keyword",
          "index": false,
          "store": true
        },
        "name":{
          "type": "text",
          "index": true,
          "store": true
        },
         "age":{
          "type": "integer",
          "index": true,
          "store": true
        },
         "birthday":{
          "type": "date",
          "index": false,
          "store": true
        },
         "remark":{
          "type": "text",
          "index": true
        },
         "sex":{
          "type":"text"
        },
         "price":{
          "type":"float"
        }
      }
   }
}


#修改索引person的别名

POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "person",
        "alias": "person_test"
      }
    }
  ]
}

#删除person索引别名

POST /_aliases
{
    "actions" : [
        { 
          "remove" :
          { 
            "index" : "person",
            "alias" : "person_test" 
          } 
          
        }
    ]
}

#查看user索引映射关系

GET /user/_mapping

# 默认查10条

GET /user/_search

#查询指定ID的索引

GET /user/_doc/1001

#为索引person新建文档

put /person/_doc/1009
{
    "id":"1009",
    "name":"王小兵",
    "age":55,
    "birthday":"1985-12-03",
    "remark":"我的备注是张九灵",
    "sex":"woman",
    "price":40,
    "desc":"这是啥啊"
}

#为索引user新建文档id为1001

put /user/_doc/1001
{
    "id": "1001",
    "name": "张三",
    "age": 24,
    "birthday": "1982-12-03",
    "remark": "我的备注是张三",
    "price": 20.56,
    "parents": {
        "name": {
            "first": "张",
            "last": "三"
        }
    }
}

#为索引person新建文档

put /person/_doc/1002
{
    "id":"1002",
    "name":"张九灵",
    "age":24,
    "birthday":"1982-12-03",
    "remark":"我的备注是张九灵",
    "price":20.56,
    "parents1.name1.first1":"1三",
    "parents.name.center":"1张"
}

#修改索引为person的文档(原有的其他字段都会消失)

post /person/_doc/1003
{
  "name": "张九灵",
  "birthday": "1982-10-03",
  "remark": "哈哈",
  "sex": "woman"
}

#删除索引为person的文档1003

DELETE person/_doc/1003

#基本查询(不能设置查询多个条件)match_all

GET /person/_search
{
    "query":{
        "match_all":{
            
        }
    }
}

#基本查询-match

GET /person/_search
{
    "query":{
        "match":{
            "desc":"这是啥啊"
        }
    }
}

#基本查询-match -and

GET /person/_search
{
    "query":{
        "match":{
            "desc":{
              "query":"这是啥啊",
              "operator":"and"
            }
        }
    }
}

#多字段查询

GET /person/_search
{
    "query":{
        "multi_match": {
            "query":    "测试",
            "fields":   [ "desc", "remark" ]
        }
    }
}

#词条匹配(term)-查询被用于精确值 匹配

GET /person/_search
{
    "query":{
        "term":{
            "price":20.56
        }
    }
}

#多词条精确匹配(terms)

GET /person/_search
{
    "query":{
        "terms":{
            "price":[20.56,40]
        }
    }
}

#结果过滤

GET /person/_search
{
  "_source": ["name","price"],
  "query":{
    "match_all":{
        
    }
  }
}

#指定includes和excludes

GET /person/_search
{
  "_source": {
     "includes":["name","price"],
      "excludes": ["name"]
  },
  "query":{
    "match_all":{
        
    }
  }
}

#高级查询布尔组合(bool)

GET person/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "match": {
            "desc": "啥啊"
          }
        }
      ],
      "must": [
        {
          "match": {
            "desc": "我是"
          }
        },
         {
          "match": {
            "age": 24
          }
        }
      ]
    }
  }
}

#范围查询(range)gt大于/gte大于等于/lt小于/lte小于等于

GET person/_search
{
  "query": {
    "range": {
      "price": {
        "gte": 30,
        "lte": 50
      }
    }
  }
}

#模糊查询fuzzy ,与term 查询的模糊等价

GET /person/_search
{
  "query": {
    "fuzzy": {
      "desc":"这"
    }
  }
}


#偏差不能超过2

GET /person/_search
{
  "query": {
    "fuzzy": {
      "desc": {
          "value":"测试",
          "fuzziness":1
      }
    }
  }
}

#过滤(filter)

GET person/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "desc": "我是"
          }
        }
      ],
      "filter": {
        "range": {
          "price": {
            "gte": 30,
            "lte": 50
          }
        }
      }
    }
  }
}

#单字段排序(Sort)是对查询后做的,因此在query查询对象外面

GET /person/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "desc": "我是"
          }
        }
      ]
    }
  },
  "sort": [
    {
      "price": {
        "order": "desc"
      }
    },
    {
      "age": {
        "order": "desc"
      }
    }
  ]
}

参考文档

ELK官网:https://www.elastic.co/
ELK官网文档:https://www.elastic.co/guide/index.html
ELK中文手册:https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.html
ELK中文社区:https://elasticsearch.cn/
ELK-API :https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/transport-client.html
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值