ElasticSearch 7 搜索(6)trem多种查询

批量导入数据

ES提供了Bulk的API 来进行批量操作

  • 数据结构类型,以换行区分

// 必须有一个索引叫book 
{"index": {"_index": "book", "_type": "_doc", "_id": 1}}
{"name": "权力的游戏"}
{"index": {"_index": "book", "_type": "_doc", "_id": 2}}
{"name": "疯狂的石头"}
// 结尾必须有换行
  • 请求
curl -X POST "localhost:9200/_bulk" -H 'Content-Type: application/json' --data-binary @name // name就是文件的路径和名字
  • 测试查询
{
    "query":{
        "match_all":{} 
    },
    "from": 0, 
    "size": 100
}

trem多种查询

  • 单词级别查询
  1. 这些查询通常用于结构化的数据,比如:number, date, keyword等,而不是对text
  2. 也就是说,全文本查询之前要先对文本内容进行分词,而单词级别的查询直接在相应字段的反向索引中精确查找,单词级别的查询一般用于数值、日期等类型的字段上
  • 准备索引 进行批量导入,数据在player中
{
    "mappings":{
        "properties":{
            "birthDay":{
                "type":"date"
            },
            "birthDayStr": {
                "type":"keyword"
            },
            "age":{
                "type":"integer"
            },
            "code": {
                "type":"text"
            },
            "country":{
                "type":"text"
            },
            "countryEn": {
                "type":"text"
            },
            "displayAffiliation":{
                "type":"text"
            },
            "displayName": {
                "type":"text"
            },
            "displayNameEn":{
                "type":"text"
            },
            "draft": {
                "type":"long"
            },
            "heightValue":{
                "type":"float"
            },
            "jerseyNo": {
                "type":"text"
            },
            "playYear":{
                "type":"long"
            },
            "playerId": {
                "type":"keyword"
            },
            "position":{
                "type":"text"
            },
            "schoolType": {
                "type":"text"
            },
            "teamCity":{
                 "type":"text"
            },
            "teamCityEn": {
                "type":"text"
            },
            "teamConference": {
                "type":"keyword"
            },
            "teamConferenceEn":{
                "type":"keyword"
            },
            "teamName": {
                "type":"keyword"
            },
            "teamNameEn":{
                "type":"keyword"
            },
            "weight": {
                "type":"text"
            }
        }
    }
}

term query 精准匹配查询 GET/POST请求

  • 请求
localhost:9200/nba/_search
  • 请求体
{   
  // 语义:精确查找球衣23号数据并分页
  "query": {
    "term": {
      "jerseyNo": 23
    }
  },
  "from": 0,
  "size": 20
}
  • 响应
{
    "took": 0,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 11,
            "relation": "eq"
        },
        "max_score": 3.6167762,
        "hits": [
            {
                "_index": "nba",
                "_type": "_doc",
                "_id": "73",
                "_score": 3.6167762,
                "_source": {
                    "countryEn": "United States",
                    "teamName": "雄鹿",
                    "birthDay": 792392400000,
                    "country": "美国",
                    "teamCityEn": "Milwaukee",
                    "code": "sterling_brown",
                    "displayAffiliation": "Southern Methodist/United States",
                    "displayName": "斯特林 布朗",
                    "schoolType": "College",
                    "teamConference": "东部",
                    "teamConferenceEn": "Eastern",
                    "weight": "105.2 公斤",
                    "teamCity": "密尔沃基",
                    "playYear": 2,
                    "jerseyNo": "23",
                    "teamNameEn": "Bucks",
                    "draft": 2017,
                    "displayNameEn": "Sterling Brown",
                    "heightValue": 1.98,
                    "birthDayStr": "1995-02-10",
                    "position": "后卫",
                    "age": 24,
                    "playerId": "1628425"
                }
            },
            {
                "_index": "nba",
                "_type": "_doc",
                "_id": "78",
                "_score": 3.6167762,
                "_source": {
                    "countryEn": "United States",
                    "teamName": "独行侠",
                    "birthDay": 721544400000,
                    "country": "美国",
                    "teamCityEn": "Dallas",
                    "code": "trey_burke",
                    "displayAffiliation": "Michigan/United States",
                    "displayName": "特雷 伯克",
                    "schoolType": "College",
                    "teamConference": "西部",
                    "teamConferenceEn": "Western",
                    "weight": "79.4 公斤",
                    "teamCity": "达拉斯",
                    "playYear": 6,
                    "jerseyNo": "23",
                    "teamNameEn": "Mavericks",
                    "draft": 2013,
                    "displayNameEn": "Trey Burke",
                    "heightValue": 1.85,
                    "birthDayStr": "1992-11-12",
                    "position": "后卫",
                    "age": 27,
                    "playerId": "203504"
                }
            },
            {
                "_index": "nba",
                "_type": "_doc",
                "_id": "167",
                "_score": 3.6167762,
                "_source": {
                    "countryEn": "United States",
                    "teamName": "雷霆",
                    "birthDay": 895377600000,
                    "country": "美国",
                    "teamCityEn": "Oklahoma City",
                    "code": "terrance_ferguson",
                    "displayAffiliation": "United States",
                    "displayName": "特伦斯 弗格森",
                    "schoolType": "",
                    "teamConference": "西部",
                    "teamConferenceEn": "Western",
                    "weight": "86.2 公斤",
                    "teamCity": "俄克拉荷马城",
                    "playYear": 2,
                    "jerseyNo": "23",
                    "teamNameEn": "Thunder",
                    "draft": 2017,
                    "displayNameEn": "Terrance Ferguson",
                    "heightValue": 2.01,
                    "birthDayStr": "1998-05-17",
                    "position": "后卫-前锋",
                    "age": 21,
                    "playerId": "1628390"
                }
            }
        ]
    }
}

