代码编织梦想

在这里插入图片描述

❤️ 博客主页:水滴技术
🚀 支持水滴:点赞👍 + 收藏⭐ + 留言💬
🌸 订阅专栏:大数据核心技术从入门到精通


大家好,我是水滴~~

Elasticsearch 提供了一个完整的基于 JSON 的 DSL(Domain Specific Language,领域特定语言) 查询语言,它非常的丰富和灵活,并支持构建更加复杂和健壮的查询。

本篇我们主要讲述常用的三种 DSL 查询:全文搜索、精确匹配、布尔查询。

一、全文搜索

全文搜索是 Elasticsearch 的核心功能,在创建索引时要将字段映射设为 text 类型,并可指定分词器,我们方可使用该功能。关于数据类型分词器可以参考之前的一些文章,这里不再赘述。

1.1 查询所有(match_all)

可以使用 match_all 来查询指定索引下的所有文档,下面例子查询“my_product”字段中所有文档:

GET /mt_product/_search
{
  "query": {
    "match_all": {}
  }
}

响应结果:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 12,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "mt_product",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "id" : 1,
          "name" : "招牌海苔单人餐",
          "tags" : [
            "寿司"
          ],
          "price" : 9.9,
          "sales" : 1000,
          "score" : 4.7,
          "store_id" : 1,
          "store_name" : "M多寿司",
          "create_time" : "2023-01-18 08:01:00"
        }
      },
      ...
    ]
  }
}

1.2 全文检索(match)

可以使用 match 进行全文搜索匹配(类似于 SQL 中的 like %%),搜索的字段类型要是 text 类型才能生效。下例中搜索"my_product"索引,搜索字段使用 “name”:

GET /mt_product/_search
{
  "query": {
    "match": {
      "name": "好吃的寿司"
    }
  }
}

响应:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.7955686,
    "hits" : [
      {
        "_index" : "mt_product",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.7955686,
        "_source" : {
          "id" : 3,
          "name" : "三文鱼寿司",
          "tags" : [
            "寿司,鱼肉"
          ],
          "price" : 16.9,
          "sales" : 820,
          "score" : 4.9,
          "store_id" : 2,
          "store_name" : "爱食寿司",
          "create_time" : "2023-01-18 08:03:00"
        }
      },
      {
        "_index" : "mt_product",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 1.6760855,
        "_source" : {
          "id" : 4,
          "name" : "极上全品寿司套餐",
          "tags" : [
            "寿司"
          ],
          "price" : 25,
          "sales" : 1500,
          "score" : 4.6,
          "store_id" : 2,
          "store_name" : "爱食寿司",
          "create_time" : "2023-01-18 08:04:00"
        }
      }
    ]
  }
}

1.3 多字段全文检索(multi_match)

有的时候同一个搜索请求,要应用到多个字段上,那么使用 multi_match 即可实现,如下例:

GET /mt_product/_search
{
  "query": {
    "multi_match": {
      "query": "好吃的寿司",
      "fields": ["name", "store_name"]
    }
  }
}

响应:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : 1.7955686,
    "hits" : [
      {
        "_index" : "mt_product",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.7955686,
        "_source" : {
          "id" : 3,
          "name" : "三文鱼寿司",
          "tags" : [
            "寿司,鱼肉"
          ],
          "price" : 16.9,
          "sales" : 820,
          "score" : 4.9,
          "store_id" : 2,
          "store_name" : "爱食寿司",
          "create_time" : "2023-01-18 08:03:00"
        }
      },
      {
        "_index" : "mt_product",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 1.6760855,
        "_source" : {
          "id" : 4,
          "name" : "极上全品寿司套餐",
          "tags" : [
            "寿司"
          ],
          "price" : 25,
          "sales" : 1500,
          "score" : 4.6,
          "store_id" : 2,
          "store_name" : "爱食寿司",
          "create_time" : "2023-01-18 08:04:00"
        }
      },
      {
        "_index" : "mt_product",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.84174097,
        "_source" : {
          "id" : 1,
          "name" : "招牌海苔单人餐",
          "tags" : [
            "寿司"
          ],
          "price" : 9.9,
          "sales" : 1000,
          "score" : 4.7,
          "store_id" : 1,
          "store_name" : "M多寿司",
          "create_time" : "2023-01-18 08:01:00"
        }
      },
      {
        "_index" : "mt_product",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.84174097,
        "_source" : {
          "id" : 2,
          "name" : "1-2人招牌双拼套餐",
          "tags" : [
            "寿司"
          ],
          "price" : 18.9,
          "sales" : 1200,
          "score" : 4.8,
          "store_id" : 1,
          "store_name" : "M多寿司",
          "create_time" : "2023-01-18 08:02:00"
        }
      }
    ]
  }
}

