当前位置:   article > 正文

postfilter_elasticsearch中的post_filter

es postfilter代码怎么写

查询state为MD的数据,并且根据查询后的结果,以城市city聚合,显示查询结果和聚合结果:

查询1:

POST /bank/_search

{

"size": 30,

"query": {

"bool": {

"filter": {

"term": {

"state.keyword": "MD"

}

}

}

},

"aggs": {

"cities": {

"terms": {

"size": 30,

"field": "city.keyword"

}

}

}

}

查询结果如下:

结果1:

{

"took" : 7,

"timed_out" : false,

"_shards" : {

"total" : 1,

"successful" : 1,

"skipped" : 0,

"failed" : 0

},

"hits" : {

"total" : {

"value" : 28,

"relation" : "eq"

},

"max_score" : 0.0,

"hits" : [

{

"_index" : "bank",

"_type" : "_doc",

"_id" : "18",

"_score" : 0.0,

"_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"

}

},

{

"_index" : "bank",

"_type" : "_doc",

"_id" : "126",

"_score" : 0.0,

"_source" : {

"account_number" : 126,

"balance" : 3607,

"firstname" : "Effie",

"lastname" : "Gates",

"age" : 39,

"gender" : "F",

"address" : "620 National Drive",

"employer" : "Digitalus",

"email" : "effiegates@digitalus.com",

"city" : "Blodgett",

"state" : "MD"

}

},

{

"_index" : "bank",

"_type" : "_doc",

"_id" : "157",

"_score" : 0.0,

"_source" : {

"account_number" : 157,

"balance" : 39868,

"firstname" : "Claudia",

"lastname" : "Terry",

"age" : 20,

"gender" : "F",

"address" : "132 Gunnison Court",

"employer" : "Lumbrex",

"email" : "claudiaterry@lumbrex.com",

"city" : "Castleton",

"state" : "MD"

}

},

{

"_index" : "bank",

"_type" : "_doc",

"_id" : "253",

"_score" : 0.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" : "323",

"_score" : 0.0,

"_source" : {

"account_number" : 323,

"balance" : 42230,

"firstname" : "Chelsea",

"lastname" : "Gamble",

"age" : 34,

"gender" : "F",

"address" : "356 Dare Court",

"employer" : "Isosphere",

"email" : "chelseagamble@isosphere.com",

"city" : "Dundee",

"state" : "MD"

}

},

{

"_index" : "bank",

"_type" : "_doc",

"_id" : "556",

"_score" : 0.0,

"_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" : "575",

"_score" : 0.0,

"_source" : {

"account_number" : 575,

"balance" : 12588,

"firstname" : "Buchanan",

"lastname" : "Pope",

"age" : 39,

"gender" : "M",

"address" : "581 Sumner Place",

"employer" : "Stucco",

"email" : "buchananpope@stucco.com",

"city" : "Ellerslie",

"state" : "MD"

}

},

{

"_index" : "bank",

"_type" : "_doc",

"_id" : "266",

"_score" : 0.0,

"_source" : {

"account_number" : 266,

"balance" : 2777,

"firstname" : "Monique",

"lastname" : "Conner",

"age" : 35,

"gender" : "F",

"address" : "489 Metrotech Courtr",

"employer" : "Flotonic",

"email" : "moniqueconner@flotonic.com",

"city" : "Retsof",

"state" : "MD"

}

},

{

"_index" : "bank",

"_type" : "_doc",

"_id" : "913",

"_score" : 0.0,

"_source" : {

"account_number" : 913,

"balance" : 47657,

"firstname" : "Margery",

"lastname" : "Monroe",

"age" : 25,

"gender" : "M",

"address" : "941 Fanchon Place",

"employer" : "Exerta",

"email" : "margerymonroe@exerta.com",

"city" : "Bannock",

"state" : "MD"

}

},

{

"_index" : "bank",

"_type" : "_doc",

"_id" : "268",

"_score" : 0.0,

"_source" : {

"account_number" : 268,

"balance" : 20925,

"firstname" : "Avis",

"lastname" : "Blackwell",

"age" : 36,

"gender" : "M",

"address" : "569 Jerome Avenue",

"employer" : "Magnina",

"email" : "avisblackwell@magnina.com",

"city" : "Bethany",

"state" : "MD"

}

},

{

"_index" : "bank",

"_type" : "_doc",

"_id" : "326",

"_score" : 0.0,

"_source" : {

"account_number" : 326,

"balance" : 9692,

"firstname" : "Pearl",

"lastname" : "Reese",

"age" : 30,

"gender" : "F",

"address" : "451 Colonial Court",

"employer" : "Accruex",

"email" : "pearlreese@accruex.com",

"city" : "Westmoreland",

"state" : "MD"

}

},

{

"_index" : "bank",

"_type" : "_doc",

"_id" : "338",

"_score" : 0.0,

"_source" : {

"account_number" : 338,

"balance" : 6969,

"firstname" : "Pierce",

"lastname" : "Lawrence",

"age" : 35,

"gender" : "M",

"address" : "318 Gallatin Place",

"employer" : "Lunchpad",

"email" : "piercelawrence@lunchpad.com",

"city" : "Iola",

"state" : "MD"

}

},

{

"_index" : "bank",

"_type" : "_doc",

"_id" : "617",

"_score" : 0.0,

"_source" : {

"account_number" : 617,

"balance" : 35445,

"firstname" : "Kitty",

"lastname" : "Cooley",

"age" : 22,

"gender" : "M",

"address" : "788 Seagate Avenue",

"employer" : "Ultrimax",

"email" : "kittycooley@ultrimax.com",

"city" : "Clarktown",

"state" : "MD"

}

},

{

"_index" : "bank",

"_type" : "_doc",

"_id" : "662",

"_score" : 0.0,

"_source" : {

"account_number" : 662,

"balance" : 10138,

"firstname" : "Daisy",

"lastname" : "Burnett",

"age" : 33,

"gender" : "M",

"address" : "114 Norman Avenue",

"employer" : "Liquicom",

"email" : "daisyburnett@liquicom.com",

"city" : "Grahamtown",

"state" : "MD"

}

},

{

"_index" : "bank",

"_type" : "_doc",

"_id" : "939",

"_score" : 0.0,

"_source" : {

"account_number" : 939,

"balance" : 31228,

"firstname" : "Hodges",

"lastname" : "Massey",

"age" : 37,

"gender" : "F",

"address" : "431 Dahl Court",

"employer" : "Kegular",

"email" : "hodgesmassey@kegular.com",

"city" : "Katonah",

"state" : "MD"

}

},

{

"_index" : "bank",

"_type" : "_doc",

"_id" : "965",

"_score" : 0.0,

"_source" : {

"account_number" : 965,

"balance" : 21882,

"firstname" : "Patrica",

"lastname" : "Melton",

"age" : 28,

"gender" : "M",

"address" : "141 Rodney Street",

"employer" : "Flexigen",

"email" : "patricamelton@flexigen.com",

"city" : "Klagetoh",

"state" : "MD"

}

},

{

"_index" : "bank",

"_type" : "_doc",

"_id" : "977",

"_score" : 0.0,

"_source" : {

"account_number" : 977,

"balance" : 6744,

"firstname" : "Rodgers",

"lastname" : "Mccray",

"age" : 21,

"gender" : "F",

"address" : "612 Duryea Place",

"employer" : "Papricut",

"email" : "rodgersmccray@papricut.com",

"city" : "Marenisco",

"state" : "MD"

}

},

{

"_index" : "bank",

"_type" : "_doc",

"_id" : "454",

"_score" : 0.0,

"_source" : {

"account_number" : 454,

"balance" : 31687,

"firstname" : "Alicia",

"lastname" : "Rollins",

"age" : 22,

"gender" : "F",

"address" : "483 Verona Place",

"employer" : "Boilcat",

"email" : "aliciarollins@boilcat.com",

"city" : "Lutsen",

"state" : "MD"

}

},

{

"_index" : "bank",

"_type" : "_doc",

"_id" : "543",

"_score" : 0.0,

"_source" : {

"account_number" : 543,

"balance" : 48022,

"firstname" : "Marina",

"lastname" : "Rasmussen",

"age" : 31,

"gender" : "M",

"address" : "446 Love Lane",

"employer" : "Crustatia",

"email" : "marinarasmussen@crustatia.com",

"city" : "Statenville",

"state" : "MD"

}

},

{

"_index" : "bank",

"_type" : "_doc",

"_id" : "644",

"_score" : 0.0,

"_source" : {

"account_number" : 644,

"balance" : 44021,

"firstname" : "Etta",

"lastname" : "Miller",

"age" : 21,

"gender" : "F",

"address" : "376 Lawton Street",

"employer" : "Bluegrain",

"email" : "ettamiller@bluegrain.com",

"city" : "Baker",

"state" : "MD"

}

},

{

"_index" : "bank",

"_type" : "_doc",

"_id" : "675",

"_score" : 0.0,

"_source" : {

"account_number" : 675,

"balance" : 36102,

"firstname" : "Fisher",

"lastname" : "Shepard",

"age" : 27,

"gender" : "F",

"address" : "859 Varick Street",

"employer" : "Qot",

"email" : "fishershepard@qot.com",

"city" : "Diaperville",

"state" : "MD"

}

},

{

"_index" : "bank",

"_type" : "_doc",

"_id" : "930",

"_score" : 0.0,

"_source" : {

"account_number" : 930,

"balance" : 47257,

"firstname" : "Kinney",

"lastname" : "Lawson",

"age" : 39,

"gender" : "M",

"address" : "501 Raleigh Place",

"employer" : "Neptide",

"email" : "kinneylawson@neptide.com",

"city" : "Deltaville",

"state" : "MD"

}

},

{

"_index" : "bank",

"_type" : "_doc",

"_id" : "954",

"_score" : 0.0,

"_source" : {

"account_number" : 954,

"balance" : 49404,

"firstname" : "Jenna",

"lastname" : "Martin",

"age" : 22,

"gender" : "M",

"address" : "688 Hart Street",

"employer" : "Zinca",

"email" : "jennamartin@zinca.com",

"city" : "Oasis",

"state" : "MD"

}

},

{

"_index" : "bank",

"_type" : "_doc",

"_id" : "200",

"_score" : 0.0,

"_source" : {

"account_number" : 200,

"balance" : 26210,

"firstname" : "Teri",

"lastname" : "Hester",

"age" : 39,

"gender" : "M",

"address" : "653 Abbey Court",

"employer" : "Electonic",

"email" : "terihester@electonic.com",

"city" : "Martell",

"state" : "MD"

}

},

{

"_index" : "bank",

"_type" : "_doc",

"_id" : "255",

"_score" : 0.0,

"_source" : {

"account_number" : 255,

"balance" : 49339,

"firstname" : "Iva",

"lastname" : "Rivers",

"age" : 38,

"gender" : "M",

"address" : "470 Rost Place",

"employer" : "Mantrix",

"email" : "ivarivers@mantrix.com",

"city" : "Disautel",

"state" : "MD"

}

},

{

"_index" : "bank",

"_type" : "_doc",

"_id" : "527",

"_score" : 0.0,

"_source" : {

"account_number" : 527,

"balance" : 2028,

"firstname" : "Carver",

"lastname" : "Peters",

"age" : 35,

"gender" : "M",

"address" : "816 Victor Road",

"employer" : "Housedown",

"email" : "carverpeters@housedown.com",

"city" : "Nadine",

"state" : "MD"

}

},

{

"_index" : "bank",

"_type" : "_doc",

"_id" : "818",

"_score" : 0.0,

"_source" : {

"account_number" : 818,

"balance" : 24433,

"firstname" : "Espinoza",

"lastname" : "Petersen",

"age" : 26,

"gender" : "M",

"address" : "641 Glenwood Road",

"employer" : "Futurity",

"email" : "espinozapetersen@futurity.com",

"city" : "Floriston",

"state" : "MD"

}

},

{

"_index" : "bank",

"_type" : "_doc",

"_id" : "952",

"_score" : 0.0,

"_source" : {

"account_number" : 952,

"balance" : 21430,

"firstname" : "Angelique",

"lastname" : "Weeks",

"age" : 33,

"gender" : "M",

"address" : "659 Reeve Place",

"employer" : "Exodoc",

"email" : "angeliqueweeks@exodoc.com",

"city" : "Turpin",

"state" : "MD"

}

}

]

},

