ElasticSearch 7 搜索(12)query_string 查询

query_string查询,如果熟悉lucene的查询语法,我们可以直接用lucene查询语法写一个查询串进行查询,ES中接到请求后,通过查询解析器,解析查询串生成对应的查询。

query_string AND OR 单字段查询 GET/POST请求

  • 请求

localhost:9200/nba/_search
  • 请求体
{
  // 语义: 指定单个字段查询(查询詹姆斯或者库里的球员)
  "query": {
    "query_string": {
      "default_field": "displayNameEn",
      "query": "james OR curry" // 这里可以使用or或and也就是sql里的or和and
      //"query": "james AND curry"
    }
  },
  "size": 3
}
  • 响应
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 7,
      "relation" : "eq"
    },
    "max_score" : 5.4989905,
    "hits" : [
      {
        "_index" : "nba",
        "_type" : "_doc",
        "_id" : "123",
        "_score" : 5.4989905,
        "_source" : {
          "countryEn" : "United States",
          "teamName" : "独行侠",
          "birthDay" : 651384000000,
          "country" : "美国",
          "teamCityEn" : "Dallas",
          "code" : "seth_curry",
          "displayAffiliation" : "Duke/United States",
          "displayName" : "賽斯 库里",
          "schoolType" : "College",
          "teamConference" : "西部",
          "teamConferenceEn" : "Western",
          "weight" : "83.9 公斤",
          "teamCity" : "达拉斯",
          "playYear" : 6,
          "jerseyNo" : "30",
          "teamNameEn" : "Mavericks",
          "draft" : 2013,
          "displayNameEn" : "Seth Curry",
          "heightValue" : 1.88,
          "birthDayStr" : "1990-08-23",
          "position" : "后卫",
          "age" : 29,
          "playerId" : "203552"
        }
      },
      {
        "_index" : "nba",
        "_type" : "_doc",
        "_id" : "124",
        "_score" : 5.4989905,
        "_source" : {
          "countryEn" : "United States",
          "teamName" : "勇士",
          "birthDay" : 574318800000,
          "country" : "美国",
          "teamCityEn" : "Golden State",
          "code" : "stephen_curry",
          "displayAffiliation" : "Davidson/United States",
          "displayName" : "斯蒂芬 库里",
          "schoolType" : "College",
          "teamConference" : "西部",
          "teamConferenceEn" : "Western",
          "weight" : "86.2 公斤",
          "teamCity" : "金州",
          "playYear" : 10,
          "jerseyNo" : "30",
          "teamNameEn" : "Warriors",
          "draft" : 2009,
          "displayNameEn" : "Stephen Curry",
          "heightValue" : 1.9,
          "birthDayStr" : "1988-03-14",
          "position" : "后卫",
          "age" : 31,
          "playerId" : "201939"
        }
      },
      {
        "_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"
        }
      }
    ]
  }
}

query_string AND OR 多字段查询 GET/POST请求

  • 请求
localhost:9200/nba/_search
  • 请求体
{
  // 语义: 指定多个字段查询(查询詹姆斯或者库里的球员)
  "query": {
    "query_string": {
      "fields": [ // 指定多个字段
        "displayNameEn",
        "teamNameEn"
        ],
      "query": "james AND Rockets" // 查询条件
    }
  },
  "size": 3
}
  • 响应
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 7.9719486,
    "hits" : [
      {
        "_index" : "nba",
        "_type" : "_doc",
        "_id" : "214",
        "_score" : 7.9719486,
        "_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"
        }
      }
    ]
  }
}

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

THE END
分享
二维码
打赏
海报
ElasticSearch 7 搜索(12)query_string 查询
query_string查询,如果熟悉lucene的查询语法,我们可以直接用lucene查询语法写一个查询串进行查询,ES中接到请求后,通过查询解析器,解析查询串生成对应的查……
<<上一篇
下一篇>>
文章目录
关闭
目 录