二、精确匹配

精确匹配应用查找结构化的数据,一般使用 keyword 类型,应避免使用 text 类型。

2.1 精确查询(term)

根据精确值进行查询,类似于 SQL 中的 = ,示例:


GET /mt_product/_search
{
  "query": {
    "term": {
      "store_id": {
        "value": "1"
      }
    }
  }
}

响应结果:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.6486585,
    "hits" : [
      {
        "_index" : "mt_product",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.6486585,
        "_source" : {
          "id" : 1,
          "name" : "招牌海苔单人餐",
          "tags" : [
            "寿司"
          ],
          "price" : 9.9,
          "sales" : 1000,
          "score" : 4.7,
          "store_id" : 1,
          "store_name" : "M多寿司",
          "create_time" : "2023-01-18 08:01:00"
        }
      },
      {
        "_index" : "mt_product",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.6486585,
        "_source" : {
          "id" : 2,
          "name" : "1-2人招牌双拼套餐",
          "tags" : [
            "寿司"
          ],
          "price" : 18.9,
          "sales" : 1200,
          "score" : 4.8,
          "store_id" : 1,
          "store_name" : "M多寿司",
          "create_time" : "2023-01-18 08:02:00"
        }
      }
    ]
  }
}

2.2 精确查询(terms)

terms 可以匹配多个值,类似于 SQL 中的 in(...),示例:

GET /mt_product/_search
{
  "query": {
    "terms": {
      "store_id": [1, 2]
    }
  }
}

响应结果:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "mt_product",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "id" : 1,
          "name" : "招牌海苔单人餐",
          "tags" : [
            "寿司"
          ],
          "price" : 9.9,
          "sales" : 1000,
          "score" : 4.7,
          "store_id" : 1,
          "store_name" : "M多寿司",
          "create_time" : "2023-01-18 08:01:00"
        }
      },
      {
        "_index" : "mt_product",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "id" : 2,
          "name" : "1-2人招牌双拼套餐",
          "tags" : [
            "寿司"
          ],
          "price" : 18.9,
          "sales" : 1200,
          "score" : 4.8,
          "store_id" : 1,
          "store_name" : "M多寿司",
          "create_time" : "2023-01-18 08:02:00"
        }
      },
      {
        "_index" : "mt_product",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "id" : 3,
          "name" : "三文鱼寿司",
          "tags" : [
            "寿司,鱼肉"
          ],
          "price" : 16.9,
          "sales" : 820,
          "score" : 4.9,
          "store_id" : 2,
          "store_name" : "爱食寿司",
          "create_time" : "2023-01-18 08:03:00"
        }
      },
      {
        "_index" : "mt_product",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 1.0,
        "_source" : {
          "id" : 4,
          "name" : "极上全品寿司套餐",
          "tags" : [
            "寿司"
          ],
          "price" : 25,
          "sales" : 1500,
          "score" : 4.6,
          "store_id" : 2,
          "store_name" : "爱食寿司",
          "create_time" : "2023-01-18 08:04:00"
        }
      }
    ]
  }
}

2.3 主键查询(ids)

根据多个主键查询,类似于 SQL 中的 id in (...),示例:

GET /mt_product/_search
{
  "query": {
    "ids": {
      "values": [10, 11]
    }
  }
}

响应结果:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "mt_product",
        "_type" : "_doc",
        "_id" : "10",
        "_score" : 1.0,
        "_source" : {
          "id" : 10,
          "name" : "双层原味板烧鸡腿麦满分四件套",
          "tags" : [
            "汉堡,鸡肉"
          ],
          "price" : 29,
          "sales" : 3000,
          "score" : 4.8,
          "store_id" : 5,
          "store_name" : "麦当劳",
          "create_time" : "2023-01-18 08:10:00"
        }
      },
      {
        "_index" : "mt_product",
        "_type" : "_doc",
        "_id" : "11",
        "_score" : 1.0,
        "_source" : {
          "id" : 11,
          "name" : "火腿扒麦满分组合",
          "tags" : [
            "汉堡"
          ],
          "price" : 8,
          "sales" : 100000,
          "score" : 4.9,
          "store_id" : 5,
          "store_name" : "麦当劳",
          "create_time" : "2023-01-18 08:11:00"
        }
      }
    ]
  }
}

