文章目录
  1. 1. ElasticSearch高级查询
  2. 2. 运行环境
  3. 3. 准备测试数据
  4. 4. 子条件查询
    1. 4.1. Query Context
    2. 4.2. Filter Context
  5. 5. 复合条件查询
    1. 5.1. 固定分数查询,这里的固定分数指的就是上面的匹配度
    2. 5.2. 指定固定分数:
    3. 5.3. 布尔查询,使用bool关键字

ElasticSearch高级查询

高级查询包含:子条件查询、复合条件查询

运行环境

1
2
3
4
{
"OS":"centos6"
"ES_version":5.5.2"
}

准备测试数据

向ES添加如下数据

测试数据

子条件查询

特定字段查询所指特定值,子查询又分为:Query Context、Filter Context

Query Context

概念:在查询过程中,除了判断文档是否满足查询条件外,ES还会计算一个_score来标识匹配的程度,旨在判断目标文档和查询条件匹配的程度。

Query Context有如下的常用查询:

  • 全文本查询 针对文本类型查询

    • 模糊查询

      请求地址:http://192.168.137.8:9200/book/_search

      请求方式: POST

      请求数据:

      1
      2
      3
      4
      5
      6
      7
      {
      "query":{
      "match":{ //模糊匹配关键字
      "title":"ElasticSearch" //匹配条件 查询title包含ElasticSearch
      }
      }
      }

      响应结果,title字段中包含ElasticSearch的全部查询出来了:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      60
      61
      62
      63
      {
      "took": 58,
      "timed_out": false,
      "_shards": {
      "total": 3,
      "successful": 3,
      "failed": 0
      },
      "hits": {
      "total": 4,
      "max_score": 1.3555917,
      "hits": [
      {
      "_index": "book",
      "_type": "novel",
      "_id": "4",
      "_score": 1.3555917,
      "_source": {
      "author": "李四",
      "title": "ElasticSearch大法好",
      "wordcount": 1000,
      "publish_date": "2010-10-01"
      }
      },
      {
      "_index": "book",
      "_type": "novel",
      "_id": "9",
      "_score": 1.1001158,
      "_source": {
      "author": "很胖的瓦力",
      "title": "ElasticSearch精通",
      "wordcount": 3000,
      "publish_date": "2017-08-15"
      }
      },
      {
      "_index": "book",
      "_type": "novel",
      "_id": "8",
      "_score": 0.19856805,
      "_source": {
      "author": "瓦力",
      "title": "ElasticSearch入门",
      "wordcount": 3000,
      "publish_date": "2017-08-20"
      }
      },
      {
      "_index": "book",
      "_type": "novel",
      "_id": "12",
      "_score": 0.155468,
      "_source": {
      "title": "瓦力教我们学ElasticSearch",
      "author": "瓦力",
      "wordcount": 1000,
      "publish_date": "2017-08-01"
      }
      }
      ]
      }
      }
    • 习语匹配查询

      请求地址和请求方式不变,请求参数修改如下:

      1
      2
      3
      4
      5
      6
      7
      {
      "query":{
      "match_phrase":{ //习语匹配关键字
      "title":"ElasticSearch入门" //查询条件
      }
      }
      }

      响应结果如下,只有title字段包含ElasticSearch入门:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      {
      "took": 36,
      "timed_out": false,
      "_shards": {
      "total": 3,
      "successful": 3,
      "failed": 0
      },
      "hits": {
      "total": 1,
      "max_score": 0.7594808,
      "hits": [
      {
      "_index": "book",
      "_type": "novel",
      "_id": "8",
      "_score": 0.7594808,
      "_source": {
      "author": "瓦力",
      "title": "ElasticSearch入门",
      "wordcount": 3000,
      "publish_date": "2017-08-20"
      }
      }
      ]
      }
      }

      通过下面的例子可以更好的理解习语匹配:

      请求地址和请求方式不变,请求数据如下:

      1
      2
      3
      4
      5
      6
      7
      {
      "query":{
      "match":{ //模糊匹配
      "title":"ElasticSearch入门"
      }
      }
      }

      响应结果如下,ES将查询条件ElasticSearch入门拆分为两个关键字ElasticSearch入门,然后匹配所有title包含这些关键字的记录:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      60
      61
      62
      63
      64
      65
      66
      67
      68
      69
      70
      71
      72
      73
      74
      75
      76
      77
      78
      79
      80
      81
      82
      83
      84
      85
      86
      87
      {
      "took": 44,
      "timed_out": false,
      "_shards": {
      "total": 3,
      "successful": 3,
      "failed": 0
      },
      "hits": {
      "total": 6,
      "max_score": 2.7111833,
      "hits": [
      {
      "_index": "book",
      "_type": "novel",
      "_id": "2",
      "_score": 2.7111833,
      "_source": {
      "author": "王五",
      "title": "Java入门",
      "wordcount": 2000,
      "publish_date": "2010-10-01"
      }
      },
      {
      "_index": "book",
      "_type": "novel",
      "_id": "3",
      "_score": 2.2002316,
      "_source": {
      "author": "张三",
      "title": "Python入门",
      "wordcount": 2000,
      "publish_date": "2005-10-01"
      }
      },
      {
      "_index": "book",
      "_type": "novel",
      "_id": "8",
      "_score": 1.7083936,
      "_source": {
      "author": "瓦力",
      "title": "ElasticSearch入门",
      "wordcount": 3000,
      "publish_date": "2017-08-20"
      }
      },
      {
      "_index": "book",
      "_type": "novel",
      "_id": "4",
      "_score": 1.3555917,
      "_source": {
      "author": "李四",
      "title": "ElasticSearch大法好",
      "wordcount": 1000,
      "publish_date": "2010-10-01"
      }
      },
      {
      "_index": "book",
      "_type": "novel",
      "_id": "9",
      "_score": 1.1001158,
      "_source": {
      "author": "很胖的瓦力",
      "title": "ElasticSearch精通",
      "wordcount": 3000,
      "publish_date": "2017-08-15"
      }
      },
      {
      "_index": "book",
      "_type": "novel",
      "_id": "12",
      "_score": 0.155468,
      "_source": {
      "title": "瓦力教我们学ElasticSearch",
      "author": "瓦力",
      "wordcount": 1000,
      "publish_date": "2017-08-01"
      }
      }
      ]
      }
      }
    • 多字段查询

      请求地址和请求方式不变,请求数据如下:

      1
      2
      3
      4
      5
      6
      7
      8
      {
      "query":{
      "multi_match":{ //多字段查询关键字
      "query":"瓦力", //查询条件
      "fields":["author","title"] //查询字段
      }
      }
      }

      响应结果如下,author字段等于瓦力title字段包含瓦力

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      {
      "took": 132,
      "timed_out": false,
      "_shards": {
      "total": 3,
      "successful": 3,
      "failed": 0
      },
      "hits": {
      "total": 2,
      "max_score": 1.1821114,
      "hits": [
      {
      "_index": "book",
      "_type": "novel",
      "_id": "12",
      "_score": 1.1821114,
      "_source": {
      "title": "瓦力教我们学ElasticSearch",
      "author": "瓦力",
      "wordcount": 1000,
      "publish_date": "2017-08-01"
      }
      },
      {
      "_index": "book",
      "_type": "novel",
      "_id": "8",
      "_score": 0.18232156,
      "_source": {
      "author": "瓦力",
      "title": "ElasticSearch入门",
      "wordcount": 3000,
      "publish_date": "2017-08-20"
      }
      }
      ]
      }
      }
    • 语法查询

      请求地址和请求方式不变,请求数据如下:

      1
      2
      3
      4
      5
      6
      7
      {
      "query":{
      "query_string":{ //语法查询关键字
      "query":"ElasticSearch AND 大法" //查询条件包含ElasticSearch和大法的记录,AND区分大小写
      }
      }
      }

      响应结果如下:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      {
      "took": 25,
      "timed_out": false,
      "_shards": {
      "total": 3,
      "successful": 3,
      "failed": 0
      },
      "hits": {
      "total": 1,
      "max_score": 4.444701,
      "hits": [
      {
      "_index": "book",
      "_type": "novel",
      "_id": "4",
      "_score": 4.444701,
      "_source": {
      "author": "李四",
      "title": "ElasticSearch大法好",
      "wordcount": 1000,
      "publish_date": "2010-10-01"
      }
      }
      ]
      }
      }

      还支持如下的查询,请求格式如下:

      1
      2
      3
      4
      5
      6
      7
      {
      "query":{
      "query_string":{
      "query":"(ElasticSearch AND 大法) OR (入门)"
      }
      }
      }

      响应结果如下:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      60
      61
      62
      63
      {
      "took": 59,
      "timed_out": false,
      "_shards": {
      "total": 3,
      "successful": 3,
      "failed": 0
      },
      "hits": {
      "total": 4,
      "max_score": 4.444701,
      "hits": [
      {
      "_index": "book",
      "_type": "novel",
      "_id": "4",
      "_score": 4.444701,
      "_source": {
      "author": "李四",
      "title": "ElasticSearch大法好",
      "wordcount": 1000,
      "publish_date": "2010-10-01"
      }
      },
      {
      "_index": "book",
      "_type": "novel",
      "_id": "2",
      "_score": 2.9631343,
      "_source": {
      "author": "李三",
      "title": "Java入门",
      "wordcount": 2000,
      "publish_date": "2010-10-01"
      }
      },
      {
      "_index": "book",
      "_type": "novel",
      "_id": "3",
      "_score": 2.4089072,
      "_source": {
      "author": "张三",
      "title": "Python入门",
      "wordcount": 2000,
      "publish_date": "2005-10-01"
      }
      },
      {
      "_index": "book",
      "_type": "novel",
      "_id": "8",
      "_score": 1.4266168,
      "_source": {
      "author": "瓦力",
      "title": "ElasticSearch入门",
      "wordcount": 3000,
      "publish_date": "2017-08-20"
      }
      }
      ]
      }
      }

      还支持多字段的查询,请求数据如下:

      1
      2
      3
      4
      5
      6
      7
      8
      {
      "query":{
      "query_string":{
      "query":"李四 OR ElasticSearch", //查询author和title字段包含 李四、ElasticSearch的记录
      "fields":["title","author"]
      }
      }
      }

      响应结果如下:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      60
      61
      62
      63
      {
      "took": 14,
      "timed_out": false,
      "_shards": {
      "total": 3,
      "successful": 3,
      "failed": 0
      },
      "hits": {
      "total": 4,
      "max_score": 2.8960366,
      "hits": [
      {
      "_index": "book",
      "_type": "novel",
      "_id": "4",
      "_score": 2.8960366,
      "_source": {
      "author": "李四",
      "title": "ElasticSearch大法好",
      "wordcount": 1000,
      "publish_date": "2010-10-01"
      }
      },
      {
      "_index": "book",
      "_type": "novel",
      "_id": "9",
      "_score": 1.1001158,
      "_source": {
      "author": "很胖的瓦力",
      "title": "ElasticSearch精通",
      "wordcount": 3000,
      "publish_date": "2017-08-15"
      }
      },
      {
      "_index": "book",
      "_type": "novel",
      "_id": "8",
      "_score": 0.19856805,
      "_source": {
      "author": "瓦力",
      "title": "ElasticSearch入门",
      "wordcount": 3000,
      "publish_date": "2017-08-20"
      }
      },
      {
      "_index": "book",
      "_type": "novel",
      "_id": "12",
      "_score": 0.155468,
      "_source": {
      "title": "瓦力教我们学ElasticSearch",
      "author": "瓦力",
      "wordcount": 1000,
      "publish_date": "2017-08-01"
      }
      }
      ]
      }
      }
  • 字段级别查询 针对结构化数据,如数字、日期等

    请求地址:http://192.168.137.8:9200/book/_search

    请求方式:POST

    请求数据:

    1
    2
    3
    4
    5
    6
    7
    {
    "query":{
    "term":{ //字段查询
    "wordcount":"5000" //查询wordcount字段的值等于5000的记录
    }
    }
    }

    响应结果如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    {
    "took": 81,
    "timed_out": false,
    "_shards": {
    "total": 3,
    "successful": 3,
    "failed": 0
    },
    "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
    {
    "_index": "book",
    "_type": "novel",
    "_id": "5",
    "_score": 1,
    "_source": {
    "author": "王五",
    "title": "菜谱",
    "wordcount": 5000,
    "publish_date": "2002-10-01"
    }
    }
    ]
    }
    }

    查询范围:

    请求地址和请求方式不变,请求参数如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    {
    "query":{
    "range":{
    "wordcount":{ //查询wordcount字段
    "gte":3000, //大于等于3000 e表示equal
    "lte":5000 //小于等于
    }
    }
    }
    }

    响应结果如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    {
    "took": 25,
    "timed_out": false,
    "_shards": {
    "total": 3,
    "successful": 3,
    "failed": 0
    },
    "hits": {
    "total": 3,
    "max_score": 1,
    "hits": [
    {
    "_index": "book",
    "_type": "novel",
    "_id": "5",
    "_score": 1,
    "_source": {
    "author": "王五",
    "title": "菜谱",
    "wordcount": 5000,
    "publish_date": "2002-10-01"
    }
    },
    {
    "_index": "book",
    "_type": "novel",
    "_id": "8",
    "_score": 1,
    "_source": {
    "author": "瓦力",
    "title": "ElasticSearch入门",
    "wordcount": 3000,
    "publish_date": "2017-08-20"
    }
    },
    {
    "_index": "book",
    "_type": "novel",
    "_id": "9",
    "_score": 1,
    "_source": {
    "author": "很胖的瓦力",
    "title": "ElasticSearch精通",
    "wordcount": 3000,
    "publish_date": "2017-08-15"
    }
    }
    ]
    }
    }

    还可以对日期字段进行范围查询:

    请求地址和请求方式不变,请求参数如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    {
    "query":{
    "range":{
    "publish_date":{ //出版日期,使用now关键表示当前时间
    "gte":"2017-01-01",
    "lte":"2017-12-31"
    }
    }
    }
    }

    响应结果如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    {
    "took": 59,
    "timed_out": false,
    "_shards": {
    "total": 3,
    "successful": 3,
    "failed": 0
    },
    "hits": {
    "total": 3,
    "max_score": 1,
    "hits": [
    {
    "_index": "book",
    "_type": "novel",
    "_id": "8",
    "_score": 1,
    "_source": {
    "author": "瓦力",
    "title": "ElasticSearch入门",
    "wordcount": 3000,
    "publish_date": "2017-08-20"
    }
    },
    {
    "_index": "book",
    "_type": "novel",
    "_id": "12",
    "_score": 1,
    "_source": {
    "title": "瓦力教我们学ElasticSearch",
    "author": "瓦力",
    "wordcount": 1000,
    "publish_date": "2017-08-01"
    }
    },
    {
    "_index": "book",
    "_type": "novel",
    "_id": "9",
    "_score": 1,
    "_source": {
    "author": "很胖的瓦力",
    "title": "ElasticSearch精通",
    "wordcount": 3000,
    "publish_date": "2017-08-15"
    }
    }
    ]
    }
    }

