[toc]


实际007-elasticsearch查问之2-Request Body与DSL查问

1. 查问语句通过HTTP Request Body发送给ES

POST movies/_search?ignore_unavailable=true{  "profile": "true",  "query": {    "match_all": {}  }}

2. 分页:from+size

POST movies/_search?ignore_unavailable=true{  "profile": "true",  "from": 0,  "size": 2,   "query": {    "match_all": {}  }}

3. 排序:sort

POST movies/_search?ignore_unavailable=true{  "profile": "true",  "from": 0,  "size": 5,  "sort": [    {      "year": "desc"    },    {      "title.keyword": "desc"    }  ],  "query": {    "match_all": {}  }}

4. source字段过滤:只返回须要的列

POST movies/_search?ignore_unavailable=true{  "profile": "true",  "_source": [    "id",    "title",    "year"  ],  "from": 0,  "size": 5,  "sort": [    {      "year": "desc"    },    {      "title.keyword": "desc"    }  ],  "query": {    "match_all": {}  }}
反对通配符*: _"source": [ "name*", "desc\*" ]

5. script_fields脚本字段:自加工后果

POST movies/_search{  "from": 0,  "size": 5,  "sort": [    {      "year": {        "order": "desc"      }    },    {      "title.keyword": {        "order": "desc"      }    }  ],  "script_fields": {    "year_and_title": {      "script": {        "lang": "painless",        "source": "doc['year'].value + '->' + doc['title.keyword'].value"      }    }  },  "query": {    "match_all": {}  }}

title=When We First Met; year=2018;

year_and_title=year->title

然而如同_source数据没了:

{  "took" : 10,  "timed_out" : false,  "_shards" : {    "total" : 1,    "successful" : 1,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 9743,      "relation" : "eq"    },    "max_score" : null,    "hits" : [      {        "_index" : "movies",        "_type" : "_doc",        "_id" : "187717",        "_score" : null,        "fields" : {          "year_and_title" : [            "2018->Won't You Be My Neighbor?"          ]        },        "sort" : [          2018,          "Won't You Be My Neighbor?"        ]      },      {        "_index" : "movies",        "_type" : "_doc",        "_id" : "184015",        "_score" : null,        "fields" : {          "year_and_title" : [            "2018->When We First Met"          ]        },        "sort" : [          2018,          "When We First Met"        ]      },      {        "_index" : "movies",        "_type" : "_doc",        "_id" : "184471",        "_score" : null,        "fields" : {          "year_and_title" : [            "2018->Tomb Raider"          ]        },        "sort" : [          2018,          "Tomb Raider"        ]      },      {        "_index" : "movies",        "_type" : "_doc",        "_id" : "183959",        "_score" : null,        "fields" : {          "year_and_title" : [            "2018->Tom Segura: Disgraceful"          ]        },        "sort" : [          2018,          "Tom Segura: Disgraceful"        ]      },      {        "_index" : "movies",        "_type" : "_doc",        "_id" : "188833",        "_score" : null,        "fields" : {          "year_and_title" : [            "2018->The Man Who Killed Don Quixote"          ]        },        "sort" : [          2018,          "The Man Who Killed Don Quixote"        ]      }    ]  }}

6. match查问表达式: TermQuery

6.1 TermQuery

POST movies/_search{  "query": {    "match": {      "title": "beautiful mind"    }  }}

后果: 蕴含 beautiful 的(16条), 或 蕴含 mind 的(5条),共(20条):