"aggregations" : {

"cities" : {

"doc_count_error_upper_bound" : 0,

"sum_other_doc_count" : 0,

"buckets" : [

{

"key" : "Baker",

"doc_count" : 1

},

{

"key" : "Bannock",

"doc_count" : 1

},

{

"key" : "Bethany",

"doc_count" : 1

},

{

"key" : "Blodgett",

"doc_count" : 1

},

{

"key" : "Castleton",

"doc_count" : 1

},

{

"key" : "Clarktown",

"doc_count" : 1

},

{

"key" : "Deltaville",

"doc_count" : 1

},

{

"key" : "Diaperville",

"doc_count" : 1

},

{

"key" : "Disautel",

"doc_count" : 1

},

{

"key" : "Dundee",

"doc_count" : 1

},

{

"key" : "Ellerslie",

"doc_count" : 1

},

{

"key" : "Floriston",

"doc_count" : 1

},

{

"key" : "Fulford",

"doc_count" : 1

},

{

"key" : "Grahamtown",

"doc_count" : 1

},

{

"key" : "Iola",

"doc_count" : 1

},

{

"key" : "Katonah",

"doc_count" : 1

},

{

"key" : "Klagetoh",

"doc_count" : 1

},

{

"key" : "Lumberton",

"doc_count" : 1

},

{

"key" : "Lutsen",

"doc_count" : 1

},

{

"key" : "Marenisco",

"doc_count" : 1

},

{

"key" : "Martell",

"doc_count" : 1

},

{

"key" : "Nadine",

"doc_count" : 1

},

{

"key" : "Oasis",

"doc_count" : 1

},

{

"key" : "Orick",

"doc_count" : 1

},

{

"key" : "Retsof",

"doc_count" : 1

},

{

"key" : "Statenville",

"doc_count" : 1

},

{

"key" : "Turpin",

"doc_count" : 1

},

{

"key" : "Westmoreland",

"doc_count" : 1

}

]

}

}

}