2.4 范围查询(range)

查找一个范围,类似于 SQL 中的 ><,示例:

GET /mt_product/_search
{
  "query": {
    "range": {
      "price": {
        "gte": 10,
        "lte": 20
      }
    }
  }
}

响应结果:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "mt_product",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "id" : 2,
          "name" : "1-2人招牌双拼套餐",
          "tags" : [
            "寿司"
          ],
          "price" : 18.9,
          "sales" : 1200,
          "score" : 4.8,
          "store_id" : 1,
          "store_name" : "M多寿司",
          "create_time" : "2023-01-18 08:02:00"
        }
      },
      {
        "_index" : "mt_product",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "id" : 3,
          "name" : "三文鱼寿司",
          "tags" : [
            "寿司,鱼肉"
          ],
          "price" : 16.9,
          "sales" : 820,
          "score" : 4.9,
          "store_id" : 2,
          "store_name" : "爱食寿司",
          "create_time" : "2023-01-18 08:03:00"
        }
      },
      {
        "_index" : "mt_product",
        "_type" : "_doc",
        "_id" : "9",
        "_score" : 1.0,
        "_source" : {
          "id" : 9,
          "name" : "霸道小酥鸡+薯霸王",
          "tags" : [
            "汉堡,鸡肉"
          ],
          "price" : 19,
          "sales" : 300,
          "score" : 4.2,
          "store_id" : 4,
          "store_name" : "汉堡王",
          "create_time" : "2023-01-18 08:09:00"
        }
      }
    ]
  }
}

三、布尔查询(bool)

布尔查询就是一个或多个子句的组合,每一个子句都是一个子查询,根据组合的方式可分为下面几种类型:

  • must:必须匹配每个子句,类似于 SQL 中的 and,参与评分。
  • should:可以匹配任意子句,类似于 SQL 中的 or,参与评分。
  • must_not:必须不匹配每个子类,类似于 SQL中的 not in,不参与评分。
  • filter:过滤上下文,它与 must 的不同之处是不会影响匹配文档的分数。

3.1 必须匹配(must)

我们要查询 tag 为“寿司”,并且价格小于等于 15 块钱,就可以使用这样:

GET /mt_product/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "tags": "寿司"
          }
        },{
          "range": {
            "price": {
              "lte": 15
            }
          }
        }
      ]
    }
  }
}

响应结果:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 2.228378,
    "hits" : [
      {
        "_index" : "mt_product",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 2.228378,
        "_source" : {
          "id" : 1,
          "name" : "招牌海苔单人餐",
          "tags" : [
            "寿司"
          ],
          "price" : 9.9,
          "sales" : 1000,
          "score" : 4.7,
          "store_id" : 1,
          "store_name" : "M多寿司",
          "create_time" : "2023-01-18 08:01:00"
        }
      }
    ]
  }
}

3.2 可以匹配(should)

查询 tag 为“鱼肉”,或者 store_name 为“麦当劳”,可以这样查询:

GET /mt_product/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "tags": "鱼肉"
          }
        },{
          "match": {
            "store_name": "麦当劳"
          }
        }
      ]
    }
  }
}