exsit query 在特定的字段中查找非空值的文档 GET/POST请求

  • 请求
localhost:9200/nba/_search
  • 请求体
{
  // 语义:查找teamNameEn 不为空的数据
  "query": {
    "exists": {
      "field": "teamNameEn" 
    }
  },
  "from": 0,
  "size": 3
}
  • 响应
{
    "took": 0,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 566,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "nba",
                "_type": "_doc",
                "_id": "1",
                "_score": 1.0,
                "_source": {
                    "countryEn": "United States",
                    "teamName": "老鹰",
                    "birthDay": 831182400000,
                    "country": "美国",
                    "teamCityEn": "Atlanta",
                    "code": "jaylen_adams",
                    "displayAffiliation": "United States",
                    "displayName": "杰伦 亚当斯",
                    "schoolType": "College",
                    "teamConference": "东部",
                    "teamConferenceEn": "Eastern",
                    "weight": "86.2 公斤",
                    "teamCity": "亚特兰大",
                    "playYear": 1,
                    "jerseyNo": "10",
                    "teamNameEn": "Hawks",
                    "draft": 2018,
                    "displayNameEn": "Jaylen Adams",
                    "heightValue": 1.88,
                    "birthDayStr": "1996-05-04",
                    "position": "后卫",
                    "age": 23,
                    "playerId": "1629121"
                }
            },
            {
                "_index": "nba",
                "_type": "_doc",
                "_id": "2",
                "_score": 1.0,
                "_source": {
                    "countryEn": "New Zealand",
                    "teamName": "雷霆",
                    "birthDay": 743140800000,
                    "country": "新西兰",
                    "teamCityEn": "Oklahoma City",
                    "code": "steven_adams",
                    "displayAffiliation": "Pittsburgh/New Zealand",
                    "displayName": "斯蒂文 亚当斯",
                    "schoolType": "College",
                    "teamConference": "西部",
                    "teamConferenceEn": "Western",
                    "weight": "120.2 公斤",
                    "teamCity": "俄克拉荷马城",
                    "playYear": 6,
                    "jerseyNo": "12",
                    "teamNameEn": "Thunder",
                    "draft": 2013,
                    "displayNameEn": "Steven Adams",
                    "heightValue": 2.13,
                    "birthDayStr": "1993-07-20",
                    "position": "中锋",
                    "age": 26,
                    "playerId": "203500"
                }
            },
            {
                "_index": "nba",
                "_type": "_doc",
                "_id": "3",
                "_score": 1.0,
                "_source": {
                    "countryEn": "United States",
                    "teamName": "热火",
                    "birthDay": 869198400000,
                    "country": "美国",
                    "teamCityEn": "Miami",
                    "code": "bam_adebayo",
                    "displayAffiliation": "Kentucky/United States",
                    "displayName": "巴姆 阿德巴约",
                    "schoolType": "College",
                    "teamConference": "东部",
                    "teamConferenceEn": "Eastern",
                    "weight": "115.7 公斤",
                    "teamCity": "迈阿密",
                    "playYear": 2,
                    "jerseyNo": "13",
                    "teamNameEn": "Heat",
                    "draft": 2017,
                    "displayNameEn": "Bam Adebayo",
                    "heightValue": 2.08,
                    "birthDayStr": "1997-07-18",
                    "position": "中锋-前锋",
                    "age": 22,
                    "playerId": "1628389"
                }
            }
        ]
    }
}

