ES官网reference翻译文章(7)—Start searching

对ES官网的reference的翻译,同时也是备忘,ES版本为7.5

下面是正文翻译,附上原文链接

https://www.elastic.co/guide/en/elasticsearch/reference/current/getting-started-search.html

==================================================================================================

开始搜索

一旦你已经往ES的索引中注入一些数据,你就可以通过向_search终点发送请求开始搜索数据了。为了获取搜索的全套能力,你使用ES查询语言来标明请求体中的搜索准则,你在请求URI中指明你想要搜索的索引名。

例如,下面的请求检索了bank索引中的所有文档并按照account number排序:

curl http://9.25.176.228:8080/bank/_search?pretty -H 'content-type:application/json' -d '{"query":{"match_all":{}}, "sort":[{"account_number":"asc"}]}'

默认的,响应的hits部分会包含匹配搜索准则的前10个文档:

{
  "took" : 8,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1000,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "0",
        "_score" : null,
        "_source" : {
          "account_number" : 0,
          "balance" : 16623,
          "firstname" : "Bradshaw",
          "lastname" : "Mckenzie",
          "age" : 29,
          "gender" : "F",
          "address" : "244 Columbus Place",
          "employer" : "Euron",
          "email" : "bradshawmckenzie@euron.com",
          "city" : "Hobucken",
          "state" : "CO"
        },
        "sort" : [
          0
        ]
      },
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : null,
        "_source" : {
          "account_number" : 1,
          "balance" : 39225,
          "firstname" : "Amber",
          "lastname" : "Duke",
          "age" : 32,
          "gender" : "M",
          "address" : "880 Holmes Lane",
          "employer" : "Pyrami",
          "email" : "amberduke@pyrami.com",
          "city" : "Brogan",
          "state" : "IL"
        },
        "sort" : [
          1
        ]
      },
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : null,
        "_source" : {
          "account_number" : 2,
          "balance" : 28838,
          "firstname" : "Roberta",
          "lastname" : "Bender",
          "age" : 22,
          "gender" : "F",
          "address" : "560 Kingsway Place",
          "employer" : "Chillium",
          "email" : "robertabender@chillium.com",
          "city" : "Bennett",
          "state" : "LA"
        },
        "sort" : [
          2
        ]
      },
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : null,
        "_source" : {
          "account_number" : 3,
          "balance" : 44947,
          "firstname" : "Levine",
          "lastname" : "Burks",
          "age" : 26,
          "gender" : "F",
          "address" : "328 Wilson Avenue",
          "employer" : "Amtap",
          "email" : "levineburks@amtap.com",
          "city" : "Cochranville",
          "state" : "HI"
        },
        "sort" : [
          3
        ]
      },
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : null,
        "_source" : {
          "account_number" : 4,
          "balance" : 27658,
          "firstname" : "Rodriquez",
          "lastname" : "Flores",
          "age" : 31,
          "gender" : "F",
          "address" : "986 Wyckoff Avenue",
          "employer" : "Tourmania",
          "email" : "rodriquezflores@tourmania.com",
          "city" : "Eastvale",
          "state" : "HI"
        },
        "sort" : [
          4
        ]
      },
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "5",
        "_score" : null,
        "_source" : {
          "account_number" : 5,
          "balance" : 29342,
          "firstname" : "Leola",
          "lastname" : "Stewart",
          "age" : 30,
          "gender" : "F",
          "address" : "311 Elm Place",
          "employer" : "Diginetic",
          "email" : "leolastewart@diginetic.com",
          "city" : "Fairview",
          "state" : "NJ"
        },
        "sort" : [
          5
        ]
      },
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "6",
        "_score" : null,
        "_source" : {
          "account_number" : 6,
          "balance" : 5686,
          "firstname" : "Hattie",
          "lastname" : "Bond",
          "age" : 36,
          "gender" : "M",
          "address" : "671 Bristol Street",
          "employer" : "Netagy",
          "email" : "hattiebond@netagy.com",
          "city" : "Dante",
          "state" : "TN"
        },
        "sort" : [
          6
        ]
      },
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "7",
        "_score" : null,
        "_source" : {
          "account_number" : 7,
          "balance" : 39121,
          "firstname" : "Levy",
          "lastname" : "Richard",
          "age" : 22,
          "gender" : "M",
          "address" : "820 Logan Street",
          "employer" : "Teraprene",
          "email" : "levyrichard@teraprene.com",
          "city" : "Shrewsbury",
          "state" : "MO"
        },
        "sort" : [
          7
        ]
      },
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "8",
        "_score" : null,
        "_source" : {
          "account_number" : 8,
          "balance" : 48868,
          "firstname" : "Jan",
          "lastname" : "Burns",
          "age" : 35,
          "gender" : "M",
          "address" : "699 Visitation Place",
          "employer" : "Glasstep",
          "email" : "janburns@glasstep.com",
          "city" : "Wakulla",
          "state" : "AZ"
        },
        "sort" : [
          8
        ]
      },
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "9",
        "_score" : null,
        "_source" : {
          "account_number" : 9,
          "balance" : 24776,
          "firstname" : "Opal",
          "lastname" : "Meadows",
          "age" : 39,
          "gender" : "M",
          "address" : "963 Neptune Avenue",
          "employer" : "Cedward",
          "email" : "opalmeadows@cedward.com",
          "city" : "Olney",
          "state" : "OH"
        },
        "sort" : [
          9
        ]
      }
    ]
  }
}

