elasticSearch的CURD操作

111 阅读12分钟

crud

所有的查询可以用get也可以用post,在代码中选择用post,因为post可以传递请求体

创建一篇文档

  • 用post请求或者put请求都可以
  • put请求创建必须指定id
  • 而post请求可以不用指定id,让es自动生成一个id
# 创建一篇文档
POST index/_doc/1000
{
  "name":"zhangsan",
  "age":22
}

PUT index1/type/1
{
  "name":"王五",
  "age":18
}

PUT index1/type/6
{
  "name":"小王的二姨妈",
  "age":202
}

PUT index1/type/2
{
  "name":"王五的姨夫",
  "age":20
}
{
  "_index" : "index1",
  "_type" : "type",
  "_id" : "1",
  "_version" : 5,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 11,
  "_primary_term" : 1
}

查询所有的索引

GET _cat/indices?v
health status index                   uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   index1                  Jl9y6uUoToy5GI-LeYrveA   1   1          3            0     34.3kb         34.3kb
yellow open   index2                  Q10LQ68cR8qBVpl9EF517Q   1   1          2            0      7.3kb          7.3kb
green  open   kibana_sample_data_logs tiLn1An8TAOq7UmLq8nffw   1   0      14074            0     11.4mb         11.4mb
green  open   .kibana_1               _8HstzxARXGUfA8BmfBvwA   1   0         81           11       80kb           80kb

查询索引的相关信息

GET kibana_sample_data_logs
{
  "kibana_sample_data_logs" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "@timestamp" : {
          "type" : "alias",
          "path" : "timestamp"
        },
        "agent" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "bytes" : {
          "type" : "long"
        },
        "clientip" : {
          "type" : "ip"
        },
        "event" : {
          "properties" : {
            "dataset" : {
              "type" : "keyword"
            }
          }
        },
        "extension" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "geo" : {
          "properties" : {
            "coordinates" : {
              "type" : "geo_point"
            },
            "dest" : {
              "type" : "keyword"
            },
            "src" : {
              "type" : "keyword"
            },
            "srcdest" : {
              "type" : "keyword"
            }
          }
        },
        "host" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "index" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "ip" : {
          "type" : "ip"
        },
        "machine" : {
          "properties" : {
            "os" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "ram" : {
              "type" : "long"
            }
          }
        },
        "memory" : {
          "type" : "double"
        },
        "message" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "phpmemory" : {
          "type" : "long"
        },
        "referer" : {
          "type" : "keyword"
        },
        "request" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "response" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "tags" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "timestamp" : {
          "type" : "date"
        },
        "url" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "utc_time" : {
          "type" : "date"
        }
      }
    },
    "settings" : {
      "index" : {
        "number_of_shards" : "1",
        "auto_expand_replicas" : "0-1",
        "provided_name" : "kibana_sample_data_logs",
        "creation_date" : "1597920229601",
        "number_of_replicas" : "0",
        "uuid" : "tiLn1An8TAOq7UmLq8nffw",
        "version" : {
          "created" : "7080199"
        }
      }
    }
  }
}

查询文档信息

GET index1/_doc/1
{
  "_index" : "index1",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 5,
  "_seq_no" : 11,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "王五",
    "age" : 18
  }
}

查询该分类下的所有文档

  • 在7.0之后,es建议只能一个索引只有一个类型_doc了,所以如果你的类型为_doc的话,就直接下面这种搜索就ok
GET index/_search
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 5,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "index",
        "_type" : "type",
        "_id" : "6",
        "_score" : 1.0,
        "_source" : {
          "name" : "小王的二姨妈",
          "age" : 202
        }
      },
      {
        "_index" : "index",
        "_type" : "type",
        "_id" : "4R7gEXQBVFvQUqGOeucN",
        "_score" : 1.0,
        "_source" : {
          "name" : "zhangsan",
          "age" : 22
        }
      },
      {
        "_index" : "index",
        "_type" : "type",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "name" : "zhangsan",
          "age" : 22
        }
      },
      {
        "_index" : "index",
        "_type" : "type",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "zhangsan",
          "age" : 22
        }
      },
      {
        "_index" : "index",
        "_type" : "type",
        "_id" : "1000",
        "_score" : 1.0,
        "_source" : {
          "name" : "zhangsan",
          "age" : 22
        }
      }
    ]
  }
}

  • 同样,你也可以用老版本的搜索所有进行搜索
