ElasticSearch 7 搜索(8)布尔查询

type description
must 必须出现在匹配文档中
filter 必须出现在文档中,但是不打分
must_not 不能出现在文档中
should 应该出现在文档中

must查询 GET/POST请求

  • 请求
localhost:9200/nba/_search
  • 请求体
{
  // 语义: 查询名字叫做james的球员
  "query": {
    "bool": { // 布尔查询
      "must":[ //匹配查询方式
        {
          "match": { // 全文搜索
            "displayNameEn": "james"
          }
        }
      ]
    }
  },
  "from": 0,
  "size": 3
}
  • 响应
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 5,
      "relation" : "eq"
    },
    "max_score" : 4.699642,
    "hits" : [
      {
        "_index" : "nba",
        "_type" : "_doc",
        "_id" : "214",
        "_score" : 4.699642,
        "_source" : {
          "countryEn" : "United States",
          "teamName" : "火箭",
          "birthDay" : 620107200000,
          "country" : "美国",
          "teamCityEn" : "Houston",
          "code" : "james_harden",
          "displayAffiliation" : "Arizona State/United States",
          "displayName" : "詹姆斯 哈登",
          "schoolType" : "College",
          "teamConference" : "西部",
          "teamConferenceEn" : "Western",
          "weight" : "99.8 公斤",
          "teamCity" : "休斯顿",
          "playYear" : 10,
          "jerseyNo" : "13",
          "teamNameEn" : "Rockets",
          "draft" : 2009,
          "displayNameEn" : "James Harden",
          "heightValue" : 1.96,
          "birthDayStr" : "1989-08-26",
          "position" : "后卫",
          "age" : 30,
          "playerId" : "201935"
        }
      },
      {
        "_index" : "nba",
        "_type" : "_doc",
        "_id" : "266",
        "_score" : 4.699642,
        "_source" : {
          "countryEn" : "United States",
          "teamName" : "国王",
          "birthDay" : 854082000000,
          "country" : "美国",
          "teamCityEn" : "Sacramento",
          "code" : "justin_james",
          "displayAffiliation" : "United States",
          "displayName" : "贾斯汀 詹姆斯",
          "schoolType" : "College",
          "teamConference" : "西部",
          "teamConferenceEn" : "Western",
          "weight" : "86.2 公斤",
          "teamCity" : "萨克拉门托",
          "playYear" : 0,
          "jerseyNo" : "",
          "teamNameEn" : "Kings",
          "draft" : 2019,
          "displayNameEn" : "Justin James",
          "heightValue" : 2.01,
          "birthDayStr" : "1997-01-24",
          "position" : "后卫-前锋",
          "age" : 22,
          "playerId" : "1629713"
        }
      },
      {
        "_index" : "nba",
        "_type" : "_doc",
        "_id" : "267",
        "_score" : 4.699642,
        "_source" : {
          "countryEn" : "United States",
          "teamName" : "湖人",
          "birthDay" : 473230800000,
          "country" : "美国",
          "teamCityEn" : "Los Angeles",
          "code" : "lebron_james",
          "displayAffiliation" : "No College/United States",
          "displayName" : "勒布朗 詹姆斯",
          "schoolType" : "High School",
          "teamConference" : "西部",
          "teamConferenceEn" : "Western",
          "weight" : "113.4 公斤",
          "teamCity" : "洛杉矶",
          "playYear" : 16,
          "jerseyNo" : "23",
          "teamNameEn" : "Lakers",
          "draft" : 2003,
          "displayNameEn" : "LeBron James",
          "heightValue" : 2.03,
          "birthDayStr" : "1984-12-30",
          "position" : "前锋",
          "age" : 35,
          "playerId" : "2544"
        }
      }
    ]
  }
}

filter查询 GET/POST请求

  • 请求