响应也提供了关于查询请求的下列信息:

1)took:ES花费了多长时间来执行查询,单位是ms

2)timed_out:这次搜索请求是否超时了

3)_shards:搜索了几个分片,其中有多少分片成功、失败或者被跳过

4)max_score:找到的最相关的文档的分数(使用match_all时不可获得)

5)hits.total.value:总共匹配上多少个文档

6)hits.sort:这个文档的排序位置(当没有被相关性打分排序时)

7)hits._score:这个文档的相关性打分(使用match_all时不可获得,个人理解其实就是没有打分或者不适用于打分时,文档的位置

每个搜索请求都是独立的:ES不会包含任何跨请求的状态信息,如果要翻阅搜索结果,请在请求中指定from和size参数。

例如,下面的请求会返回10-19的hits:

curl http://9.25.176.228:8080/bank/_search?pretty -H 'content-type:application/json' -d '{"query":{"match_all":{}}, "sort":[{"account_number":"asc"}], "from":10, "size":10}'
{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1000,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "10",
        "_score" : null,
        "_source" : {
          "account_number" : 10,
          "balance" : 46170,
          "firstname" : "Dominique",
          "lastname" : "Park",
          "age" : 37,
          "gender" : "F",
          "address" : "100 Gatling Place",
          "employer" : "Conjurica",
          "email" : "dominiquepark@conjurica.com",
          "city" : "Omar",
          "state" : "NJ"
        },
        "sort" : [
          10
        ]
      },
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "11",
        "_score" : null,
        "_source" : {
          "account_number" : 11,
          "balance" : 20203,
          "firstname" : "Jenkins",
          "lastname" : "Haney",
          "age" : 20,
          "gender" : "M",
          "address" : "740 Ferry Place",
          "employer" : "Qimonk",
          "email" : "jenkinshaney@qimonk.com",
          "city" : "Steinhatchee",
          "state" : "GA"
        },
        "sort" : [
          11
        ]
      },
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "12",
        "_score" : null,
        "_source" : {
          "account_number" : 12,
          "balance" : 37055,
          "firstname" : "Stafford",
          "lastname" : "Brock",
          "age" : 20,
          "gender" : "F",
          "address" : "296 Wythe Avenue",
          "employer" : "Uncorp",
          "email" : "staffordbrock@uncorp.com",
          "city" : "Bend",
          "state" : "AL"
        },
        "sort" : [
          12
        ]
      },
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "13",
        "_score" : null,
        "_source" : {
          "account_number" : 13,
          "balance" : 32838,
          "firstname" : "Nanette",
          "lastname" : "Bates",
          "age" : 28,
          "gender" : "F",
          "address" : "789 Madison Street",
          "employer" : "Quility",
          "email" : "nanettebates@quility.com",
          "city" : "Nogal",
          "state" : "VA"
        },
        "sort" : [
          13
        ]
      },
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "14",
        "_score" : null,
        "_source" : {
          "account_number" : 14,
          "balance" : 20480,
          "firstname" : "Erma",
          "lastname" : "Kane",
          "age" : 39,
          "gender" : "F",
          "address" : "661 Vista Place",
          "employer" : "Stockpost",
          "email" : "ermakane@stockpost.com",
          "city" : "Chamizal",
          "state" : "NY"
        },
        "sort" : [
          14
        ]
      },
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "15",
        "_score" : null,
        "_source" : {
          "account_number" : 15,
          "balance" : 43456,
          "firstname" : "Bobbie",
          "lastname" : "Sexton",
          "age" : 21,
          "gender" : "M",
          "address" : "232 Sedgwick Place",
          "employer" : "Zytrex",
          "email" : "bobbiesexton@zytrex.com",
          "city" : "Hendersonville",
          "state" : "CA"
        },
        "sort" : [
          15
        ]
      },
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "16",
        "_score" : null,
        "_source" : {
          "account_number" : 16,
          "balance" : 35883,
          "firstname" : "Adrian",
          "lastname" : "Pitts",
          "age" : 34,
          "gender" : "F",
          "address" : "963 Fay Court",
          "employer" : "Combogene",
          "email" : "adrianpitts@combogene.com",
          "city" : "Remington",
          "state" : "SD"
        },
        "sort" : [
          16
        ]
      },
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "17",
        "_score" : null,
        "_source" : {
          "account_number" : 17,
          "balance" : 7831,
          "firstname" : "Bessie",
          "lastname" : "Orr",
          "age" : 31,
          "gender" : "F",
          "address" : "239 Hinsdale Street",
          "employer" : "Skyplex",
          "email" : "bessieorr@skyplex.com",
          "city" : "Graball",
          "state" : "FL"
        },
        "sort" : [
          17
        ]
      },
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "18",
        "_score" : null,
        "_source" : {
          "account_number" : 18,
          "balance" : 4180,
          "firstname" : "Dale",
          "lastname" : "Adams",
          "age" : 33,
          "gender" : "M",
          "address" : "467 Hutchinson Court",
          "employer" : "Boink",
          "email" : "daleadams@boink.com",
          "city" : "Orick",
          "state" : "MD"
        },
        "sort" : [
          18
        ]
      },
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "19",
        "_score" : null,
        "_source" : {
          "account_number" : 19,
          "balance" : 27894,
          "firstname" : "Schwartz",
          "lastname" : "Buchanan",
          "age" : 28,
          "gender" : "F",
          "address" : "449 Mersereau Court",
          "employer" : "Sybixtex",
          "email" : "schwartzbuchanan@sybixtex.com",
          "city" : "Greenwich",
          "state" : "KS"
        },
        "sort" : [
          19
        ]
      }
    ]
  }
}