prefix query 查找包含带有指定前缀term的文档 GET/POST请求

  • 请求
localhost:9200/nba/_search
  • 请求体
{
  // 语义:查找teamNameEn 不为空的数据 teamNameEn必须为text类型
  // 使用前缀类型必须为keyword,不能为text
  "query": {
    "prefix": {
      "teamNameEn": "Rock"
    }
  },
  "from": 0,
  "size": 3
}
  • 响应
{
    "took": 4,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 21,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "nba",
                "_type": "_doc",
                "_id": "86",
                "_score": 1.0,
                "_source": {
                    "countryEn": "Switzerland",
                    "teamName": "火箭",
                    "birthDay": 769233600000,
                    "country": "瑞士",
                    "teamCityEn": "Houston",
                    "code": "clint_capela",
                    "displayAffiliation": "Switzerland/Switzerland",
                    "displayName": "克林特 卡佩拉",
                    "schoolType": "",
                    "teamConference": "西部",
                    "teamConferenceEn": "Western",
                    "weight": "108.9 公斤",
                    "teamCity": "休斯顿",
                    "playYear": 5,
                    "jerseyNo": "15",
                    "teamNameEn": "Rockets",
                    "draft": 2014,
                    "displayNameEn": "Clint Capela",
                    "heightValue": 2.08,
                    "birthDayStr": "1994-05-18",
                    "position": "中锋",
                    "age": 25,
                    "playerId": "203991"
                }
            },
            {
                "_index": "nba",
                "_type": "_doc",
                "_id": "99",
                "_score": 1.0,
                "_source": {
                    "countryEn": "United States",
                    "teamName": "火箭",
                    "birthDay": 816930000000,
                    "country": "美国",
                    "teamCityEn": "Houston",
                    "code": "chris_chiozza",
                    "displayAffiliation": "University of Florida/United States",
                    "displayName": "克里斯 Chiozza",
                    "schoolType": "",
                    "teamConference": "西部",
                    "teamConferenceEn": "Western",
                    "weight": "79.4 公斤",
                    "teamCity": "休斯顿",
                    "playYear": 1,
                    "jerseyNo": "2",
                    "teamNameEn": "Rockets",
                    "draft": 2018,
                    "displayNameEn": "Chris Chiozza",
                    "heightValue": 1.83,
                    "birthDayStr": "1995-11-21",
                    "position": "后卫",
                    "age": 24,
                    "playerId": "1629185"
                }
            },
            {
                "_index": "nba",
                "_type": "_doc",
                "_id": "101",
                "_score": 1.0,
                "_source": {
                    "countryEn": "United States",
                    "teamName": "火箭",
                    "birthDay": 784962000000,
                    "country": "美国",
                    "teamCityEn": "Houston",
                    "code": "gary_clark",
                    "displayAffiliation": "University of Cincinnati/United States",
                    "displayName": "加里 克拉克",
                    "schoolType": "",
                    "teamConference": "西部",
                    "teamConferenceEn": "Western",
                    "weight": "102.1 公斤",
                    "teamCity": "休斯顿",
                    "playYear": 1,
                    "jerseyNo": "6",
                    "teamNameEn": "Rockets",
                    "draft": 2018,
                    "displayNameEn": "Gary Clark",
                    "heightValue": 2.03,
                    "birthDayStr": "1994-11-16",
                    "position": "前锋",
                    "age": 25,
                    "playerId": "1629109"
                }
            }
        ]
    }
}

wildcard query 支持通配符查询 GET/POST请求

*表示任意字符,?表示任意单个字符,类似SQL模糊查询

  • 请求