响应结果:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : 1.9003463,
    "hits" : [
      {
        "_index" : "mt_product",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.9003463,
        "_source" : {
          "id" : 3,
          "name" : "三文鱼寿司",
          "tags" : [
            "寿司,鱼肉"
          ],
          "price" : 16.9,
          "sales" : 820,
          "score" : 4.9,
          "store_id" : 2,
          "store_name" : "爱食寿司",
          "create_time" : "2023-01-18 08:03:00"
        }
      },
      {
        "_index" : "mt_product",
        "_type" : "_doc",
        "_id" : "10",
        "_score" : 1.6119244,
        "_source" : {
          "id" : 10,
          "name" : "双层原味板烧鸡腿麦满分四件套",
          "tags" : [
            "汉堡,鸡肉"
          ],
          "price" : 29,
          "sales" : 3000,
          "score" : 4.8,
          "store_id" : 5,
          "store_name" : "麦当劳",
          "create_time" : "2023-01-18 08:10:00"
        }
      },
      {
        "_index" : "mt_product",
        "_type" : "_doc",
        "_id" : "11",
        "_score" : 1.6119244,
        "_source" : {
          "id" : 11,
          "name" : "火腿扒麦满分组合",
          "tags" : [
            "汉堡"
          ],
          "price" : 8,
          "sales" : 100000,
          "score" : 4.9,
          "store_id" : 5,
          "store_name" : "麦当劳",
          "create_time" : "2023-01-18 08:11:00"
        }
      },
      {
        "_index" : "mt_product",
        "_type" : "_doc",
        "_id" : "12",
        "_score" : 1.6119244,
        "_source" : {
          "id" : 12,
          "name" : "原味板烧鸡腿麦满组件",
          "tags" : [
            "汉堡,鸡肉"
          ],
          "price" : 9.9,
          "sales" : 140000,
          "score" : 4.9,
          "store_id" : 5,
          "store_name" : "麦当劳",
          "create_time" : "2023-01-18 08:12:00"
        }
      }
    ]
  }
}

3.3 不匹配(must_not)

查询 store_name 不是“麦当劳”,并且 tags 不包含“寿司”,可以这样查询:

GET /mt_product/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "match": {
            "store_name": "麦当劳"
          }
        },{
          "match": {
            "tags": "寿司"
          }
        }
      ]
    }
  }
}

响应结果:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 5,
      "relation" : "eq"
    },
    "max_score" : 0.0,
    "hits" : [
      {
        "_index" : "mt_product",
        "_type" : "_doc",
        "_id" : "5",
        "_score" : 0.0,
        "_source" : {
          "id" : 5,
          "name" : "劲脆鸡腿汉堡",
          "tags" : [
            "汉堡,鸡肉"
          ],
          "price" : 21.5,
          "sales" : 200,
          "score" : 4.5,
          "store_id" : 3,
          "store_name" : "肯德基",
          "create_time" : "2023-01-18 08:05:00"
        }
      },
      {
        "_index" : "mt_product",
        "_type" : "_doc",
        "_id" : "6",
        "_score" : 0.0,
        "_source" : {
          "id" : 6,
          "name" : "香辣鸡腿汉堡",
          "tags" : [
            "汉堡,鸡肉"
          ],
          "price" : 21.5,
          "sales" : 98,
          "score" : 4.4,
          "store_id" : 3,
          "store_name" : "肯德基",
          "create_time" : "2023-01-18 08:06:00"
        }
      },
      {
        "_index" : "mt_product",
        "_type" : "_doc",
        "_id" : "7",
        "_score" : 0.0,
        "_source" : {
          "id" : 7,
          "name" : "20块香辣鸡翅",
          "tags" : [
            "鸡肉"
          ],
          "price" : 99,
          "sales" : 5,
          "score" : 4.8,
          "store_id" : 3,
          "store_name" : "肯德基",
          "create_time" : "2023-01-18 08:07:00"
        }
      },
      {
        "_index" : "mt_product",
        "_type" : "_doc",
        "_id" : "8",
        "_score" : 0.0,
        "_source" : {
          "id" : 8,
          "name" : "3层芝士年堡套餐",
          "tags" : [
            "汉堡"
          ],
          "price" : 29,
          "sales" : 4000,
          "score" : 4.9,
          "store_id" : 4,
          "store_name" : "汉堡王",
          "create_time" : "2023-01-18 08:08:00"
        }
      },
      {
        "_index" : "mt_product",
        "_type" : "_doc",
        "_id" : "9",
        "_score" : 0.0,
        "_source" : {
          "id" : 9,
          "name" : "霸道小酥鸡+薯霸王",
          "tags" : [
            "汉堡,鸡肉"
          ],
          "price" : 19,
          "sales" : 300,
          "score" : 4.2,
          "store_id" : 4,
          "store_name" : "汉堡王",
          "create_time" : "2023-01-18 08:09:00"
        }
      }
    ]
  }
}

3.4 过滤器(filter)

filter 过滤的结果不会影响原查询的得分。比如我们在上一条查询的基础上,增加 store_name 为“汉堡王”,其查询结果的得分,与上一条查询的得分是一样的。