既然你已经知道如何提交一个基础的搜索请求了,你可以开始构造比match_all有趣一点的请求了。

如果要搜索某个字段的特定词,你可以使用match查询。例如,下面的请求搜索address字段来匹配地址中包含mill或者lane的顾客:

curl http://9.25.176.228:8080/bank/_search?pretty -H 'content-type:application/json' -d '{"query":{"match":{"address":"mill lane"}}}'
{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 19,
      "relation" : "eq"
    },
    "max_score" : 9.507477,
    "hits" : [
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "136",
        "_score" : 9.507477,
        "_source" : {
          "account_number" : 136,
          "balance" : 45801,
          "firstname" : "Winnie",
          "lastname" : "Holland",
          "age" : 38,
          "gender" : "M",
          "address" : "198 Mill Lane",
          "employer" : "Neteria",
          "email" : "winnieholland@neteria.com",
          "city" : "Urie",
          "state" : "IL"
        }
      },
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "970",
        "_score" : 5.4032025,
        "_source" : {
          "account_number" : 970,
          "balance" : 19648,
          "firstname" : "Forbes",
          "lastname" : "Wallace",
          "age" : 28,
          "gender" : "M",
          "address" : "990 Mill Road",
          "employer" : "Pheast",
          "email" : "forbeswallace@pheast.com",
          "city" : "Lopezo",
          "state" : "AK"
        }
      },
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "345",
        "_score" : 5.4032025,
        "_source" : {
          "account_number" : 345,
          "balance" : 9812,
          "firstname" : "Parker",
          "lastname" : "Hines",
          "age" : 38,
          "gender" : "M",
          "address" : "715 Mill Avenue",
          "employer" : "Baluba",
          "email" : "parkerhines@baluba.com",
          "city" : "Blackgum",
          "state" : "KY"
        }
      },
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "472",
        "_score" : 5.4032025,
        "_source" : {
          "account_number" : 472,
          "balance" : 25571,
          "firstname" : "Lee",
          "lastname" : "Long",
          "age" : 32,
          "gender" : "F",
          "address" : "288 Mill Street",
          "employer" : "Comverges",
          "email" : "leelong@comverges.com",
          "city" : "Movico",
          "state" : "MT"
        }
      },
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 4.1042743,
        "_source" : {
          "account_number" : 1,
          "balance" : 39225,
          "firstname" : "Amber",
          "lastname" : "Duke",
          "age" : 32,
          "gender" : "M",
          "address" : "880 Holmes Lane",
          "employer" : "Pyrami",
          "email" : "amberduke@pyrami.com",
          "city" : "Brogan",
          "state" : "IL"
        }
      },
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "70",
        "_score" : 4.1042743,
        "_source" : {
          "account_number" : 70,
          "balance" : 38172,
          "firstname" : "Deidre",
          "lastname" : "Thompson",
          "age" : 33,
          "gender" : "F",
          "address" : "685 School Lane",
          "employer" : "Netplode",
          "email" : "deidrethompson@netplode.com",
          "city" : "Chestnut",
          "state" : "GA"
        }
      },
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "556",
        "_score" : 4.1042743,
        "_source" : {
          "account_number" : 556,
          "balance" : 36420,
          "firstname" : "Collier",
          "lastname" : "Odonnell",
          "age" : 35,
          "gender" : "M",
          "address" : "591 Nolans Lane",
          "employer" : "Sultraxin",
          "email" : "collierodonnell@sultraxin.com",
          "city" : "Fulford",
          "state" : "MD"
        }
      },
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "568",
        "_score" : 4.1042743,
        "_source" : {
          "account_number" : 568,
          "balance" : 36628,
          "firstname" : "Lesa",
          "lastname" : "Maynard",
          "age" : 29,
          "gender" : "F",
          "address" : "295 Whitty Lane",
          "employer" : "Coash",
          "email" : "lesamaynard@coash.com",
          "city" : "Broadlands",
          "state" : "VT"
        }
      },
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "715",
        "_score" : 4.1042743,
        "_source" : {
          "account_number" : 715,
          "balance" : 23734,
          "firstname" : "Tammi",
          "lastname" : "Hodge",
          "age" : 24,
          "gender" : "M",
          "address" : "865 Church Lane",
          "employer" : "Netur",
          "email" : "tammihodge@netur.com",
          "city" : "Lacomb",
          "state" : "KS"
        }
      },
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "449",
        "_score" : 4.1042743,
        "_source" : {
          "account_number" : 449,
          "balance" : 41950,
          "firstname" : "Barnett",
          "lastname" : "Cantrell",
          "age" : 39,
          "gender" : "F",
          "address" : "945 Bedell Lane",
          "employer" : "Zentility",
          "email" : "barnettcantrell@zentility.com",
          "city" : "Swartzville",
          "state" : "ND"
        }
      }
    ]
  }
}