GET index1/type/_search
#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "index1",
        "_type" : "type",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "name" : "王五的姨夫",
          "age" : 20
        }
      },
      {
        "_index" : "index1",
        "_type" : "type",
        "_id" : "6",
        "_score" : 1.0,
        "_source" : {
          "name" : "小王的二姨妈",
          "age" : 202
        }
      },
      {
        "_index" : "index1",
        "_type" : "type",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "王五",
          "age" : 18
        }
      }
    ]
  }
}

删除指定文档

image.png

DELETE index1/type/doc1
{
  "_index" : "index1",
  "_type" : "type",
  "_id" : "doc1",
  "_version" : 5,
  "result" : "deleted",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 16,
  "_primary_term" : 1
}

部分修改指定文档

POST zhifou/doc/1/_update
{
  "doc": {
    "desc": "皮肤很黄,武器很长,性格很直",
    "tags": ["很黄","很长", "很直"]
  }
}
{
  "_index" : "zhifou",
  "_type" : "doc",
  "_id" : "1",
  "_version" : 4,
  "result" : "noop",
  "_shards" : {
    "total" : 0,
    "successful" : 0,
    "failed" : 0
  },
  "_seq_no" : 7,
  "_primary_term" : 1
}

验证分词器安装是否成功

GET _analyze
{
  "analyzer": "ik_max_word",
  "text": "上海自来水来自海上"
}
{
  "tokens" : [
    {
      "token" : "上海",
      "start_offset" : 0,
      "end_offset" : 2,
      "type" : "CN_WORD",
      "position" : 0
    },
    {
      "token" : "自来水",
      "start_offset" : 2,
      "end_offset" : 5,
      "type" : "CN_WORD",
      "position" : 1
    },
    {
      "token" : "自来",
      "start_offset" : 2,
      "end_offset" : 4,
      "type" : "CN_WORD",
      "position" : 2
    },
    {
      "token" : "水",
      "start_offset" : 4,
      "end_offset" : 5,
      "type" : "CN_CHAR",
      "position" : 3
    },
    {
      "token" : "来自",
      "start_offset" : 5,
      "end_offset" : 7,
      "type" : "CN_WORD",
      "position" : 4
    },
    {
      "token" : "海上",
      "start_offset" : 7,
      "end_offset" : 9,
      "type" : "CN_WORD",
      "position" : 5
    }
  ]
}

ik分词器目录:

  • IKAnalyzer.cfg.xml,用来配置自定义的词库
  • main.dic,ik原生内置的中文词库,大约有27万多条,只要是这些单词,都会被分在一起。
  • surname.dic,中国的姓氏。
  • suffix.dic,特殊(后缀)名词,例如乡、江、所、省等等。
  • preposition.dic,中文介词,例如不、也、了、仍等等。
  • stopword.dic,英文停用词库,例如a、an、and、the等。
  • quantifier.dic,单位名词,如厘米、件、倍、像素等。
ik_smart:该参数将文档作最粗粒度的拆分。

ik_max_word:将文档作最细粒度的拆分

查询字符串

