From 443cb1085bdaf526b2a10dfa85dfba540a1617b7 Mon Sep 17 00:00:00 2001 From: asahi Date: Wed, 25 Dec 2024 12:55:27 +0800 Subject: [PATCH] =?UTF-8?q?=E9=98=85=E8=AF=BBes=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- elastic search/elastic search.md | 89 ++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/elastic search/elastic search.md b/elastic search/elastic search.md index 1df71f0..61a4c3b 100644 --- a/elastic search/elastic search.md +++ b/elastic search/elastic search.md @@ -611,6 +611,95 @@ PUT /cooking_blog/_mapping > > 当`ignore_above`没有显式指定时,其值默认为256. +#### 批量插入数据 +再创建索引后,可以向索引中批量插入文档数据: +``` +POST /cooking_blog/_bulk?refresh=wait_for +{"index":{"_id":"1"}} +{"title":"Perfect Pancakes: A Fluffy Breakfast Delight","description":"Learn the secrets to making the fluffiest pancakes, so amazing you won't believe your tastebuds. This recipe uses buttermilk and a special folding technique to create light, airy pancakes that are perfect for lazy Sunday mornings.","author":"Maria Rodriguez","date":"2023-05-01","category":"Breakfast","tags":["pancakes","breakfast","easy recipes"],"rating":4.8} +{"index":{"_id":"2"}} +{"title":"Spicy Thai Green Curry: A Vegetarian Adventure","description":"Dive into the flavors of Thailand with this vibrant green curry. Packed with vegetables and aromatic herbs, this dish is both healthy and satisfying. Don't worry about the heat - you can easily adjust the spice level to your liking.","author":"Liam Chen","date":"2023-05-05","category":"Main Course","tags":["thai","vegetarian","curry","spicy"],"rating":4.6} +{"index":{"_id":"3"}} +{"title":"Classic Beef Stroganoff: A Creamy Comfort Food","description":"Indulge in this rich and creamy beef stroganoff. Tender strips of beef in a savory mushroom sauce, served over a bed of egg noodles. It's the ultimate comfort food for chilly evenings.","author":"Emma Watson","date":"2023-05-10","category":"Main Course","tags":["beef","pasta","comfort food"],"rating":4.7} +{"index":{"_id":"4"}} +{"title":"Vegan Chocolate Avocado Mousse","description":"Discover the magic of avocado in this rich, vegan chocolate mousse. Creamy, indulgent, and secretly healthy, it's the perfect guilt-free dessert for chocolate lovers.","author":"Alex Green","date":"2023-05-15","category":"Dessert","tags":["vegan","chocolate","avocado","healthy dessert"],"rating":4.5} +{"index":{"_id":"5"}} +{"title":"Crispy Oven-Fried Chicken","description":"Get that perfect crunch without the deep fryer! This oven-fried chicken recipe delivers crispy, juicy results every time. A healthier take on the classic comfort food.","author":"Maria Rodriguez","date":"2023-05-20","category":"Main Course","tags":["chicken","oven-fried","healthy"],"rating":4.9} +``` + +#### 执行full-text search +full-text search会在一个或多个document fields之间执行基于文本的查询。这些查询会为每个匹配的文档计算relevance score,relevance score的计算基于文档内容和search terms的关联程度。 + +ES支持多种查询类型,每种查询类型都有其`matching text`和`relevance scoring`的方法。 + +##### `match` +match是针对full-text的标准查询,基于每个字段上配置的analyzer,query text将会被分析。 + +``` +GET /cooking_blog/_search +{ + "query": { + "match": { + "description": { + "query": "fluffy pancakes" + } + } + } +} +``` +默认情况下,`match query`在resulting tokens间使用`or`,故而在上述的查询中,会查找description中包含`fluffy`或`pancakes`任一的document。 + +其会返回结果如下: +``` +{ + "took": 0, + "timed_out": false, + "_shards": { + "total": 1, + "successful": 1, + "skipped": 0, + "failed": 0 + }, + "hits": { + "total": { + "value": 1, + "relation": "eq" + }, + "max_score": 1.8378843, + "hits": [ + { + "_index": "cooking_blog", + "_id": "1", + "_score": 1.8378843, + "_source": { + "title": "Perfect Pancakes: A Fluffy Breakfast Delight", + "description": "Learn the secrets to making the fluffiest pancakes, so amazing you won't believe your tastebuds. This recipe uses buttermilk and a special folding technique to create light, airy pancakes that are perfect for lazy Sunday mornings.", + "author": "Maria Rodriguez", + "date": "2023-05-01", + "category": "Breakfast", + "tags": [ + "pancakes", + "breakfast", + "easy recipes" + ], + "rating": 4.8 + } + } + ] + } +} +``` + +> ##### track total hits +> 如果想要精确计算hit count,通常需要遍历所有的匹配文档,这将会带来很大开销。 +> +> `track_total_hists`参数允许对`如何计算hit count`进行控制。 +> - 如果设置为true,那么会精确的计算匹配数量,`total.relation`会一直为`eq`,代表`total.value`和实际hit count相同 +> - 如果该值为其他值,例如其默认值`10000`,则该查询数量的`下限`为`10000` +> - 如果`total.relation`为`eq`,则`total.value`代表实际hit count +> - 如果`total.relation`为`gte`, 则`total.value`为hit count的下界,实际hit count大于或等于`total.value` + +