美文网首页
ES | ES 查询命令

ES | ES 查询命令

作者: Ada54 | 来源:发表于2021-10-28 16:32 被阅读0次
#查看索引配置信息#
GET /test_index/_settings


#1.数据准备#
PUT user_index
{
  "settings": {
    "number_of_replicas": 0,
    "number_of_shards": 3
  },
  "mappings": {
    "properties": {
      "name": {
        "type": "text"
      },
      "address": {
        "type": "text"
      },
      "age": {
        "type": "integer"
      },
      "interests": {
        "type": "text"
      },
      "birthday": {
        "type": "date"
      }
    }
  }
}

GET /user_index

#2.插入数据#
POST /user_index/_doc/1
{
  "name":"lisi",
  "address":"shandong",
  "age":18,
  "interests":"youyong shufa changge tiaowu",
  "birthday":"2001-01-19"
}

POST /user_index/_doc/2
{
  "name": "wangwu",
  "address": "zhejiang",
  "age": 22,
  "interests": "youyong shufa",
  "birthday": "1997-01-19"
}

POST /user_index/_doc/3
{
  "name": "zhangsan",
  "address": "zhejiang",
  "age": 20,
  "interests": "youyong shufa changge changpao",
  "birthday": "1999-08-29"
}

POST /user_index/_doc/6
{
  "name": "youyong",
  "address": "zhejiang",
  "age": 22,
  "interests": "youyong shufa changge changpao",
  "birthday": "1999-08-29"
}

#精确查询#
GET /user_index/_search?q=name:youyong
GET /user_index/_search?q=name:youyong&sort=age:desc


#term查询和terms查询:term query去倒排索引中找到确切的term,不知道分词器的存在,适合keyword,date,numetic,是包含(contains操作),不是等值(equals)判断#
GET /user_index/_doc/_search
{
  "query":{
    "term":{
      "interests":"changpao"
    }
  }
}

GET /user_index/_doc/_search
{
  "query":{
    "terms":{
      "interests":["youyong","shufa"]
    }
  }
}

#查询控制返回的数量#
GET /user_index/_search
{
  "from": 0, 
  "size": 2,
  "query": {
    "terms": {
      "interests": [
        "shufa",
        "youyong"
      ]
    }
  }
}

#返回版本号,版本号是啥???
GET /user_index/_search
{
  "version": true, 
  "from": 0, 
  "size": 2,
  "query": {
    "terms": {
      "interests": [
        "shufa",
        "youyong"
      ]
    }
  }
}

#match查询,知道分词器的存在,对filed会进行分词操作,再查询
#单字段查询 match
GET user_index/_search
{
  "query": {
    "match": {
      "age": 20
    }
  }
}

GET user_index/_search
{
  "query": {
    "match": {
      "interests": "changge"
    }
  }
}
#指定多字段查询 multi_match
GET user_index/_search
{
  "query": {
    "multi_match": {
      "query": "youyong",
      "fields": ["interests","name"]
    }
  }
}

#短语匹配查询
GET user_index/_search
{
  "query": {
    "match_phrase": {
      "interests": "shufa changge"
    }
  }
}

#查询全部
GET /user_index/_search/
{
  "query": {
    "match_all": {}
  }
}

#指定返回的字段
GET /user_index/_search
{
  "_source": ["name","age"],
  "query":{
    "match": {
      "interests": "changge"
    }
  }
}

#指定需要的字段,去除不要的字段,可使用通配符
GET /user_index/_search
{
  "query": {
    "match_all": {}
  }, 
  "_source": {
    "includes": "addr*",
    "excludes": ["name","dir*"]
  }
}

#排序
#前缀匹配查询,并使用sort排序:desc:降序,asc:升序
GET /user_index/_search
{
  "query": {
    "match_phrase_prefix": {
      "interests": "you"
    }
  },
  "sort": [
    {
      "age": {
        "order": "desc"
      }
    }
  ]
}

# range范围查询,参数 from,to,include_lower是否包含范围左边界,include_upper是否包含范围右边界,boost
GET /user_index/_search
{
  "query": {
    "range": {
      "birthday": {
        "gte": "1990-10-10",
        "lte": "2000-05-01"
      }
    }
  }
}

#wildcard查询,允许通配符 * 和?来查询; * 代表0个或多个字符, ? 代表任意一个字符
GET /user_index/_search
{
  "query": {
    "wildcard": {
      "name": {
        "value": "wang*"
      }
    }
  }
}

# fuzzy 模糊查询,与term查询的模糊等价,是包含(contains)操作,不是等值(equals)操作,不知道分词器的存
GET /user_index/_search 
{
  "query": {
    "fuzzy": {
      "interests": "shuf"
    }
  }
}

# 删除单个:
DELETE /index
curl -XDELETE http://192.169.1.666:9200/index

#你也可以这样删除多个索引:
DELETE /index_one,index_two
curl -XDELETE http://192.169.1.666:9200/index_one,index_two
DELETE /index_*
curl -XDELETE http://192.169.1.666:9200/index_*

相关文章

网友评论

      本文标题:ES | ES 查询命令

      本文链接:https://www.haomeiwen.com/subject/nyjmoltx.html