GET zhifou/doc/_search?q=from:gu
#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 0.6103343,
    "hits" : [
      {
        "_index" : "zhifou",
        "_type" : "doc",
        "_id" : "3",
        "_score" : 0.6103343,
        "_source" : {
          "name" : "龙套偏房",
          "age" : 22,
          "from" : "gu",
          "desc" : "mmp,没怎么看,不知道怎么形容",
          "tags" : [
            "造数据",
            "真",
            "难"
          ]
        }
      },
      {
        "_index" : "zhifou",
        "_type" : "doc",
        "_id" : "4",
        "_score" : 0.6103343,
        "_source" : {
          "name" : "石头",
          "age" : 29,
          "from" : "gu",
          "desc" : "粗中有细,狐假虎威",
          "tags" : [
            "粗",
            "大",
            "猛"
          ]
        }
      },
      {
        "_index" : "zhifou",
        "_type" : "doc",
        "_id" : "1",
        "_score" : 0.6103343,
        "_source" : {
          "name" : "顾老二",
          "age" : 30,
          "from" : "gu",
          "desc" : "皮肤很黄,武器很长,性格很直",
          "tags" : [
            "很黄",
            "很长",
            "很直"
          ]
        }
      }
    ]
  }
}

包含查询

会将desc给的短语进行分词,再进行搜索

  • match查询的关键字会被分词
GET zhifou/doc/_search
{
  "query":{
    "match":{
      "desc":"很长"
    }
  }
}
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 3.7195964,
    "hits" : [
      {
        "_index" : "zhifou",
        "_type" : "doc",
        "_id" : "1",
        "_score" : 3.7195964,
        "_source" : {
          "name" : "顾老二",
          "age" : 30,
          "from" : "gu",
          "desc" : "皮肤很黄,武器很长,性格很直",
          "tags" : [
            "很黄",
            "很长",
            "很直"
          ]
        }
      }
    ]
  }
}

短语查询

短语查询不会对查询的desc字段进行分词,会直接使用它进行查询

  • match_phrase查询的关键字会被分词,但是必须要连续
# 短语查询
GET zhifou/doc/_search
{
  "query":{
    "match_phrase": {
      "desc": "怎么形容"
    }
  }
}
{
  "took" : 10,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 5.889361,
    "hits" : [
      {
        "_index" : "zhifou",
        "_type" : "doc",
        "_id" : "3",
        "_score" : 5.889361,
        "_source" : {
          "name" : "龙套偏房",
          "age" : 22,
          "from" : "gu",
          "desc" : "mmp,没怎么看,不知道怎么形容",
          "tags" : [
            "造数据",
            "真",
            "难"
          ]
        }
      }
    ]
  }
}

短语间隔查询

GET zhifou/doc/_search
{
  "query":{
    "match_phrase": {
      "desc": {
        //等于没.*?看
        "query":"没看", 
        "slop": 2 
      }
    }
  }
}
{
  "took" : 8,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.180717,
    "hits" : [
      {
        "_index" : "zhifou",
        "_type" : "doc",
        "_id" : "3",
        "_score" : 1.180717,
        "_source" : {
          "name" : "龙套偏房",
          "age" : 22,
          "from" : "gu",
          "desc" : "mmp,没怎么看,不知道怎么形容",
          "tags" : [
            "造数据",
            "真",
            "难"
          ]
        }
      }
    ]
  }
}

单词最左前缀

GET zhifou/doc/_search
{
  "query":{
    "match_phrase_prefix": {
      "desc": {
        "query": "肤白貌美",
        "max_expansions": 1
      }
    }
  }
}
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 6.1046467,
    "hits" : [
      {
        "_index" : "zhifou",
        "_type" : "doc",
        "_id" : "2",
        "_score" : 6.1046467,
        "_source" : {
          "name" : "大娘子",
          "age" : 18,
          "from" : "sheng",
          "desc" : "肤白貌美,娇憨可爱",
          "tags" : [
            "白",
            "富",
            "美"
          ]
        }
      }
    ]
  }
}

多字段包含查询

# 多字段查询
GET zhifou/doc/_search
{
  "query":{
    "multi_match": {
      "query": "白",
      "fields": ["desc", "name"]
    }
  }
}
#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.6599495,
    "hits" : [
      {
        "_index" : "zhifou",
        "_type" : "doc",
        "_id" : "2",
        "_score" : 1.6599495,
        "_source" : {
          "name" : "大娘子",
          "age" : 18,
          "from" : "sheng",
          "desc" : "肤白貌美,娇憨可爱",
          "tags" : [
            "白",
            "富",
            "美"
          ]
        }
      }
    ]
  }
}

