Elastic Search:(四)搜索进阶

目录

1 批量导入数据

1.1 目的

1.2 bulk指令

1.3 实操

2 term多种查询

2.1 exists 查询

2.2 prefix 查询

2.3 wildcard查询

3 范围查询

3.1 基本内容

3.2 简单查询

3.3 时间查询


1 批量导入数据

1.1 目的

在进行数据搜索之前,需要进行数据的创建,本文以多名歌手的基本信息作为搜索测试数据,采用bulk指令进行数据的批量导入,方便后续的数据搜索。

1.2 bulk指令

bulk为批量导入数据指令,采用PUT请求,该方式导入数据十分方便,无需建立索引、映射等复杂操作,将数据按照特定格式(指定索引名称、映射名称等)导入后,会自动创建索引、映射等。

1.3 实操

根据上述内容,导入数名歌手基本信息。发送PUT请求,_bulk指令进行批量数据导入。

PUT _bulk
{"index":{"_index":"singers","_type":"_doc","_id":"1"}}
{"name":"许嵩","neck":"Vae","No":"1","age":"35","sex":"男","brith":"1986/5/14","address":"安徽省合肥市"}
{"index":{"_index":"singers","_type":"_doc","_id":"2"}}
{"name":"周杰伦","neck":"Jay","No":"2","age":"42","sex":"男","brith":"1979/1/18","address":"台湾省新北市"}
{"index":{"_index":"singers","_type":"_doc","_id":"3"}}
{"name":"徐良","neck":"","No":"3","age":"34","sex":"男","brith":"1987/2/8","address":"山东省青岛市"}
{"index":{"_index":"singers","_type":"_doc","_id":"4"}}
{"name":"汪苏泷","neck":"Silence","No":"4","age":"32","sex":"男","brith":"1989/9/17","address":"辽宁省沈阳市"}
{"index":{"_index":"singers","_type":"_doc","_id":"5"}}
{"name":"韩红","No":"5","age":"50","sex":"女","brith":"1971/8/26","address":"西藏自治区昌都市"}
{"index":{"_index":"singers","_type":"_doc","_id":"6"}}
{"name":"汪峰","No":"6","age":"50","sex":"男","brith":"1971/6/29","address":"北京市"}
}

2 term多种查询

之前提到过term词条查询并进行了简单的学习和理解,本文对term的一些其他用法进行讨论。

2.1 exists 查询

exists查询是查询非空值文档的一中查询方式,当字段不为空时被查询到。

POST singers/_search
{
  "query": {
    "exists": {
      "field": "neck"
    }
  }
}

得到结果:

#! Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See <https://www.elastic.co/guide/en/elasticsearch/reference/7.14/security-minimal-setup.html> to enable security.
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "singers",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "许嵩",
          "neck" : "Vae",
          "No" : "1",
          "age" : "35",
          "sex" : "男",
          "address" : "安徽省合肥市"
        }
      },
      {
        "_index" : "singers",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "name" : "周杰伦",
          "neck" : "Jay",
          "No" : "2",
          "age" : "42",
          "sex" : "男",
          "address" : "台湾省新北市"
        }
      },
      {
        "_index" : "singers",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "name" : "徐良",
          "neck" : "",
          "No" : "3",
          "age" : "34",
          "sex" : "男",
          "address" : "山东省青岛市"
        }
      },
      {
        "_index" : "singers",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 1.0,
        "_source" : {
          "name" : "汪苏泷",
          "neck" : "Silence",
          "No" : "4",
          "age" : "32",
          "sex" : "男",
          "address" : "辽宁省沈阳市"
        }
      }
    ]
  }
}

上述实例可知,本文在导入数据时,“韩红”“汪峰”两个文档并不包括neck字段,故使用exists查询时查询不到这两条文档。

2.2 prefix 查询

该查询方式与之前提到的match_phrase_prefix类似,但由于属于term查询方式,无法进行全文查询(不能查询text类型的字段)。

查询neck字段中带“V”的文档。

POST singers/_search
{
  "query": {
    "prefix": {
      "neck": "V"
    }
  }
}

得到结果:

#! Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See <https://www.elastic.co/guide/en/elasticsearch/reference/7.14/security-minimal-setup.html> to enable security.
{
  "took" : 54,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "singers",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "许嵩",
          "neck" : "Vae",
          "No" : "1",
          "age" : "35",
          "sex" : "男",
          "address" : "安徽省合肥市"
        }
      }
    ]
  }
}

2.3 wildcard查询

通配符查询

3 范围查询

3.1 基本内容

查询某一字段的范围,符合查询条件则返回结果

关键字:range

gt 区间最小值(不包括最小值)

gte 区间最小值(包括最小值)

lt:区间最大值(不包括最大值)

lte:区间最大值(包括最大值)

3.2 简单查询

可查询number类型的字段,例如:查询年龄在20到30岁的歌手。

请求

GET singers/_search
{
  "query": {
    "range": {
      "brith": {
        "gte": 20,
        "lte": 35,
      }
    }
  }
}

返回结果:

#! Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See <https://www.elastic.co/guide/en/elasticsearch/reference/7.14/security-minimal-setup.html> to enable security.
{
  "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" : "singers",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "许嵩",
          "neck" : "Vae",
          "No" : "1",
          "age" : "35",
          "sex" : "男",
          "brith" : "1986/5/14",
          "address" : "安徽省合肥市"
        }
      },
      {
        "_index" : "singers",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "name" : "徐良",
          "neck" : "",
          "No" : "3",
          "age" : "34",
          "sex" : "男",
          "brith" : "1987/2/8",
          "address" : "山东省青岛市"
        }
      },
      {
        "_index" : "singers",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 1.0,
        "_source" : {
          "name" : "汪苏泷",
          "neck" : "Silence",
          "No" : "4",
          "age" : "32",
          "sex" : "男",
          "brith" : "1989/9/17",
          "address" : "辽宁省沈阳市"
        }
      }
    ]
  }
}

3.3 时间查询

可查询时间字段,使用format设定时间的格式,例如查找1980年到1990年出生的歌手

GET singers/_search
{
  "query": {
    "range": {
      "brith": {
        "gte": 1970,
        "lte": 1990,
        "format": "yyyy/MM/dd||yyyy"
      }
    }
  }
}

返回结果:

#! Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See <https://www.elastic.co/guide/en/elasticsearch/reference/7.14/security-minimal-setup.html> to enable security.
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "singers",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "许嵩",
          "neck" : "Vae",
          "No" : "1",
          "age" : "35",
          "sex" : "男",
          "brith" : "1986/5/14",
          "address" : "安徽省合肥市"
        }
      },
      {
        "_index" : "singers",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "name" : "徐良",
          "neck" : "",
          "No" : "3",
          "age" : "34",
          "sex" : "男",
          "brith" : "1987/2/8",
          "address" : "山东省青岛市"
        }
      },
      {
        "_index" : "singers",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 1.0,
        "_source" : {
          "name" : "汪苏泷",
          "neck" : "Silence",
          "No" : "4",
          "age" : "32",
          "sex" : "男",
          "brith" : "1989/9/17",
          "address" : "辽宁省沈阳市"
        }
      }
    ]
  }
}

更新中... 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Shaco、LYF

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值