GET /mt_product/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "match": {
            "store_name": "麦当劳"
          }
        },{
          "match": {
            "tags": "寿司"
          }
        }
      ], 
      "filter": [
        {
          "match": {
            "store_name": "汉堡王"
          }
        }
      ]
    }
  }
}

响应结果:

{
  "took" : 6,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.0,
    "hits" : [
      {
        "_index" : "mt_product",
        "_type" : "_doc",
        "_id" : "8",
        "_score" : 0.0,
        "_source" : {
          "id" : 8,
          "name" : "3层芝士年堡套餐",
          "tags" : [
            "汉堡"
          ],
          "price" : 29,
          "sales" : 4000,
          "score" : 4.9,
          "store_id" : 4,
          "store_name" : "汉堡王",
          "create_time" : "2023-01-18 08:08:00"
        }
      },
      {
        "_index" : "mt_product",
        "_type" : "_doc",
        "_id" : "9",
        "_score" : 0.0,
        "_source" : {
          "id" : 9,
          "name" : "霸道小酥鸡+薯霸王",
          "tags" : [
            "汉堡,鸡肉"
          ],
          "price" : 19,
          "sales" : 300,
          "score" : 4.2,
          "store_id" : 4,
          "store_name" : "汉堡王",
          "create_time" : "2023-01-18 08:09:00"
        }
      }
    ]
  }
}

附录

附录一:mt_product 索引 demo 脚本

PUT mt_product
{
  "mappings": {
    "properties": {
      "id": {
        "type": "long"
      },
      "name": {
        "type": "text",
        "analyzer": "ik_max_word"
      },
      "tags": {
        "type":  "text",
        "analyzer": "ik_max_word"
      },
      "price": {
        "type": "float"
      },
      "sales": {
        "type": "integer"
      },
      "score": {
        "type": "float"
      },
      "store_id": {
        "type": "keyword"
      },
      "store_name": {
        "type": "text",
        "analyzer": "ik_max_word"
      },
      "create_time": {
        "type": "date"
      }
    }
  }
}

附录二:mt_product 数据 demo 脚本

POST /mt_product/_doc/1
{
  "id": 1,
  "name": "招牌海苔单人餐",
  "tags": ["寿司"],
  "price": 9.9,
  "sales": 1000,
  "score": 4.7,
  "store_id": 1,
  "store_name": "M多寿司",
  "create_time": "2023-01-18 08:01:00"
}

POST /mt_product/_doc/2
{
  "id": 2,
  "name": "1-2人招牌双拼套餐",
  "tags": ["寿司"],
  "price": 18.9,
  "sales": 1200,
  "score": 4.8,
  "store_id": 1,
  "store_name": "M多寿司",
  "create_time": "2023-01-18 08:02:00"
}

POST /mt_product/_doc/3
{
  "id": 3,
  "name": "三文鱼寿司",
  "tags": ["寿司,鱼肉"],
  "price": 16.9,
  "sales": 820,
  "score": 4.9,
  "store_id": 2,
  "store_name": "爱食寿司",
  "create_time": "2023-01-18 08:03:00"
}

POST /mt_product/_doc/4
{
  "id": 4,
  "name": "极上全品寿司套餐",
  "tags": ["寿司"],
  "price": 25,
  "sales": 1500,
  "score": 4.6,
  "store_id": 2,
  "store_name": "爱食寿司",
  "create_time": "2023-01-18 08:04:00"
}

POST /mt_product/_doc/5
{
  "id": 5,
  "name": "劲脆鸡腿汉堡",
  "tags": ["汉堡,鸡肉"],
  "price": 21.5,
  "sales": 200,
  "score": 4.5,
  "store_id": 3,
  "store_name": "肯德基",
  "create_time": "2023-01-18 08:05:00"
}

POST /mt_product/_doc/6
{
  "id": 6,
  "name": "香辣鸡腿汉堡",
  "tags": ["汉堡,鸡肉"],
  "price": 21.5,
  "sales": 98,
  "score": 4.4,
  "store_id": 3,
  "store_name": "肯德基",
  "create_time": "2023-01-18 08:06:00"
}