精确等于查询term

  • term查询的关键字不会被分词
# 精确查询
GET zhifou/doc/_search
{
  "query":{
    "term": {
      "name": "大"
    }
  }
}

使用terms也可以使用多个关键字查询

GET w10/doc/_search
{
  "query": {
    "terms": {
      "t1": ["beautiful", "sexy"]
    }
  }
}
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.3862942,
    "hits" : [
      {
        "_index" : "zhifou",
        "_type" : "doc",
        "_id" : "2",
        "_score" : 1.3862942,
        "_source" : {
          // 查询name时刻,会先对name进行分词,然后在和自己的查询语句给出的条件进行匹配
          "name" : "大娘子",
          "age" : 18,
          "from" : "sheng",
          "desc" : "肤白貌美,娇憨可爱",
          "tags" : [
            "白",
            "富",
            "美"
          ]
        }
      }
    ]
  }
}

排序

es只能对下列两种类型字段进行排序:

  • 数字
  • 日期
# 排序
GET zhifou/doc/_search
{
  "query":{
    "match": {
      "from": "gu"
    }
  },
  "sort":{
    "age":{
      // 升序asc 降序desc
      "order":"asc"
    }
  }
}
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "zhifou",
        "_type" : "doc",
        "_id" : "3",
        "_score" : null,
        "_source" : {
          "name" : "龙套偏房",
          "age" : 22,
          "from" : "gu",
          "desc" : "mmp,没怎么看,不知道怎么形容",
          "tags" : [
            "造数据",
            "真",
            "难"
          ]
        },
        "sort" : [
          22
        ]
      },
      {
        "_index" : "zhifou",
        "_type" : "doc",
        "_id" : "4",
        "_score" : null,
        "_source" : {
          "name" : "石头",
          "age" : 29,
          "from" : "gu",
          "desc" : "粗中有细,狐假虎威",
          "tags" : [
            "粗",
            "大",
            "猛"
          ]
        },
        "sort" : [
          29
        ]
      },
      {
        "_index" : "zhifou",
        "_type" : "doc",
        "_id" : "1",
        "_score" : null,
        "_source" : {
          "name" : "顾老二",
          "age" : 30,
          "from" : "gu",
          "desc" : "皮肤黑、武器长、性格直",
          "tags" : [
            "黑",
            "长",
            "直"
          ]
        },
        "sort" : [
          30
        ]
      }
    ]
  }
}

分页

  • from:从哪开始查 == offset
  • size:返回几条结果 == limit
# 分页
GET zhifou/doc/_search
{
  "query":{
    "match": {
      "from": "gu"
    }
  },
  "sort":{
    "age":{
      "order":"asc"
    }
  },
  "from":0,
  "size":2
}
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "zhifou",
        "_type" : "doc",
        "_id" : "3",
        "_score" : null,
        "_source" : {
          "name" : "龙套偏房",
          "age" : 22,
          "from" : "gu",
          "desc" : "mmp,没怎么看,不知道怎么形容",
          "tags" : [
            "造数据",
            "真",
            "难"
          ]
        },
        "sort" : [
          22
        ]
      },
      {
        "_index" : "zhifou",
        "_type" : "doc",
        "_id" : "4",
        "_score" : null,
        "_source" : {
          "name" : "石头",
          "age" : 29,
          "from" : "gu",
          "desc" : "粗中有细,狐假虎威",
          "tags" : [
            "粗",
            "大",
            "猛"
          ]
        },
        "sort" : [
          29
        ]
      }
    ]
  }
}

image.png

布尔查询:

must查询:

GET zhifou/doc/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "房"
          }
        },
        {
          "match": {
            "age": 22
          }
        }
      ]
    }
  }
}
{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 2.2199392,
    "hits" : [
      {
        "_index" : "zhifou",
        "_type" : "doc",
        "_id" : "3",
        "_score" : 2.2199392,
        "_source" : {
          "name" : "龙套偏房",
          "age" : 22,
          "from" : "gu",
          "desc" : "mmp,没怎么看,不知道怎么形容",
          "tags" : [
            "造数据",
            "真",
            "难"
          ]
        }
      }
    ]
  }
}

should查询:

# should查询
GET zhifou/doc/_search
{
  "query":{
    "bool": {
      "should": [
        {
          "match": {
            "age": 30
          }
        },
        {
          "match": {
            "name": "龙套"
          }
        }
      ]
    }
  }
}

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 2.4398782,
    "hits" : [
      {
        "_index" : "zhifou",
        "_type" : "doc",
        "_id" : "3",
        "_score" : 2.4398782,
        "_source" : {
          "name" : "龙套偏房",
          "age" : 22,
          "from" : "gu",
          "desc" : "mmp,没怎么看,不知道怎么形容",
          "tags" : [
            "造数据",
            "真",
            "难"
          ]
        }
      },
      {
        "_index" : "zhifou",
        "_type" : "doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "顾老二",
          "age" : 30,
          "from" : "gu",
          "desc" : "皮肤黑、武器长、性格直",
          "tags" : [
            "黑",
            "长",
            "直"
          ]
        }
      }
    ]
  }
}

must_not查询:

# must_not查询
GET zhifou/doc/_search
{
  "query":{
    "bool":{
      "must_not": [
        {
          "match": {
            "age": "30"
          }
        },
        {
          "match": {
            "name": "龙套"
          }
        }
      ]
    }
}
{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 0.0,
    "hits" : [
      {
        "_index" : "zhifou",
        "_type" : "doc",
        "_id" : "2",
        "_score" : 0.0,
        "_source" : {
          "name" : "大娘子",
          "age" : 18,
          "from" : "sheng",
          "desc" : "肤白貌美,娇憨可爱",
          "tags" : [
            "白",
            "富",
            "美"
          ]
        }
      },
      {
        "_index" : "zhifou",
        "_type" : "doc",
        "_id" : "4",
        "_score" : 0.0,
        "_source" : {
          "name" : "石头",
          "age" : 29,
          "from" : "gu",
          "desc" : "粗中有细,狐假虎威",
          "tags" : [
            "粗",
            "大",
            "猛"
          ]
        }
      },
      {
        "_index" : "zhifou",
        "_type" : "doc",
        "_id" : "5",
        "_score" : 0.0,
        "_source" : {
          "name" : "魏行首",
          "age" : 25,
          "from" : "广云台",
          "desc" : "仿佛兮若轻云之蔽月,飘飘兮若流风之回雪,mmp,最后竟然没有嫁给顾老二!",
          "tags" : [
            "闭月",
            "羞花"
          ]
        }
      }
    ]
  }
}

filter查询:

image.png
bool查询中有了filter查询就不要再有should查询,否则should查询的结果会不准确

#filter查询
GET zhifou/doc/_search
{
  "query":{
    "bool":{
      "filter": [
        {
          "range": {
            "age": {
              "gte": 23,
              "lte": 60
            }
          }
        }
      ],
      "must": [
        {
          "match": {
            "name": "行首"
          }
        }
      ]
    }
  }
}

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 2.7725885,
    "hits" : [
      {
        "_index" : "zhifou",
        "_type" : "doc",
        "_id" : "5",
        "_score" : 2.7725885,
        "_source" : {
          "name" : "魏行首",
          "age" : 25,
          "from" : "广云台",
          "desc" : "仿佛兮若轻云之蔽月,飘飘兮若流风之回雪,mmp,最后竟然没有嫁给顾老二!",
          "tags" : [
            "闭月",
            "羞花"
          ]
        }
      }
    ]
  }
}

结果过滤_source:

主要指定返回字段