localhost:9200/nba/_search
  • 请求体
{
  // 语义: 查询名字叫做james的球员 不包含打分
  "query": {
    "bool": {
      "filter":[
        {
          "match": {
            "displayNameEn": "james"
          }
        }
      ]
    }
  },
  "from": 0,
  "size": 3
}
  • 响应
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 5,
      "relation" : "eq"
    },
    "max_score" : 0.0,
    "hits" : [
      {
        "_index" : "nba",
        "_type" : "_doc",
        "_id" : "158",
        "_score" : 0.0, // 不包含打分
        "_source" : {
          "countryEn" : "United States",
          "teamName" : "76人",
          "birthDay" : 646804800000,
          "country" : "美国",
          "teamCityEn" : "Philadelphia",
          "code" : "james_ennis iii",
          "displayAffiliation" : "Cal State-Long Beach/United States",
          "displayName" : "詹姆斯 恩尼斯三世",
          "schoolType" : "College",
          "teamConference" : "东部",
          "teamConferenceEn" : "Eastern",
          "weight" : "95.3 公斤",
          "teamCity" : "费城",
          "playYear" : 5,
          "jerseyNo" : "11",
          "teamNameEn" : "76ers",
          "draft" : 2013,
          "displayNameEn" : "James Ennis III",
          "heightValue" : 2.01,
          "birthDayStr" : "1990-07-01",
          "position" : "前锋",
          "age" : 29,
          "playerId" : "203516"
        }
      },
      {
        "_index" : "nba",
        "_type" : "_doc",
        "_id" : "214",
        "_score" : 0.0,
        "_source" : {
          "countryEn" : "United States",
          "teamName" : "火箭",
          "birthDay" : 620107200000,
          "country" : "美国",
          "teamCityEn" : "Houston",
          "code" : "james_harden",
          "displayAffiliation" : "Arizona State/United States",
          "displayName" : "詹姆斯 哈登",
          "schoolType" : "College",
          "teamConference" : "西部",
          "teamConferenceEn" : "Western",
          "weight" : "99.8 公斤",
          "teamCity" : "休斯顿",
          "playYear" : 10,
          "jerseyNo" : "13",
          "teamNameEn" : "Rockets",
          "draft" : 2009,
          "displayNameEn" : "James Harden",
          "heightValue" : 1.96,
          "birthDayStr" : "1989-08-26",
          "position" : "后卫",
          "age" : 30,
          "playerId" : "201935"
        }
      },
      {
        "_index" : "nba",
        "_type" : "_doc",
        "_id" : "266",
        "_score" : 0.0,
        "_source" : {
          "countryEn" : "United States",
          "teamName" : "国王",
          "birthDay" : 854082000000,
          "country" : "美国",
          "teamCityEn" : "Sacramento",
          "code" : "justin_james",
          "displayAffiliation" : "United States",
          "displayName" : "贾斯汀 詹姆斯",
          "schoolType" : "College",
          "teamConference" : "西部",
          "teamConferenceEn" : "Western",
          "weight" : "86.2 公斤",
          "teamCity" : "萨克拉门托",
          "playYear" : 0,
          "jerseyNo" : "",
          "teamNameEn" : "Kings",
          "draft" : 2019,
          "displayNameEn" : "Justin James",
          "heightValue" : 2.01,
          "birthDayStr" : "1997-01-24",
          "position" : "后卫-前锋",
          "age" : 22,
          "playerId" : "1629713"
        }
      }
    ]
  }
}

must_not查询 GET/POST请求

  • 请求
