草庐IT

Elasticsearch 核心技术(八):常用 DSL 查询(全文搜索、精确匹配、布尔查询)

水滴技术 2023-06-11 原文

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

文章目录


大家好,我是水滴~~

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 教程:从入门到精通
👍 《大数据核心技术从入门到精通

有关Elasticsearch 核心技术(八):常用 DSL 查询(全文搜索、精确匹配、布尔查询)的更多相关文章

  1. ruby - ECONNRESET (Whois::ConnectionError) - 尝试在 Ruby 中查询 Whois 时出错 - 2

    我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.

  2. ruby-on-rails - Rails 常用字符串(用于通知和错误信息等) - 2

    大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje

  3. ruby 正则表达式 - 如何替换字符串中匹配项的第 n 个实例 - 2

    在我的应用程序中,我需要能够找到所有数字子字符串,然后扫描每个子字符串,找到第一个匹配范围(例如5到15之间)的子字符串,并将该实例替换为另一个字符串“X”。我的测试字符串s="1foo100bar10gee1"我的初始模式是1个或多个数字的任何字符串,例如,re=Regexp.new(/\d+/)matches=s.scan(re)给出["1","100","10","1"]如果我想用“X”替换第N个匹配项,并且只替换第N个匹配项,我该怎么做?例如,如果我想替换第三个匹配项“10”(匹配项[2]),我不能只说s[matches[2]]="X"因为它做了两次替换“1fooX0barXg

  4. ruby - 匹配未转义的平衡定界符对 - 2

    如何匹配未被反斜杠转义的平衡定界符对(其本身未被反斜杠转义)(无需考虑嵌套)?例如对于反引号,我试过了,但是转义的反引号没有像转义那样工作。regex=/(?!$1:"how\\"#expected"how\\`are"上面的正则表达式不考虑由反斜杠转义并位于反引号前面的反斜杠,但我愿意考虑。StackOverflow如何做到这一点?这样做的目的并不复杂。我有文档文本,其中包括内联代码的反引号,就像StackOverflow一样,我想在HTML文件中显示它,内联代码用一些spanMaterial装饰。不会有嵌套,但转义反引号或转义反斜杠可能出现在任何地方。

  5. ruby-on-rails - 在 Rails 和 ActiveRecord 中查询时忽略某些字段 - 2

    我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr

  6. ruby - 匹配大写字母并用后续字母填充,直到一定的字符串长度 - 2

    我有一个驼峰式字符串,例如:JustAString。我想按照以下规则形成长度为4的字符串:抓取所有大写字母;如果超过4个大写字母,只保留前4个;如果少于4个大写字母,则将最后大写字母后的字母大写并添加字母,直到长度变为4。以下是可能发生的3种情况:ThisIsMyString将产生TIMS(大写字母);ThisIsOneVeryLongString将产生TIOV(前4个大写字母);MyString将生成MSTR(大写字母+tr大写)。我设法用这个片段解决了前两种情况:str.scan(/[A-Z]/).first(4).join但是,我不太确定如何最好地修改上面的代码片段以处理最后一种

  7. ruby-on-rails - Nokogiri:使用 XPath 搜索 <div> - 2

    我使用Nokogiri(Rubygem)css搜索寻找某些在我的html里面。看起来Nokogiri的css搜索不喜欢正则表达式。我想切换到Nokogiri的xpath搜索,因为这似乎支持搜索字符串中的正则表达式。如何在xpath搜索中实现下面提到的(伪)css搜索?require'rubygems'require'nokogiri'value=Nokogiri::HTML.parse(ABBlaCD3"HTML_END#my_blockisgivenmy_bl="1"#my_eqcorrespondstothisregexmy_eq="\/[0-9]+\/"#FIXMEThefoll

  8. ruby-on-rails - Rails 3,嵌套资源,没有路由匹配 [PUT] - 2

    我真的为这个而疯狂。我一直在搜索答案并尝试我找到的所有内容,包括相关问题和stackoverflow上的答案,但仍然无法正常工作。我正在使用嵌套资源,但无法使表单正常工作。我总是遇到错误,例如没有路线匹配[PUT]"/galleries/1/photos"表格在这里:/galleries/1/photos/1/edit路线.rbresources:galleriesdoresources:photosendresources:galleriesresources:photos照片Controller.rbdefnew@gallery=Gallery.find(params[:galle

  9. ruby - 如何在 Ruby 中创建无类 DSL? - 2

    我正在尝试找出如何为我的Ruby项目创建一种“无类DSL”,类似于在Cucumber步骤定义文件中定义步骤定义或在Sinatra应用程序中定义路由。例如,我想要一个文件,其中调用了我的所有DSL函数:#sample.rbwhen_string_matches/hello(.+)/do|name|call_another_method(name)end我认为用我的项目特有的一堆方法污染全局(内核)命名空间是一种不好的做法。因此方法when_string_matches和call_another_method将在我的库中定义,并且sample.rb文件将以某种方式在我的DSL方法的上下文中

  10. Unity 热更新技术 | (三) Lua语言基本介绍及下载安装 - 2

    ?博客主页:https://xiaoy.blog.csdn.net?本文由呆呆敲代码的小Y原创,首发于CSDN??学习专栏推荐:Unity系统学习专栏?游戏制作专栏推荐:游戏制作?Unity实战100例专栏推荐:Unity实战100例教程?欢迎点赞?收藏⭐留言?如有错误敬请指正!?未来很长,值得我们全力奔赴更美好的生活✨------------------❤️分割线❤️-------------------------

随机推荐