Filter Context

概念:在查询过程中,只判断该文档是否满足条件,只有YES或NO。必须结合关键字bool一起使用,并且ES对Filter查询会进行缓存。示例如下:

请求地址:http://192.168.137.8:9200/book/_search

请求方式:POST

请求数据:

1
2
3
4
5
6
7
8
9
10
11
{
"query":{
"bool":{ //结合bool一起使用
"filter":{ //关键字
"term":{
"wordcount":2000 //字数等于2000
}
}
}
}
}

响应结果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
{
"took": 52,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0,
"hits": [
{
"_index": "book",
"_type": "novel",
"_id": "2",
"_score": 0,
"_source": {
"author": "李三",
"title": "Java入门",
"wordcount": 2000,
"publish_date": "2010-10-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "3",
"_score": 0,
"_source": {
"author": "张三",
"title": "Python入门",
"wordcount": 2000,
"publish_date": "2005-10-01"
}
}
]
}
}

复合条件查询

以一定的逻辑组合子条件查询;包含固定分数查询、布尔查询等

固定分数查询,这里的固定分数指的就是上面的匹配度

请求地址:http://192.168.137.8:9200/_search

请求方式:POST

请求数据:

1
2
3
4
5
6
7
8
9
10
11
{
"query":{
"constant_score":{ //固定分数
"filter":{
"match":{
"title":"ElasticSearch"
}
}
}
}
}