localhost:9200/nba/_search
  • 请求体
{
  // 语义: 查询名字叫做james的球员,一定不在东部的
  "query": {
    "bool": {
      "filter":[
        {
          "match": {
            "displayNameEn": "james"
          }
        }
      ],
      "must_not": [
        {
          "term": {
            "teamConferenceEn": {
              "value": "eastern"
            }
          }
        }
      ]
    }
  },
  "from": 0,
  "size": 3
}
  • 响应
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 5,
      "relation" : "eq"
    },
    "max_score" : 0.0,
    "hits" : [
      {
        "_index" : "nba",
        "_type" : "_doc",
        "_id" : "158",
        "_score" : 0.0,
        "_source" : {
          "countryEn" : "United States",
          "teamName" : "76人",
          "birthDay" : 646804800000,
          "country" : "美国",
          "teamCityEn" : "Philadelphia",
          "code" : "james_ennis iii",
          "displayAffiliation" : "Cal State-Long Beach/United States",
          "displayName" : "詹姆斯 恩尼斯三世",
          "schoolType" : "College",
          "teamConference" : "东部",
          "teamConferenceEn" : "Eastern",
          "weight" : "95.3 公斤",
          "teamCity" : "费城",
          "playYear" : 5,
          "jerseyNo" : "11",
          "teamNameEn" : "76ers",
          "draft" : 2013,
          "displayNameEn" : "James Ennis III",
          "heightValue" : 2.01,
          "birthDayStr" : "1990-07-01",
          "position" : "前锋",
          "age" : 29,
          "playerId" : "203516"
        }
      },
      {
        "_index" : "nba",
        "_type" : "_doc",
        "_id" : "214",
        "_score" : 0.0,
        "_source" : {
          "countryEn" : "United States",
          "teamName" : "火箭",
          "birthDay" : 620107200000,
          "country" : "美国",
          "teamCityEn" : "Houston",
          "code" : "james_harden",
          "displayAffiliation" : "Arizona State/United States",
          "displayName" : "詹姆斯 哈登",
          "schoolType" : "College",
          "teamConference" : "西部",
          "teamConferenceEn" : "Western",
          "weight" : "99.8 公斤",
          "teamCity" : "休斯顿",
          "playYear" : 10,
          "jerseyNo" : "13",
          "teamNameEn" : "Rockets",
          "draft" : 2009,
          "displayNameEn" : "James Harden",
          "heightValue" : 1.96,
          "birthDayStr" : "1989-08-26",
          "position" : "后卫",
          "age" : 30,
          "playerId" : "201935"
        }
      },
      {
        "_index" : "nba",
        "_type" : "_doc",
        "_id" : "266",
        "_score" : 0.0,
        "_source" : {
          "countryEn" : "United States",
          "teamName" : "国王",
          "birthDay" : 854082000000,
          "country" : "美国",
          "teamCityEn" : "Sacramento",
          "code" : "justin_james",
          "displayAffiliation" : "United States",
          "displayName" : "贾斯汀 詹姆斯",
          "schoolType" : "College",
          "teamConference" : "西部",
          "teamConferenceEn" : "Western",
          "weight" : "86.2 公斤",
          "teamCity" : "萨克拉门托",
          "playYear" : 0,
          "jerseyNo" : "",
          "teamNameEn" : "Kings",
          "draft" : 2019,
          "displayNameEn" : "Justin James",
          "heightValue" : 2.01,
          "birthDayStr" : "1997-01-24",
          "position" : "后卫-前锋",
          "age" : 22,
          "playerId" : "1629713"
        }
      }
    ]
  }
}

should 第一种查询 GET/POST请求

  • 请求