POST /mt_product/_doc/7
{
  "id": 7,
  "name": "20块香辣鸡翅",
  "tags": ["鸡肉"],
  "price": 99,
  "sales": 5,
  "score": 4.8,
  "store_id": 3,
  "store_name": "肯德基",
  "create_time": "2023-01-18 08:07:00"
}

POST /mt_product/_doc/8
{
  "id": 8,
  "name": "3层芝士年堡套餐",
  "tags": ["汉堡"],
  "price": 29,
  "sales": 4000,
  "score": 4.9,
  "store_id": 4,
  "store_name": "汉堡王",
  "create_time": "2023-01-18 08:08:00"
}

POST /mt_product/_doc/9
{
  "id": 9,
  "name": "霸道小酥鸡+薯霸王",
  "tags": ["汉堡,鸡肉"],
  "price": 19,
  "sales": 300,
  "score": 4.2,
  "store_id": 4,
  "store_name": "汉堡王",
  "create_time": "2023-01-18 08:09:00"
}

POST /mt_product/_doc/10
{
  "id": 10,
  "name": "双层原味板烧鸡腿麦满分四件套",
  "tags": ["汉堡,鸡肉"],
  "price": 29,
  "sales": 3000,
  "score": 4.8,
  "store_id": 5,
  "store_name": "麦当劳",
  "create_time": "2023-01-18 08:10:00"
}

POST /mt_product/_doc/11
{
  "id": 11,
  "name": "火腿扒麦满分组合",
  "tags": ["汉堡"],
  "price": 8,
  "sales": 100000,
  "score": 4.9,
  "store_id": 5,
  "store_name": "麦当劳",
  "create_time": "2023-01-18 08:11:00"
}

POST /mt_product/_doc/12
{
  "id": 12,
  "name": "原味板烧鸡腿麦满组件",
  "tags": ["汉堡,鸡肉"],
  "price": 9.9,
  "sales": 140000,
  "score": 4.9,
  "store_id": 5,
  "store_name": "麦当劳",
  "create_time": "2023-01-18 08:12:00"
}

系列文章

🔥 Elasticsearch 核心技术(一):Elasticsearch 安装、配置、运行(Windows 版)
🔥 Elasticsearch 核心技术(二):elasticsearch-head 插件安装和使用
🔥 Elasticsearch 核心技术(三):Kibana 安装、配置、运行(Windows 版)
🔥 Elasticsearch 核心技术(四):索引管理、映射管理、文档管理(REST API)
🔥 Elasticsearch 核心技术(五):常用数据类型详解
🔥 Elasticsearch 核心技术(六):内置的 8 种分词器详解 + 代码示例
🔥 Elasticsearch 核心技术(七):IK 中文分词器的安装、使用、自定义字典

热门专栏

👍 《Python入门核心技术
👍 《IDEA 教程:从入门到精通
👍 《Java 教程:从入门到精通
👍 《MySQL 教程:从入门到精通
👍 《大数据核心技术从入门到精通

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_67276852/article/details/129821934

ElasticSearch第十一篇:常用DSL查询语法-爱代码爱编程