响应结果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
{
"took": 6,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 1,
"hits": [
{
"_index": "book",
"_type": "novel",
"_id": "4",
"_score": 1, //分数固定为1
"_source": {
"author": "李四",
"title": "ElasticSearch大法好",
"wordcount": 1000,
"publish_date": "2010-10-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "8",
"_score": 1,
"_source": {
"author": "瓦力",
"title": "ElasticSearch入门",
"wordcount": 3000,
"publish_date": "2017-08-20"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "12",
"_score": 1,
"_source": {
"title": "瓦力教我们学ElasticSearch",
"author": "瓦力",
"wordcount": 1000,
"publish_date": "2017-08-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "9",
"_score": 1,
"_source": {
"author": "很胖的瓦力",
"title": "ElasticSearch精通",
"wordcount": 3000,
"publish_date": "2017-08-15"
}
}
]
}
}

指定固定分数:

1
2
3
4
5
6
7
8
9
10
11
12
{
"query": {
"constant_score": {
"filter": {
"match": {
"title": "ElasticSearch"
}
},
"boost": 4 //指定固定分数为4
}
}
}

布尔查询,使用bool关键字

请求地址:http://192.168.137.8:9200/_search

请求方式:POST

请求数据:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
"query": {
"bool": { //布尔关键
"should": [ //或的关系
{
"match": {
"author": "瓦力" //作者是瓦力
}
},
{
"match": {
"title": "ElasticSearch" //标题
}
}
]
}
}
}

响应结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 1.3555917,
"hits": [
{
"_index": "book",
"_type": "novel",
"_id": "4",
"_score": 1.3555917,
"_source": {
"author": "李四",
"title": "ElasticSearch大法好",
"wordcount": 1000,
"publish_date": "2010-10-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "9",
"_score": 1.1001158,
"_source": {
"author": "很胖的瓦力",
"title": "ElasticSearch精通",
"wordcount": 3000,
"publish_date": "2017-08-15"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "8",
"_score": 0.3808896,
"_source": {
"author": "瓦力",
"title": "ElasticSearch入门",
"wordcount": 3000,
"publish_date": "2017-08-20"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "12",
"_score": 0.33778957,
"_source": {
"title": "瓦力教我们学ElasticSearch",
"author": "瓦力",
"wordcount": 1000,
"publish_date": "2017-08-01"
}
}
]
}
}

还可以使用must关键字

请求地址和请求方式不变,请求参数如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
"query": {
"bool": {
"must": [ //与的关系
{
"match": {
"author": "瓦力"
}
},
{
"match": {
"title": "ElasticSearch"
}
}
]
}
}
}

响应结果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
{
"took": 195,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.3808896,
"hits": [
{
"_index": "book",
"_type": "novel",
"_id": "8",
"_score": 0.3808896,
"_source": {
"author": "瓦力",
"title": "ElasticSearch入门",
"wordcount": 3000,
"publish_date": "2017-08-20"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "12",
"_score": 0.33778957,
"_source": {
"title": "瓦力教我们学ElasticSearch",
"author": "瓦力",
"wordcount": 1000,
"publish_date": "2017-08-01"
}
}
]
}
}

也可以结合filter关键一起使用,请求地址和方式不变,请求数据如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
{
"query": {
"bool": {
"must": [
{
"match": {
"author": "瓦力"
}
},
{
"match": {
"title": "ElasticSearch"
}
}
],
"filter": [
{
"term": {
"wordcount": 1000
}
}
]
}
}
}

响应结果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
{
"took": 44,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.33778957,
"hits": [
{
"_index": "book",
"_type": "novel",
"_id": "12",
"_score": 0.33778957,
"_source": {
"title": "瓦力教我们学ElasticSearch",
"author": "瓦力",
"wordcount": 1000,
"publish_date": "2017-08-01"
}
}
]
}
}