localhost:9200/nba/_search
  • 请求体
{
  // 语义: 查询teamNameEn为Ro*s *为模糊搜索
  "query": {
    "wildcard": {
      "teamNameEn": "Ro*s"
    }
  },
  "from": 0,
  "size": 3
}
  • 响应
{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 21,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "nba",
                "_type": "_doc",
                "_id": "86",
                "_score": 1.0,
                "_source": {
                    "countryEn": "Switzerland",
                    "teamName": "火箭",
                    "birthDay": 769233600000,
                    "country": "瑞士",
                    "teamCityEn": "Houston",
                    "code": "clint_capela",
                    "displayAffiliation": "Switzerland/Switzerland",
                    "displayName": "克林特 卡佩拉",
                    "schoolType": "",
                    "teamConference": "西部",
                    "teamConferenceEn": "Western",
                    "weight": "108.9 公斤",
                    "teamCity": "休斯顿",
                    "playYear": 5,
                    "jerseyNo": "15",
                    "teamNameEn": "Rockets",
                    "draft": 2014,
                    "displayNameEn": "Clint Capela",
                    "heightValue": 2.08,
                    "birthDayStr": "1994-05-18",
                    "position": "中锋",
                    "age": 25,
                    "playerId": "203991"
                }
            },
            {
                "_index": "nba",
                "_type": "_doc",
                "_id": "99",
                "_score": 1.0,
                "_source": {
                    "countryEn": "United States",
                    "teamName": "火箭",
                    "birthDay": 816930000000,
                    "country": "美国",
                    "teamCityEn": "Houston",
                    "code": "chris_chiozza",
                    "displayAffiliation": "University of Florida/United States",
                    "displayName": "克里斯 Chiozza",
                    "schoolType": "",
                    "teamConference": "西部",
                    "teamConferenceEn": "Western",
                    "weight": "79.4 公斤",
                    "teamCity": "休斯顿",
                    "playYear": 1,
                    "jerseyNo": "2",
                    "teamNameEn": "Rockets",
                    "draft": 2018,
                    "displayNameEn": "Chris Chiozza",
                    "heightValue": 1.83,
                    "birthDayStr": "1995-11-21",
                    "position": "后卫",
                    "age": 24,
                    "playerId": "1629185"
                }
            },
            {
                "_index": "nba",
                "_type": "_doc",
                "_id": "101",
                "_score": 1.0,
                "_source": {
                    "countryEn": "United States",
                    "teamName": "火箭",
                    "birthDay": 784962000000,
                    "country": "美国",
                    "teamCityEn": "Houston",
                    "code": "gary_clark",
                    "displayAffiliation": "University of Cincinnati/United States",
                    "displayName": "加里 克拉克",
                    "schoolType": "",
                    "teamConference": "西部",
                    "teamConferenceEn": "Western",
                    "weight": "102.1 公斤",
                    "teamCity": "休斯顿",
                    "playYear": 1,
                    "jerseyNo": "6",
                    "teamNameEn": "Rockets",
                    "draft": 2018,
                    "displayNameEn": "Gary Clark",
                    "heightValue": 2.03,
                    "birthDayStr": "1994-11-16",
                    "position": "前锋",
                    "age": 25,
                    "playerId": "1629109"
                }
            }
        ]
    }
}

regexp query 正则表达式查询 GET/POST请求

  • 请求
localhost:9200/nba/_search
  • 请求体
{
  // 语义: 查询teamNameEn 为Ro.*s  .表示任意 *为多个
  "query": {
    "regexp": {
      "teamNameEn": "Ro.*s"
    }
  },
  "from": 0,
  "size": 3
}
  • 响应
{
    "took": 0,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 21,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "nba",
                "_type": "_doc",
                "_id": "86",
                "_score": 1.0,
                "_source": {
                    "countryEn": "Switzerland",
                    "teamName": "火箭",
                    "birthDay": 769233600000,
                    "country": "瑞士",
                    "teamCityEn": "Houston",
                    "code": "clint_capela",
                    "displayAffiliation": "Switzerland/Switzerland",
                    "displayName": "克林特 卡佩拉",
                    "schoolType": "",
                    "teamConference": "西部",
                    "teamConferenceEn": "Western",
                    "weight": "108.9 公斤",
                    "teamCity": "休斯顿",
                    "playYear": 5,
                    "jerseyNo": "15",
                    "teamNameEn": "Rockets",
                    "draft": 2014,
                    "displayNameEn": "Clint Capela",
                    "heightValue": 2.08,
                    "birthDayStr": "1994-05-18",
                    "position": "中锋",
                    "age": 25,
                    "playerId": "203991"
                }
            },
            {
                "_index": "nba",
                "_type": "_doc",
                "_id": "99",
                "_score": 1.0,
                "_source": {
                    "countryEn": "United States",
                    "teamName": "火箭",
                    "birthDay": 816930000000,
                    "country": "美国",
                    "teamCityEn": "Houston",
                    "code": "chris_chiozza",
                    "displayAffiliation": "University of Florida/United States",
                    "displayName": "克里斯 Chiozza",
                    "schoolType": "",
                    "teamConference": "西部",
                    "teamConferenceEn": "Western",
                    "weight": "79.4 公斤",
                    "teamCity": "休斯顿",
                    "playYear": 1,
                    "jerseyNo": "2",
                    "teamNameEn": "Rockets",
                    "draft": 2018,
                    "displayNameEn": "Chris Chiozza",
                    "heightValue": 1.83,
                    "birthDayStr": "1995-11-21",
                    "position": "后卫",
                    "age": 24,
                    "playerId": "1629185"
                }
            },
            {
                "_index": "nba",
                "_type": "_doc",
                "_id": "101",
                "_score": 1.0,
                "_source": {
                    "countryEn": "United States",
                    "teamName": "火箭",
                    "birthDay": 784962000000,
                    "country": "美国",
                    "teamCityEn": "Houston",
                    "code": "gary_clark",
                    "displayAffiliation": "University of Cincinnati/United States",
                    "displayName": "加里 克拉克",
                    "schoolType": "",
                    "teamConference": "西部",
                    "teamConferenceEn": "Western",
                    "weight": "102.1 公斤",
                    "teamCity": "休斯顿",
                    "playYear": 1,
                    "jerseyNo": "6",
                    "teamNameEn": "Rockets",
                    "draft": 2018,
                    "displayNameEn": "Gary Clark",
                    "heightValue": 2.03,
                    "birthDayStr": "1994-11-16",
                    "position": "前锋",
                    "age": 25,
                    "playerId": "1629109"
                }
            }
        ]
    }
}