为了构造更加复杂的查询,你可以使用bool查询来结合多个查询准则,你可以将准则指定为必备(must match)、可选的(should match)或者不应该(must not match)。

例如,下面的请求会搜索bank索引来匹配属于40岁且不居住在Iadho(ID)的顾客的账户:

curl http://9.25.176.228:8080/bank/_search?pretty -H 'content-type:application/json' -d '{"query":{"bool":{"must":[{"match":{"age":"40"}}],"must_not":[{"match":{"state":"ID"}}]}}}'
{
  "took" : 7,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 43,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "474",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 474,
          "balance" : 35896,
          "firstname" : "Obrien",
          "lastname" : "Walton",
          "age" : 40,
          "gender" : "F",
          "address" : "192 Ide Court",
          "employer" : "Suremax",
          "email" : "obrienwalton@suremax.com",
          "city" : "Crucible",
          "state" : "UT"
        }
      },
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "479",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 479,
          "balance" : 31865,
          "firstname" : "Cameron",
          "lastname" : "Ross",
          "age" : 40,
          "gender" : "M",
          "address" : "904 Bouck Court",
          "employer" : "Telpod",
          "email" : "cameronross@telpod.com",
          "city" : "Nord",
          "state" : "MO"
        }
      },
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "549",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 549,
          "balance" : 1932,
          "firstname" : "Jacqueline",
          "lastname" : "Maxwell",
          "age" : 40,
          "gender" : "M",
          "address" : "444 Schenck Place",
          "employer" : "Fuelworks",
          "email" : "jacquelinemaxwell@fuelworks.com",
          "city" : "Oretta",
          "state" : "OR"
        }
      },
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "878",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 878,
          "balance" : 49159,
          "firstname" : "Battle",
          "lastname" : "Blackburn",
          "age" : 40,
          "gender" : "F",
          "address" : "234 Hendrix Street",
          "employer" : "Zilphur",
          "email" : "battleblackburn@zilphur.com",
          "city" : "Wanamie",
          "state" : "PA"
        }
      },
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "885",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 885,
          "balance" : 31661,
          "firstname" : "Valdez",
          "lastname" : "Roberson",
          "age" : 40,
          "gender" : "F",
          "address" : "227 Scholes Street",
          "employer" : "Delphide",
          "email" : "valdezroberson@delphide.com",
          "city" : "Chilton",
          "state" : "MT"
        }
      },
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "948",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 948,
          "balance" : 37074,
          "firstname" : "Sargent",
          "lastname" : "Powers",
          "age" : 40,
          "gender" : "M",
          "address" : "532 Fiske Place",
          "employer" : "Accuprint",
          "email" : "sargentpowers@accuprint.com",
          "city" : "Umapine",
          "state" : "AK"
        }
      },
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "998",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 998,
          "balance" : 16869,
          "firstname" : "Letha",
          "lastname" : "Baker",
          "age" : 40,
          "gender" : "F",
          "address" : "206 Llama Court",
          "employer" : "Dognosis",
          "email" : "lethabaker@dognosis.com",
          "city" : "Dunlo",
          "state" : "WV"
        }
      },
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "40",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 40,
          "balance" : 33882,
          "firstname" : "Pace",
          "lastname" : "Molina",
          "age" : 40,
          "gender" : "M",
          "address" : "263 Ovington Court",
          "employer" : "Cytrak",
          "email" : "pacemolina@cytrak.com",
          "city" : "Silkworth",
          "state" : "OR"
        }
      },
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "165",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 165,
          "balance" : 18956,
          "firstname" : "Sims",
          "lastname" : "Mckay",
          "age" : 40,
          "gender" : "F",
          "address" : "205 Jackson Street",
          "employer" : "Comtour",
          "email" : "simsmckay@comtour.com",
          "city" : "Tilden",
          "state" : "DC"
        }
      },
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "177",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 177,
          "balance" : 48972,
          "firstname" : "Harris",
          "lastname" : "Gross",
          "age" : 40,
          "gender" : "F",
          "address" : "468 Suydam Street",
          "employer" : "Kidstock",
          "email" : "harrisgross@kidstock.com",
          "city" : "Yettem",
          "state" : "KY"
        }
      }
    ]
  }
}