还有must_not关键,请求地址和请求方式不变,请求数据如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
{
"query": {
"bool": {
"must_not": [ //查询作者不是瓦力的记录
{
"term": {
"author": "瓦力"
}
}
]
}
}
}

响应数据如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
{
"took": 23,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"failed": 0
},
"hits": {
"total": 10,
"max_score": 1,
"hits": [
{
"_index": "book",
"_type": "novel",
"_id": "2",
"_score": 1,
"_source": {
"author": "李三",
"title": "Java入门",
"wordcount": 2000,
"publish_date": "2010-10-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "4",
"_score": 1,
"_source": {
"author": "李四",
"title": "ElasticSearch大法好",
"wordcount": 1000,
"publish_date": "2010-10-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "5",
"_score": 1,
"_source": {
"author": "王五",
"title": "菜谱",
"wordcount": 5000,
"publish_date": "2002-10-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "6",
"_score": 1,
"_source": {
"author": "赵六",
"title": "剑谱",
"wordcount": 10000,
"publish_date": "1997-01-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "7",
"_score": 1,
"_source": {
"author": "张三丰",
"title": "太极拳",
"wordcount": 1000,
"publish_date": "1997-01-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "11",
"_score": 1,
"_source": {
"author": "孙悟空",
"title": "七十二变",
"wordcount": 1000,
"publish_date": "2000-10-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "1",
"_score": 1,
"_source": {
"author": "张三",
"title": "移魂大法",
"wordcount": 1000,
"publish_date": "2000-10-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "3",
"_score": 1,
"_source": {
"author": "张三",
"title": "Python入门",
"wordcount": 2000,
"publish_date": "2005-10-01"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "9",
"_score": 1,
"_source": {
"author": "很胖的瓦力",
"title": "ElasticSearch精通",
"wordcount": 3000,
"publish_date": "2017-08-15"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "10",
"_score": 1,
"_source": {
"author": "牛魔王",
"title": "芭蕉扇",
"wordcount": 1000,
"publish_date": "2000-10-01"
}
}
]
}
}
文章目录
  1. 1. ElasticSearch高级查询
  2. 2. 运行环境
  3. 3. 准备测试数据
  4. 4. 子条件查询
    1. 4.1. Query Context
    2. 4.2. Filter Context
  5. 5. 复合条件查询
    1. 5.1. 固定分数查询,这里的固定分数指的就是上面的匹配度
    2. 5.2. 指定固定分数:
    3. 5.3. 布尔查询,使用bool关键字