# _source过滤
GET zhifou/doc/_search
{
  "query":{
    "bool": {
      "must":[
        {
          "match": {
            "from": "gu"
          }
        }
      ],
      "filter": [
        {
          "range":{
            "age":{
            "gt":25
            }
          }
        }
      ]
    }
  },
  "_source":["name","age"]
}

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.6103343,
    "hits" : [
      {
        "_index" : "zhifou",
        "_type" : "doc",
        "_id" : "1",
        "_score" : 0.6103343,
        "_source" : {
          "name" : "顾老二",
          "age" : 30
        }
      },
      {
        "_index" : "zhifou",
        "_type" : "doc",
        "_id" : "4",
        "_score" : 0.6103343,
        "_source" : {
          "name" : "石头",
          "age" : 29
        }
      }
    ]
  }
}

高亮查询

有三种高亮的语法:

  • plain highlight:使用standard Lucene highlighter,对简单的查询支持度非常好。
  • unified highlight:默认的高亮语法,使用Lucene Unified Highlighter,将文本切分成句子,并对句子使用BM25计算词条的score,支持精准查询和模糊查询。
  • fast vector highlighter:使用Lucene Fast Vector highlighter,功能很强大,如果在mapping中对field开启了term_vector,并设置了with_positions_offsets,就会使用该highlighter,对内容特别长的文本(大于1MB)有性能上的优势。
GET zhifou/doc/_search
{
  "query":{
    "bool":{
      "should":[
        {
          "match":{
            "name":"顾老二"
          }
        },
        {
          "match": {
            "age": 18
          }
        }
      ]
    }
  },
  "highlight":{
    "pre_tags": "<b class='key' style='color:red'>",
    "post_tags": "</b>",
    "fields": {
      "name":{
      	"type":"plain"
      },
      "from":{},
      "age":{}
    }
  }
}
{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : 4.769217,
    "hits" : [
      {
        "_index" : "zhifou",
        "_type" : "doc",
        "_id" : "1",
        "_score" : 4.769217,
        "_source" : {
          "name" : "顾老二",
          "age" : 30,
          "from" : "gu",
          "desc" : "皮肤黑、武器长、性格直",
          "tags" : [
            "黑",
            "长",
            "直"
          ]
        },
        "highlight" : {
          "name" : [
            "<b class='key' style='color:red'>顾</b><b class='key' style='color:red'>老</b><b class='key' style='color:red'>二</b>"
          ],
          "from" : [
            "<b class='key' style='color:red'>gu</b>"
          ]
        }
      },
      {
        "_index" : "zhifou",
        "_type" : "doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "name" : "大娘子",
          "age" : 18,
          "from" : "sheng",
          "desc" : "肤白貌美,娇憨可爱",
          "tags" : [
            "白",
            "富",
            "美"
          ]
        }
      },
      {
        "_index" : "zhifou",
        "_type" : "doc",
        "_id" : "3",
        "_score" : 0.6103343,
        "_source" : {
          "name" : "龙套偏房",
          "age" : 22,
          "from" : "gu",
          "desc" : "mmp,没怎么看,不知道怎么形容",
          "tags" : [
            "造数据",
            "真",
            "难"
          ]
        },
        "highlight" : {
          "from" : [
            "<b class='key' style='color:red'>gu</b>"
          ]
        }
      },
      {
        "_index" : "zhifou",
        "_type" : "doc",
        "_id" : "4",
        "_score" : 0.6103343,
        "_source" : {
          "name" : "石头",
          "age" : 29,
          "from" : "gu",
          "desc" : "粗中有细,狐假虎威",
          "tags" : [
            "粗",
            "大",
            "猛"
          ]
        },
        "highlight" : {
          "from" : [
            "<b class='key' style='color:red'>gu</b>"
          ]
        }
      }
    ]
  }
}

聚合分组操作

1.avg

# 聚合操作
POST zhifou/doc/_search
{
  "size":0,
  "aggs":{
    "age_avg":{
      "avg": {
        "field": "age"
      }
    }
  }
}
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 5,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "age_avg" : {
      "value" : 24.8
    }
  }
}

