Elasticsearch(3) show me some examples

Elasticsearch(3) show me some examples Let’s clear up that confusion right now. Seeing the actual query structure makes a world of difference.To make this completely clear, let’s pretend we have an index calledstore_products. Before we search, let’s look at the type of data we are working with. Imagine our index has documents that look like this:{name:Wireless Noise-Canceling Headphones,brand:Sony,category:Electronics,price:199.99,in_stock:true,tags:[audio,wireless,gadget]}Now, let’s look at three detailed, real-world search examples, ranging from simple to advanced.Example 1: The “Bool” Query (Combining Queries and Filters)In the real world, you rarely just search for a keyword. Usually, a user types a word, and then clicks some checkboxes to filter the results. In Elasticsearch, we do this using abool(Boolean) query.Inside aboolquery, we use two main clauses:must: The resultsmustmatch this text search (calculates a relevance score).filter: The resultsmustmatch this exact criteria (fast, cached, does not affect the score).The ScenarioA user searches your store for the word“wireless”, but they only want items in the“Electronics”category that costunder $250.The Actual Search QueryGET/store_products/_search{query:{bool:{must:[{match:{name:wireless}}],filter:[{term:{category.keyword:Electronics}},{range:{price:{lt:250.00}}}]}}} Crucial Detail: What is.keyword?Notice that for the category filter, I wrotecategory.keywordinstead of justcategory.category(Text field) is broken down into lowercase tokens for searching (e.g., “electronics”).category.keyword(Keyword field) treats the entire string as one exact unit (“Electronics”). When doing exact filters,always use the.keywordversion of a text field.Example 2: Aggregations (Getting Analytics Data)Aggregations don’t just find documents; they calculate dataaboutyour documents. Think of it like aGROUP BYandAVG()in SQL.The ScenarioYou want to build a dashboard sidebar. You need Elasticsearch to look at all your products and tell you:How many products are in eachcategory? (Bucket Aggregation)What is theaverage_priceof the products in each of those categories? (Metric Aggregation)The Actual Search QueryGET/store_products/_search{size:0,aggs:{group_by_category:{terms:{field:category.keyword},aggs:{average_price:{avg:{field:price}}}}}} Crucial Detail: Whysize: 0?By default, a search returns the top 10 matching documentsplusthe aggregation data. By settingsize: 0, we tell Elasticsearch:“I don’t want to see individual products. I only want the analytical totals.”This makes the query incredibly fast.Example 3: Putting It All Together (Search, Filter, and Aggregate)This is the ultimate intermediate query. You can combine everything we just learned into a single request.The ScenarioA user searches for the word“gadget”. You want to show them the matching items that arein stock, but youalsowant to show them a breakdown of which brands make those gadgets and how much they cost on average.The Actual Search QueryGET/store_products/_search{size:5,query:{bool:{must:[{match:{tags:gadget}}],filter:[{term:{in_stock:true}}]}},aggs:{brands_breakdown:{terms:{field:brand.keyword},aggs:{avg_brand_price:{avg:{field:price}}}}}}What Elasticsearch will return:The top 5 documents(size: 5) that have the tag “gadget” and are currently in stock, ranked by how relevant they are to the search.An aggregation blockthat only looks at those specific matching gadgets, groups them by brand, and calculates the average price per brand.If you paste these templates into Elasticvue or Kibana, you can easily change the field names to match your own data!