{  "took" : 0,  "timed_out" : false,  "_shards" : {    "total" : 1,    "successful" : 1,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 20,      "relation" : "eq"    },    "max_score" : 13.687479,    "hits" : [      {        "_index" : "movies",        "_type" : "_doc",        "_id" : "4995",        "_score" : 13.687479,        "_source" : {          "id" : "4995",          "genre" : [            "Drama",            "Romance"          ],          "title" : "Beautiful Mind, A",          "@version" : "1",          "year" : 2001        }      },      {        "_index" : "movies",        "_type" : "_doc",        "_id" : "3912",        "_score" : 8.723258,        "_source" : {          "id" : "3912",          "genre" : [            "Comedy",            "Drama"          ],          "title" : "Beautiful",          "@version" : "1",          "year" : 2000        }      },      {        "_index" : "movies",        "_type" : "_doc",        "_id" : "47404",        "_score" : 8.576847,        "_source" : {          "id" : "47404",          "genre" : [            "Adventure",            "Animation",            "Comedy",            "Fantasy",            "Romance",            "Sci-Fi"          ],          "title" : "Mind Game",          "@version" : "1",          "year" : 2004        }      },      {        "_index" : "movies",        "_type" : "_doc",        "_id" : "1046",        "_score" : 7.317063,        "_source" : {          "id" : "1046",          "genre" : [            "Drama",            "Romance"          ],          "title" : "Beautiful Thing",          "@version" : "1",          "year" : 1996        }      },      {        "_index" : "movies",        "_type" : "_doc",        "_id" : "94",        "_score" : 7.317063,        "_source" : {          "id" : "94",          "genre" : [            "Comedy",            "Drama",            "Romance"          ],          "title" : "Beautiful Girls",          "@version" : "1",          "year" : 1996        }      },      {        "_index" : "movies",        "_type" : "_doc",        "_id" : "4242",        "_score" : 7.317063,        "_source" : {          "id" : "4242",          "genre" : [            "Comedy",            "Crime",            "Drama",            "Thriller"          ],          "title" : "Beautiful Creatures",          "@version" : "1",          "year" : 2000        }      },      {        "_index" : "movies",        "_type" : "_doc",        "_id" : "4372",        "_score" : 7.317063,        "_source" : {          "id" : "4372",          "genre" : [            "Drama",            "Romance"          ],          "title" : "Crazy/Beautiful",          "@version" : "1",          "year" : 2001        }      },      {        "_index" : "movies",        "_type" : "_doc",        "_id" : "3302",        "_score" : 7.317063,        "_source" : {          "id" : "3302",          "genre" : [            "Comedy"          ],          "title" : "Beautiful People",          "@version" : "1",          "year" : 1999        }      },      {        "_index" : "movies",        "_type" : "_doc",        "_id" : "90353",        "_score" : 7.317063,        "_source" : {          "id" : "90353",          "genre" : [            "Drama"          ],          "title" : "Beautiful Boy",          "@version" : "1",          "year" : 2010        }      },      {        "_index" : "movies",        "_type" : "_doc",        "_id" : "100487",        "_score" : 7.317063,        "_source" : {          "id" : "100487",          "genre" : [            "Drama",            "Fantasy",            "Romance"          ],          "title" : "Beautiful Creatures",          "@version" : "1",          "year" : 2013        }      }    ]  }}

6.2 TermQuery+AND/OR/NOT

POST movies/_search{  "query": {    "match": {      "title": {        "query": "beautiful mind",        "operator": "and"      }    }  }}
  • operator: and/or/not 对"query"的词条做限度逻辑关系,加上and后,便只有一个记录:
{  "took" : 0,  "timed_out" : false,  "_shards" : {    "total" : 1,    "successful" : 1,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 1,      "relation" : "eq"    },    "max_score" : 13.687479,    "hits" : [      {        "_index" : "movies",        "_type" : "_doc",        "_id" : "4995",        "_score" : 13.687479,        "_source" : {          "id" : "4995",          "genre" : [            "Drama",            "Romance"          ],          "title" : "Beautiful Mind, A",          "@version" : "1",          "year" : 2001        }      }    ]  }}

7.match_phrase查问表达式:PhraseQuery

7.1 PhraseQuery

POST movies/_search{  "query": {    "match_phrase": {      "title": {        "query": "beautiful mind"      }    }  }}#// 或者上面模式都可POST movies/_search{  "query": {    "match_phrase": {      "title": "beautiful mind"    }  }}

后果就只有一条, 因为是PhraseQuery: 既要求分词,又要求程序;

7.2 PhraseQuery+slop

  • slop /sl>p/ n.脏水,淡而无味的半流质食物;泔水
POST movies/_search{  "query": {    "match_phrase": {      "title": {        "query": "friday part",        "slop": 2      }    }  }}

slop=2, 就是容许 fridaypart 两头 掺和 2个杂词(不相干的任意词)