接下来增加post_filter,进行第二次查询

查询2:

POST /bank/_search

{

"size": 30,

"query": {

"bool": {

"filter": {

"term": {

"state.keyword": "MD"

}

}

}

},

"aggs": {

"cities": {

"terms": {

"size": 30,

"field": "city.keyword"

}

}

},

"post_filter": {

"term": {

"city.keyword": "Ellerslie"

}

}

}

查询结果

结果2:

{

"took" : 4,

"timed_out" : false,

"_shards" : {

"total" : 1,

"successful" : 1,

"skipped" : 0,

"failed" : 0

},

"hits" : {

"total" : {

"value" : 1,

"relation" : "eq"

},

"max_score" : 0.0,

"hits" : [

{

"_index" : "bank",

"_type" : "_doc",

"_id" : "575",

"_score" : 0.0,

"_source" : {

"account_number" : 575,

"balance" : 12588,

"firstname" : "Buchanan",

"lastname" : "Pope",

"age" : 39,

"gender" : "M",

"address" : "581 Sumner Place",

"employer" : "Stucco",

"email" : "buchananpope@stucco.com",

"city" : "Ellerslie",

"state" : "MD"

}

}

]

},

"aggregations" : {

"cities" : {

"doc_count_error_upper_bound" : 0,

"sum_other_doc_count" : 0,

"buckets" : [

{

"key" : "Baker",

"doc_count" : 1

},

{

"key" : "Bannock",

"doc_count" : 1

},

{

"key" : "Bethany",

"doc_count" : 1

},

{

"key" : "Blodgett",

"doc_count" : 1

},

{

"key" : "Castleton",

"doc_count" : 1

},

{

"key" : "Clarktown",

"doc_count" : 1

},

{

"key" : "Deltaville",

"doc_count" : 1

},

{

"key" : "Diaperville",

"doc_count" : 1

},

{

"key" : "Disautel",

"doc_count" : 1

},

{

"key" : "Dundee",

"doc_count" : 1

},

{

"key" : "Ellerslie",

"doc_count" : 1

},

{

"key" : "Floriston",

"doc_count" : 1

},

{

"key" : "Fulford",

"doc_count" : 1

},

{

"key" : "Grahamtown",

"doc_count" : 1

},

{

"key" : "Iola",

"doc_count" : 1

},

{

"key" : "Katonah",

"doc_count" : 1

},

{

"key" : "Klagetoh",

"doc_count" : 1

},

{

"key" : "Lumberton",

"doc_count" : 1

},

{

"key" : "Lutsen",

"doc_count" : 1

},

{

"key" : "Marenisco",

"doc_count" : 1

},

{

"key" : "Martell",

"doc_count" : 1

},

{

"key" : "Nadine",

"doc_count" : 1

},

{

"key" : "Oasis",

"doc_count" : 1

},

{

"key" : "Orick",

"doc_count" : 1

},

{

"key" : "Retsof",

"doc_count" : 1

},

{

"key" : "Statenville",

"doc_count" : 1

},

{

"key" : "Turpin",

"doc_count" : 1

},

{

"key" : "Westmoreland",

"doc_count" : 1

}

]

}

}

}

很明显,查询1与查询2的不同在于,查询2比查询1多了如下代码:

"post_filter": {

"term": {

"city.keyword": "Ellerslie"

}

}

导致的结果是,查询1出现在hits中的结果有28条,而查询2出现在hits中的结果只有1条,对比两个查询实例,我们可以了解到:

查询1的查询结果只经过一次过滤(根据state.keyword = "MD"过滤),而查询2的查询结果,经过了两次过滤(post_filter贡献了第二次过滤)。但是两次查询后的聚合结果都没有发生变化。也就是说,查询2中的第一次查询后的结果,可以用于聚合,生成聚合结果后,我们可以根据相应的需求(如:city.keyword = "Ellerslie"),对一次查询得到的结果用post_filter进行二次过滤。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/817744
推荐阅读
相关标签
  

闽ICP备14008679号