2.max,sum,min同理

# 聚合操作
POST zhifou/doc/_search
{
  "query":{
    "match": {
      "from": "gu"
    }
  },
  "size":0,
  "aggs":{
    "age_max":{
      "max": {
        "field": "age"
      }
    }
  }
}
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "age_max" : {
      "value" : 30.0
    }
  }
}

3.聚合

# 分组
POST zhifou/doc/_search
{
  "size":0,
  "aggs":{
    "cus_group":{
      "range": {
        "field": "age",
        "ranges": [
          {
            "from": 0,
            "to": 15
          },
          {
            "from":15,
            "to":25
          },
          {
            "from":25,
            "to":35
          }
        ]
      }
    }
  }
}
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 5,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "cus_group" : {
      "buckets" : [
        {
          "key" : "0.0-15.0",
          "from" : 0.0,
          "to" : 15.0,
          "doc_count" : 0
        },
        {
          "key" : "15.0-25.0",
          "from" : 15.0,
          "to" : 25.0,
          "doc_count" : 2
        },
        {
          "key" : "25.0-35.0",
          "from" : 25.0,
          "to" : 35.0,
          "doc_count" : 3
        }
      ]
    }
  }
}

4.报错illegal_argument_exception,

  • 第一种解决方法,查询field加上.keyword
  • 第二种解决方法,自定义建立索引的规则,不使用默认值创建索引
# 分组
POST zhifou/doc/_search
{
  "size":0,
  "aggs":{
    "cus_group":{
      "terms": {
        "field": "from.keyword"
      }
    }
  }
}
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 5,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "cus_group" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "gu",
          "doc_count" : 3
        },
        {
          "key" : "sheng",
          "doc_count" : 1
        },
        {
          "key" : "广云台",
          "doc_count" : 1
        }
      ]
    }
  }
}


5.分组聚合查询

# 分组聚合查询
GET zhifou/doc/_search
{
  "size":0,
  "aggs":{
    "group":{
      "terms": {
        "field": "from.keyword"
      },
      "aggs": {
        "from_max": {
          "max": {
            "field": "age"
          }
        }
      }
    }
  }
}

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 5,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "group" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "gu",
          "doc_count" : 3,
          "from_max" : {
            "value" : 30.0
          }
        },
        {
          "key" : "sheng",
          "doc_count" : 1,
          "from_max" : {
            "value" : 18.0
          }
        },
        {
          "key" : "广云台",
          "doc_count" : 1,
          "from_max" : {
            "value" : 25.0
          }
        }
      ]
    }
  }
}

结果去除元数据

  • 必须指定id,后面跟上_source
GET index/type/1/_source
{
  "name" : "zhangsan",
  "age" : 22
}

查询数据是否存在

  • 必须要有id,仅仅只能判断单个文档是否存在
# 查询数据是否存在
HEAD zhifou/_doc/1
// 仅仅返回状态码,200即是存在,404为不存在
200 - OK

exists查询包含该字段的结果

GET t1/_search
{
  "query": {
    "exists": {
      "field": "first name"
    }
  }
}
  • 返回包含该字段的结果集
{
  "took" : 361,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "t1",
        "_type" : "_doc",
        "_id" : "hL4ZG3QBysPpbvX9DFDa",
        "_score" : 1.0,
        "_source" : {
          "first name" : "zhnsa",
          "last name" : "sadas"
        }
      }
    ]
  }
}

批量操作

image.png

1.批量查询

  • 查询不到的,元数据会返回found:false