localhost:9200/nba/_search
  • 请求体
{
  // 语义: 查找名字叫做james的打球时间应该在11到20年的西部球员
  // should应该 但是不是必须
  "query": {
    "bool": {
      "filter":[
        {
          "match": {
            "displayNameEn": "james"
          }
        }
      ],
      "must_not": [
        {
          "term": {
            "teamConferenceEn": {
              "value": "eastern"
            }
          }
        }
      ],
      "should": [
        {
          "range": {
            "playYear": {
              "gte": 11,
              "lte": 20
            }
          }
        }
      ]
    }
  },
  "from": 0,
  "size": 3
}
  • 响应
{
  "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" : "nba",
        "_type" : "_doc",
        "_id" : "267",
        "_score" : 1.0,
        "_source" : {
          "countryEn" : "United States",
          "teamName" : "湖人",
          "birthDay" : 473230800000,
          "country" : "美国",
          "teamCityEn" : "Los Angeles",
          "code" : "lebron_james",
          "displayAffiliation" : "No College/United States",
          "displayName" : "勒布朗 詹姆斯",
          "schoolType" : "High School",
          "teamConference" : "西部",
          "teamConferenceEn" : "Western",
          "weight" : "113.4 公斤",
          "teamCity" : "洛杉矶",
          "playYear" : 16,
          "jerseyNo" : "23",
          "teamNameEn" : "Lakers",
          "draft" : 2003,
          "displayNameEn" : "LeBron James",
          "heightValue" : 2.03,
          "birthDayStr" : "1984-12-30",
          "position" : "前锋",
          "age" : 35,
          "playerId" : "2544"
        }
      },
      {
        "_index" : "nba",
        "_type" : "_doc",
        "_id" : "158",
        "_score" : 0.0,
        "_source" : {
          "countryEn" : "United States",
          "teamName" : "76人",
          "birthDay" : 646804800000,
          "country" : "美国",
          "teamCityEn" : "Philadelphia",
          "code" : "james_ennis iii",
          "displayAffiliation" : "Cal State-Long Beach/United States",
          "displayName" : "詹姆斯 恩尼斯三世",
          "schoolType" : "College",
          "teamConference" : "东部",
          "teamConferenceEn" : "Eastern",
          "weight" : "95.3 公斤",
          "teamCity" : "费城",
          "playYear" : 5,
          "jerseyNo" : "11",
          "teamNameEn" : "76ers",
          "draft" : 2013,
          "displayNameEn" : "James Ennis III",
          "heightValue" : 2.01,
          "birthDayStr" : "1990-07-01",
          "position" : "前锋",
          "age" : 29,
          "playerId" : "203516"
        }
      },
      {
        "_index" : "nba",
        "_type" : "_doc",
        "_id" : "214",
        "_score" : 0.0,
        "_source" : {
          "countryEn" : "United States",
          "teamName" : "火箭",
          "birthDay" : 620107200000,
          "country" : "美国",
          "teamCityEn" : "Houston",
          "code" : "james_harden",
          "displayAffiliation" : "Arizona State/United States",
          "displayName" : "詹姆斯 哈登",
          "schoolType" : "College",
          "teamConference" : "西部",
          "teamConferenceEn" : "Western",
          "weight" : "99.8 公斤",
          "teamCity" : "休斯顿",
          "playYear" : 10,
          "jerseyNo" : "13",
          "teamNameEn" : "Rockets",
          "draft" : 2009,
          "displayNameEn" : "James Harden",
          "heightValue" : 1.96,
          "birthDayStr" : "1989-08-26",
          "position" : "后卫",
          "age" : 30,
          "playerId" : "201935"
        }
      }
    ]
  }
}

should 第二种查询 GET/POST请求

  • 请求
localhost:9200/nba/_search
  • 请求体

    {
    // 语义: 查找名字叫做james的打球时间应该在11到20年的西部球员
    // minimum_should_match = 1 最小匹配精度,至少有一个条件满足
    "query": {
    "bool": {
      "filter":[
        {
          "match": {
            "displayNameEn": "james"
          }
        }
      ],
      "must_not": [
        {
          "term": {
            "teamConferenceEn": {
              "value": "eastern"
            }
          }
        }
      ],
      "should": [
        {
          "range": {
            "playYear": {
              "gte": 11,
              "lte": 20
            }
          }
        }
      ]
      , "minimum_should_match": 1
    }
    },
    "from": 0,
    "size": 3
    }
  • 响应

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "nba",
        "_type" : "_doc",
        "_id" : "267",
        "_score" : 1.0,
        "_source" : {
          "countryEn" : "United States",
          "teamName" : "湖人",
          "birthDay" : 473230800000,
          "country" : "美国",
          "teamCityEn" : "Los Angeles",
          "code" : "lebron_james",
          "displayAffiliation" : "No College/United States",
          "displayName" : "勒布朗 詹姆斯",
          "schoolType" : "High School",
          "teamConference" : "西部",
          "teamConferenceEn" : "Western",
          "weight" : "113.4 公斤",
          "teamCity" : "洛杉矶",
          "playYear" : 16,
          "jerseyNo" : "23",
          "teamNameEn" : "Lakers",
          "draft" : 2003,
          "displayNameEn" : "LeBron James",
          "heightValue" : 2.03,
          "birthDayStr" : "1984-12-30",
          "position" : "前锋",
          "age" : 35,
          "playerId" : "2544"
        }
      }
    ]
  }
}

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

THE END
分享
二维码
打赏
海报
ElasticSearch 7 搜索(8)布尔查询
type description must 必须出现在匹配文档中 filter 必须出现在文档中,但是不打分 must_not 不能出现在文档中 should 应该出现在文档中 mu……
<<上一篇
下一篇>>
文章目录
关闭
目 录