ES-基础查询

基础命令语句

  • 使用PUT 进行更新时需要将所有属性都抄一遍,否则会被置空
  • 而通过POST /索引名/类型名/文档id/_update 只需填写要修改的属性即可,灵活性更高
method url 描述
PUT localhost:9200/索引名/类型名/文档id 创建文档(指定id)
POST localhost:9200/索引名/类型名 创建文档(随机文档id)
POST localhost:9200/索引名/类型名/文档id/_update 修改文档
DELETE localhost:9200/索引名/类型名/文档id 删除文档
GET localhost:9200/索引名/类型名/文档id 通过id查询文档
POST localhost:9200/索引名/类型名/_search 查询所有数据
// 创建一个空的索引
PUT /test2
{
  "mappings": {
    "properties": {
      "name": {"type": "text"},
      "age": { "type": "integer"},
      "birthday": {"type": "date"}
    }
  }
}
//创建一个索引test1 类型type1 id=1 
PUT /test1/type1/1
{
  // 往索引中插入数据
  "name": "xxxxx",
  "age": 18
}
// 获取索引信息
GET test1
复制代码

查询

简单查询

GET test1/type1/_search
{
  // 查询的参数体
  "query":{
    "match": { //查询的结果为模糊匹配
      "name": "遇见" //查询的条件
    }
  }
}
复制代码

结果:

{
  "took" : 901,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {// 查询的结果都放在hits里了
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 0.26706278,
    "hits" : [
      {
        "_index" : "test1",
        "_type" : "type1",
        "_id" : "2",
        "_score" : 0.26706278, //匹配度越大,数值越大
        "_source" : {
          "name" : "遇见_line",
          "age" : 20
        }
      },
      {
        "_index" : "test1",
        "_type" : "type1",
        "_id" : "3",
        "_score" : 0.26706278,
        "_source" : {
          "name" : "遇见_line3",
          "age" : 20
        }
      },
      {
        "_index" : "test1",
        "_type" : "type1",
        "_id" : "1",
        "_score" : 0.26706278,
        "_source" : {
          "name" : "遇见_line1",
          "age" : 20
        }
      }
    ]
  }
}
复制代码

结果过滤

在原本的查询中添加 _source即可进行字段的筛选

GET test1/type1/_search
{
  "query":{
    "match": {
      "name": "遇见"
    }
  },
  "_source" : ["name"]
}
复制代码

结果:

"hits" : [
      {
        "_index" : "test1",
        "_type" : "type1",
        "_id" : "2",
        "_score" : 0.26706278,
        "_source" : {
          "name" : "遇见_line"
          // age 没有了
        }
      }
复制代码

结果排序

通过sort 来排序

GET test1/type1/_search
{
  "query":{
    "match": {
      "name": "遇见"
    }
  },
"_source" : ["name"],
  "sort":[
    {
      "age":{
        "order": "desc" //"asc" 升序, desc降序
      }
    }
    ]
  
}
复制代码
"hits" : [
      {
        "_index" : "test1",
        "_type" : "type1",
        "_id" : "3",
        "_score" : null,
        "_source" : {
          "name" : "遇见_line3"
        },
        "sort" : [
          21
        ]
      },
      {
        "_index" : "test1",
        "_type" : "type1",
        "_id" : "2",
        "_score" : null,
        "_source" : {
          "name" : "遇见_line2"
        },
        "sort" : [
          20
        ]
      },
      {
        "_index" : "test1",
        "_type" : "type1",
        "_id" : "1",
        "_score" : null,
        "_source" : {
          "name" : "遇见_line1"
        },
        "sort" : [
          19
        ]
      }
    ]
  }
复制代码

分页查询

GET test1/type1/_search
{
  "query":{
    "match": {
      "name": "遇见"
    }
  },
"_source" : ["name"],
  "sort":[
    {
      "age":{
        "order": "desc"
      }
    }
    ],
  "from":0, //从第0条数据开始 ,数据下标从0开始
  "size":2  // 每页显示2条数据
}
复制代码

多条件查询 | Bool 查询

must(and) 所有条件都必须符合

查询名字包含遇见且年龄为19的

GET test1/type1/_search
{
  "query":{
     "bool": {
      "must": [
        {
          "match": {
            "name": "遇见"
          }
        },
        {
          "match": {
            "age": 19
          }
        }
      ]
    }
  }
}
复制代码

should (or)

查询年龄20 or 19的人

GET test1/type1/_search
{
  "query":{
     "bool": {
      "should": [
        {
          "match": {
            "age": "20"
          }
        },
        {
          "match": {
            "age": 19
          }
        }
      ]
    }
  }
}
复制代码

msut_not 等价于not

查询既不是19也不是20 岁的人

GET test1/type1/_search
{
  "query":{
     "bool": {
      "must_not": [
        {
          "match": {
            "age": "20"
          }
        },
        {
          "match": {
            "age": 19
          }
        }
      ]
    }
  }
}
复制代码

过滤器 filter

查询年龄在【10,20】,且名称包含遇见的人

GET test1/type1/_search
{
  "query":{
     "bool": {
      "must": [
        {
          "match": {
            "name": "遇见"
          }
        }
      ],
      "filter": [
        {
          "range": {
            "age": {
              "gte": 10, //gt> gte>=
              "lte": 20
            }
          }
        }
      ]
    }
  }
}
复制代码

多匹配查询 tags

查询标签中包含男or 技术的人

GET test1/type1/_search
{
  "query":{
    "match": {
      "tags": "男 技术" // 空格表示or
    }
  }
复制代码

精准查询term

通过倒排索引进行查询

原始数据 倒排索引
博客id 标签 标签 博客id
1 windows windows 1,2,3
2 windows linux 3,4
3 windows,linux
4 liunx
  • term 直接精准匹配
  • match 使用分词器(先分析文档,在查找)
  • text :会被分词器拆分
  • keyword 被当成一个整体不会被分词器拆分
GET test1/type1/_search
{
  "query":{
    "term": {
      "name": "遇见_line1"
    }
  }
}
复制代码

高亮查询

GET test1/type1/_search
{
  "query":{
    "match": {
      "name": "遇见"
    }
  },
  // 选择name字段高亮
  "highlight" : {
    "fields":{
      "name":{}
    }
  }
}
复制代码
"hits" : [
      {
        "_index" : "test1",
        "_type" : "type1",
        "_id" : "3",
        "_score" : 0.17402273,
        "_source" : {
          "name" : "遇见_line3",
          "age" : 21,
          "tags" : [
            "直男",
            "技术宅",
            "内向"
          ]
        },
        "highlight" : {
          "name" : [
          // 被em标签包裹的就是高亮部分
            "<em>遇</em><em>见</em>_line3"
          ]
        }
      },
复制代码

我们也可以自定义高亮标签

GET test1/type1/_search
{
  "query":{
    "match": {
      "name": "遇见"
    }
  },
  "highlight" : {
    "pre_tags": "<p color='red'>",
    "post_tags": "</p>", 
    "fields":{
      "name":{}
    }
  }
}
复制代码
"highlight" : {
          "name" : [
            "<p color='red'>遇</p><p color='red'>见</p>_line3"
          ]
        }
      }
复制代码
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享