# 批量查询
POST zhifou/_doc/_mget
{
  "ids":[1,2,3000]
}
{
  "docs" : [
    {
      "_index" : "zhifou",
      "_type" : "_doc",
      "_id" : "1",
      "_version" : 1,
      "_seq_no" : 0,
      "_primary_term" : 1,
      "found" : true,
      "_source" : {
        "name" : "顾老二",
        "age" : 30,
        "from" : "gu",
        "desc" : "皮肤黑、武器长、性格直",
        "tags" : [
          "黑",
          "长",
          "直"
        ]
      }
    },
    {
      "_index" : "zhifou",
      "_type" : "_doc",
      "_id" : "2",
      "_version" : 1,
      "_seq_no" : 1,
      "_primary_term" : 1,
      "found" : true,
      "_source" : {
        "name" : "大娘子",
        "age" : 18,
        "from" : "sheng",
        "desc" : "肤白貌美,娇憨可爱",
        "tags" : [
          "白",
          "富",
          "美"
        ]
      }
    },
    {
      "_index" : "zhifou",
      "_type" : "_doc",
      "_id" : "3000",
      "found" : false
    }
  ]
}

2.批量新增

# 批量插入
POST zhifou/doc/_bulk
{"create":{"_index":"zhifou","_type":"doc","_id":40}}
{"name":"王插入","age":45,"from":"wang","desc":"王家老大","tags":["土","老"]}
{"create":{"_index":"zhifou","_type":"doc","_id":41}}
{"name":"王插婴","age":3,"from":"wang","desc":"王家小皇帝","tags":["新","小"]}
{"create":{"_index":"zhifou","_type":"doc","_id":42}}
{"name":"王插小","age":15,"from":"wang","desc":"王家少爷","tags":["叛逆","少年"]}
{"create":{"_index":"zhifou","_type":"doc","_id":43}}
{"name":"王插大","age":35,"from":"wang","desc":"王家奴隶","tags":["时尚","壮年"]}
// 最后要以\n作为结尾
{
  "took" : 19,
  "errors" : false,
  "items" : [
    {
      "create" : {
        "_index" : "zhifou",
        "_type" : "doc",
        "_id" : "40",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 9,
        "_primary_term" : 3,
        "status" : 201
      }
    },
    {
      "create" : {
        "_index" : "zhifou",
        "_type" : "doc",
        "_id" : "41",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 10,
        "_primary_term" : 3,
        "status" : 201
      }
    },
    {
      "create" : {
        "_index" : "zhifou",
        "_type" : "doc",
        "_id" : "42",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 11,
        "_primary_term" : 3,
        "status" : 201
      }
    },
    {
      "create" : {
        "_index" : "zhifou",
        "_type" : "doc",
        "_id" : "43",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 12,
        "_primary_term" : 3,
        "status" : 201
      }
    }
  ]
}

3.批量删除

# 批量删除
POST zhifou/doc/_bulk
{"delete":{"_index":"zhifou","_type":"doc","_id":30}}
{"delete":{"_index":"zhifou","_type":"doc","_id":31}}
{"delete":{"_index":"zhifou","_type":"doc","_id":32}}
{"delete":{"_index":"zhifou","_type":"doc","_id":33}}
// 最后要以\n作为结尾
{
  "took" : 24,
  "errors" : false,
  "items" : [
    {
      "delete" : {
        "_index" : "zhifou",
        "_type" : "doc",
        "_id" : "30",
        "_version" : 2,
        "result" : "deleted",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 13,
        "_primary_term" : 3,
        "status" : 200
      }
    },
    {
      "delete" : {
        "_index" : "zhifou",
        "_type" : "doc",
        "_id" : "31",
        "_version" : 2,
        "result" : "deleted",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 14,
        "_primary_term" : 3,
        "status" : 200
      }
    },
    {
      "delete" : {
        "_index" : "zhifou",
        "_type" : "doc",
        "_id" : "32",
        "_version" : 2,
        "result" : "deleted",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 15,
        "_primary_term" : 3,
        "status" : 200
      }
    },
    {
      "delete" : {
        "_index" : "zhifou",
        "_type" : "doc",
        "_id" : "33",
        "_version" : 2,
        "result" : "deleted",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 16,
        "_primary_term" : 3,
        "status" : 200
      }
    }
  ]
}