一:Filter DSL 1.term 过滤(精确匹配)  term主要用于精确匹配哪些值,比如数字,日期,布尔值或 not_analyzed 的字符串(未经分析的文本数据类型) DSL: { "query" : { "term" : { "picParentId" : "15020400114"

bool查询原理 es_ES(Elasticsearch)常用查询和布尔查询boolQuery-爱代码爱编程

ES完整查询示例: { "query": { "bool": { "must": [ { "multi_match": { "query": "spring框架", "minimum_should_match": "50%", "fields": ["name^10", "description"] } }, { "term"

Elasticsearch查询DSL-爱代码爱编程

ES与数据库对应关系 Elasticsearch索引(Index)类型(Type)文档(Docments)字段(Fields)关系数据库(MySQL)数据库(DataBase)表(Table)行(Rows)列(Columns)基本命令 #创建索引库 PUT /shopping #查看所有索引详细信息 GET /_cat/indices?v #查询某个索

elasticSearch学习入门-DSL查询-爱代码爱编程

DSL查询语句 1. 概念2. 数据准备3. match条件查询3.1 查询全部3.2 match查询3.3 match_phrase 查询3.4 match_phrase_prefix查询3.5 mutil_match查询4、term-level 查询4.1 term查询4.2 terms查询4.3 range查询4.4 exists查询4.5

es 常用DSL查询语序 以及springDataES对应和使用-爱代码爱编程

基本理解 索引 = 数据库 类型 = 表 文档 = 一条数据 字段 = 字段 映射 = 设计表的组成(这里不像是关系型数据库不能多加字段可以多加只是一个软规定) ik 地址直接访问用就ok:http://182.61.52.136:5601/ 进去点击DevTools 一个扳手就直接能用了练习es了 自己练习完把自己的数据清理了谢谢 elasticse

elasticsearch7.17 二:mapping映射和高级语法查询dsl_天黑请闭眼丶风的博客-爱代码爱编程

文章目录 mapping映射和高级语法查询DSL文档映射Mapping映射类型更改Mapping的字段类型常用Mapping参数配置批量操作ES高级查询Query DSL查询所有match_all分页查询指定字段排序和指定字段返回match多字段查询multi_match多字段查询query_string关键词查询TermES中的结构化搜索前缀查询

elasticsearch(四):dsl query_men-dd的博客-爱代码爱编程

ES中提供了一种强大的检索数据方式,这种检索方式称之为Query DSL(Domain Specified Language) Query DSL是利用Rest API传递JSON格式的请求体(RequestBody)数据与ES进行交互,这种方式的丰富查询语法让ES检索变得更强大,更简洁 文档: https://www.elastic.co/guide

elasticsearch - dsl 查询语句_从零开始的java世界的博客-爱代码爱编程

文章目录 1.DSL查询文档1.1.DSL查询分类1.2.全文检索查询1.2.1.使用场景1.2.2.基本语法1.2.3.总结1.3.精准查询1.3.1.term查询1.3.2.range查询1.3.3.总结1.4.地理坐标查询1.4.1.矩形范围查询1.4.2.附近查询1.5.复合查询1.5.1.相关性算分1.5.2.算分函数查询1)语法说明2)

48-elasticsearch-2(dsl查询_搜索结果处理_restclient查询文档)_欣慰的三叶草(● ̄(エ) ̄●)的博客-爱代码爱编程

48-Elasticsearch-2(DSL查询_搜索结果处理_RestClient查询文档) 视频内容来源于黑马程序员教学视频 文章目录 48-Elasticsearch-2(DSL查询_搜索结果处理_

es搜索功能——dsl查询文档——dsl基本语法_专吃海绵宝宝菠萝屋的派大星的博客-爱代码爱编程

1、查询的基本语法 # GET请求方式(固定写法) # indexName 要查询的索引库 # _search 查询语句的固定格式 GET /indexName/_search {   "query": {     "查询类型": {       "查询条件": "条件值"     }   } } 2、无条件查询(查询所有) 注:不会把所有查询

elasticsearch -爱代码爱编程

一、相关概念     1、DSL 介绍         Elasticsearch 提供了一个完整的基于 JSON 的查询 DSL(Domain Specific Language)来定义查询         参考:Query DSL | Elasticsearch Guide [8.0] | Elastic         一个查询语句可由

elasticsearch——dsl查询及结果处理-爱代码爱编程

文章目录 1.DSL查询语法1.1.DSL查询分类和基本语法1.2.全文检索1.3.精确查询1.4.地理查询1.5复合查询 2.查询结果处理2.1.排序2.2.分页2.3.高亮 3.RestClie

elk-爱代码爱编程

DSL查询文档 elasticsearch的查询依然是基于JSON风格的DSL来实现的。 DSL查询分类 Elasticsearch提供了基于JSON的DSL(Domain Specific Language)来定义查

【elasticsearch】(五)—— dsl查询文档_php dsl文档-爱代码爱编程

目录 1)DSL查询分类 2)全文检索查询  1、使用场景 2、基本语法 3、示例 4、总结 3)精准查询 1、term 查询 2、range查询  3、总结 4)地理坐标查询 1、矩形范围查询 2、附近查询 5)复合查询 1、相关性算分 2、算分函数查询 3、布尔查询 elasticsearch的查询依

elasticsearch(五):相关性和相关性算分、布尔查询、boosting查询、单多字段查询_elasticsearch boost查询-爱代码爱编程

相关性和相关性算分 相关性(Relevance)什么是TF-IDFBM25通过Explain API查看TF-IDFBoosting 布尔查询bool Query 如何解决结构化查询“包含而不是相等”的问题利用b