bool查询中的每个must、should和must_not元素被称为一个查询子句。文档和每个在must或者should子句中指定的准则的匹配程度会影响该文档的相关性打分。分数越高,该文档就越符合你的搜索准则。默认的,ES会按照相关性打分对文档进行排序和返回。

must_not子句中的准备被用作过滤器,它影响的是这篇文档是否应该被包含在结果中,但并不会影响文档的分数。你也能通过清晰的指定任意的基于结构化数据的过滤器来包含或者不包含某些文档。

例如,下面的请求使用range过滤器来限制返回的结果是余额在$20000-$30000之间的账户:

curl http://9.25.176.228:8080/bank/_search?pretty -H 'content-type:application/json' -d '{"query":{"bool":{"must":[{"match_all":{}}],"filter":{"range":{"balance":{"gte":20000, "lte":30000}}}}}}'
{
  "took" : 19,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 217,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "49",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 49,
          "balance" : 29104,
          "firstname" : "Fulton",
          "lastname" : "Holt",
          "age" : 23,
          "gender" : "F",
          "address" : "451 Humboldt Street",
          "employer" : "Anocha",
          "email" : "fultonholt@anocha.com",
          "city" : "Sunriver",
          "state" : "RI"
        }
      },
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "102",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 102,
          "balance" : 29712,
          "firstname" : "Dena",
          "lastname" : "Olson",
          "age" : 27,
          "gender" : "F",
          "address" : "759 Newkirk Avenue",
          "employer" : "Hinway",
          "email" : "denaolson@hinway.com",
          "city" : "Choctaw",
          "state" : "NJ"
        }
      },
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "133",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 133,
          "balance" : 26135,
          "firstname" : "Deena",
          "lastname" : "Richmond",
          "age" : 36,
          "gender" : "F",
          "address" : "646 Underhill Avenue",
          "employer" : "Sunclipse",
          "email" : "deenarichmond@sunclipse.com",
          "city" : "Austinburg",
          "state" : "SC"
        }
      },
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "140",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 140,
          "balance" : 26696,
          "firstname" : "Cotton",
          "lastname" : "Christensen",
          "age" : 32,
          "gender" : "M",
          "address" : "878 Schermerhorn Street",
          "employer" : "Prowaste",
          "email" : "cottonchristensen@prowaste.com",
          "city" : "Mayfair",
          "state" : "LA"
        }
      },
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "203",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 203,
          "balance" : 21890,
          "firstname" : "Eve",
          "lastname" : "Wyatt",
          "age" : 33,
          "gender" : "M",
          "address" : "435 Furman Street",
          "employer" : "Assitia",
          "email" : "evewyatt@assitia.com",
          "city" : "Jamestown",
          "state" : "MN"
        }
      },
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "239",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 239,
          "balance" : 25719,
          "firstname" : "Chang",
          "lastname" : "Boyer",
          "age" : 36,
          "gender" : "M",
          "address" : "895 Brigham Street",
          "employer" : "Qaboos",
          "email" : "changboyer@qaboos.com",
          "city" : "Belgreen",
          "state" : "NH"
        }
      },
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "241",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 241,
          "balance" : 25379,
          "firstname" : "Schroeder",
          "lastname" : "Harrington",
          "age" : 26,
          "gender" : "M",
          "address" : "610 Tapscott Avenue",
          "employer" : "Otherway",
          "email" : "schroederharrington@otherway.com",
          "city" : "Ebro",
          "state" : "TX"
        }
      },
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "246",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 246,
          "balance" : 28405,
          "firstname" : "Katheryn",
          "lastname" : "Foster",
          "age" : 21,
          "gender" : "F",
          "address" : "259 Kane Street",
          "employer" : "Quantalia",
          "email" : "katherynfoster@quantalia.com",
          "city" : "Bath",
          "state" : "TX"
        }
      },
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "253",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 253,
          "balance" : 20240,
          "firstname" : "Melissa",
          "lastname" : "Gould",
          "age" : 31,
          "gender" : "M",
          "address" : "440 Fuller Place",
          "employer" : "Buzzopia",
          "email" : "melissagould@buzzopia.com",
          "city" : "Lumberton",
          "state" : "MD"
        }
      },
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "277",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 277,
          "balance" : 29564,
          "firstname" : "Romero",
          "lastname" : "Lott",
          "age" : 31,
          "gender" : "M",
          "address" : "456 Danforth Street",
          "employer" : "Plasto",
          "email" : "romerolott@plasto.com",
          "city" : "Vincent",
          "state" : "VT"
        }
      }
    ]
  }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值