ids query 通过id批量查询 GET/POST请求

  • 请求
localhost:9200/nba/_search
  • 请求体
{
  "query": {
    "ids": {
      "values": [1,2,3]
    }
  },
  "from": 0,
  "size": 3
}
  • 响应
{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "nba",
                "_type": "_doc",
                "_id": "1",
                "_score": 1.0,
                "_source": {
                    "countryEn": "United States",
                    "teamName": "老鹰",
                    "birthDay": 831182400000,
                    "country": "美国",
                    "teamCityEn": "Atlanta",
                    "code": "jaylen_adams",
                    "displayAffiliation": "United States",
                    "displayName": "杰伦 亚当斯",
                    "schoolType": "College",
                    "teamConference": "东部",
                    "teamConferenceEn": "Eastern",
                    "weight": "86.2 公斤",
                    "teamCity": "亚特兰大",
                    "playYear": 1,
                    "jerseyNo": "10",
                    "teamNameEn": "Hawks",
                    "draft": 2018,
                    "displayNameEn": "Jaylen Adams",
                    "heightValue": 1.88,
                    "birthDayStr": "1996-05-04",
                    "position": "后卫",
                    "age": 23,
                    "playerId": "1629121"
                }
            },
            {
                "_index": "nba",
                "_type": "_doc",
                "_id": "2",
                "_score": 1.0,
                "_source": {
                    "countryEn": "New Zealand",
                    "teamName": "雷霆",
                    "birthDay": 743140800000,
                    "country": "新西兰",
                    "teamCityEn": "Oklahoma City",
                    "code": "steven_adams",
                    "displayAffiliation": "Pittsburgh/New Zealand",
                    "displayName": "斯蒂文 亚当斯",
                    "schoolType": "College",
                    "teamConference": "西部",
                    "teamConferenceEn": "Western",
                    "weight": "120.2 公斤",
                    "teamCity": "俄克拉荷马城",
                    "playYear": 6,
                    "jerseyNo": "12",
                    "teamNameEn": "Thunder",
                    "draft": 2013,
                    "displayNameEn": "Steven Adams",
                    "heightValue": 2.13,
                    "birthDayStr": "1993-07-20",
                    "position": "中锋",
                    "age": 26,
                    "playerId": "203500"
                }
            },
            {
                "_index": "nba",
                "_type": "_doc",
                "_id": "3",
                "_score": 1.0,
                "_source": {
                    "countryEn": "United States",
                    "teamName": "热火",
                    "birthDay": 869198400000,
                    "country": "美国",
                    "teamCityEn": "Miami",
                    "code": "bam_adebayo",
                    "displayAffiliation": "Kentucky/United States",
                    "displayName": "巴姆 阿德巴约",
                    "schoolType": "College",
                    "teamConference": "东部",
                    "teamConferenceEn": "Eastern",
                    "weight": "115.7 公斤",
                    "teamCity": "迈阿密",
                    "playYear": 2,
                    "jerseyNo": "13",
                    "teamNameEn": "Heat",
                    "draft": 2017,
                    "displayNameEn": "Bam Adebayo",
                    "heightValue": 2.08,
                    "birthDayStr": "1997-07-18",
                    "position": "中锋-前锋",
                    "age": 22,
                    "playerId": "1628389"
                }
            }
        ]
    }
}

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/03/12/elasticsearch-7-search-tree-multiple-queries/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
ElasticSearch 7 搜索(6)trem多种查询
批量导入数据 ES提供了Bulk的API 来进行批量操作 数据结构类型,以换行区分 // 必须有一个索引叫book {"index": {"_index": "bo……
<<上一篇
下一篇>>
文章目录
关闭
目 录