How can I retrieve the available filters together with the search query in Elastic Search?

As a newcomer to the world of elastic search, I could use some guidance on querying. I'm also open to any suggestions on modifying my MongoDB Schema.

I'm interested in finding out how I can use elastic search to display available products based on specified queries and filters.

For example:

{
  products: [{...}, {...}, ...],
  filters: [ ... ]
}

The Schema for products is:

    {
      title: "...",
      description: "...",
      categoryId: ObjectId,
      variants: [ variant1ID, variant2ID ],
     ... other details
   }

The Schema for Variant is:

{
 product : objectId,
 image: ..,
 price: ..., 
 attributes:[
   "SIZE-XL",
   "COLOR-BLUE",
    // and more
 ]
}

Initially, I thought about retrieving filters based on category, assuming each category has fixed facets. However, I realized that search queries don't need to be restricted to a specific category.

I am considering storing products in elastic search in the following format:

 { 
     productId: "...",
     title:"..",
     description: "...",
     categoryId: "....",
     image: "..",
     variantId:"...",
     price:"..",
     attributes: [
     {
      name:"SIZE",
      value:"XL",
     },
     {
      name:"COLOR",
      value:"BLUE",
     }
      // more attributes
      ]
}

Essentially, storing each variant separately along with its product title and description for faster and easier searching.

Answer №1

It's important to keep in mind that in Elasticsearch, it's best to avoid using populate/joins and instead focus on flattening/de-normalizing your data.

For more information on how to model variants effectively, check out this helpful article:

https://www.elastic.co/blog/how-to-create-a-document-schema-for-product-variants-and-skus-for-your-ecommerce-search-experience

By creating a query that includes facets for any necessary fields (with a keyword type by default), you can easily retrieve the data you need.

For detailed instructions on using aggregations in Elasticsearch, refer to this guide:

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html

Using a filter query can help narrow down your results:

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-filter-context.html#filter-context

For efficient filtering, consider using a "terms" query within your filters, especially for exact matches like checkboxes in the UI:

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The term 'sequelize' is missing its definition - it involves Sequelize and consign

Just starting out in the world of Node.js. I'm currently working on integrating Sequelize into a basic application with Consign. In my "config/db.js" file: var Sequelize = require('sequelize'); var sequelize = new Sequelize('test&ap ...

Converting php array submitted from a form using ajax

I have a form on my website that collects user input and sends it to an email address using php. The form includes a checkbox input where users can select multiple options, which are then stored in an array. However, when the form is submitted, the email r ...

Troubleshooting JavaScript Oscilloscope: resolving audio playback problems

I am exploring JavaScript for the first time and came across an interesting oscilloscope example on this GitHub page. It seemed quite easy to follow initially, but I am facing an issue with audio playback. Although the HTML5 audio element loads the audio f ...

Terminate child process with specified user ID using the Forever-monitor

Whenever I need to create new child node processes, I use the following code: var forever = require('forever-monitor'); function startNodeProcess(envVariables, jsFileName, uid) { var child = new (forever.Monitor)(jsFileName, { ...

The HTML5 range input is consistently defaulting to the minimum value and is not triggering the onChange event for text input

My goal is to implement a feature where an input type text is used along with an input type range with three specific use cases: 1. Default case: When the page loads, the slider should point to the value specified in the text input field. 2. When a user e ...

Error Occurred while Uploading Images using Ajax HTML Editor with JSON Data

I am facing an issue with my AJAX HtmlEditorExtender, specifically when trying to upload an image. The error message I receive is as follows: JavaScript runtime error: Sys.ArgumentException: Cannot de-serialize. The data does not correspond to valid JSON. ...

Submitting Multi-part forms using JQuery/Ajax and Spring Rest API

Recently, I started exploring JQuery and decided to experiment with asynchronous multipart form uploading. The form includes various data fields along with a file type. On the server side (using Spring), I have set up the code as follows: @RequestMapping ...

Setting up AngularJS can be a pain

Greetings, my name is Rahim. While setting up AngularCLI, I ran into the following issue: 'ng' is not recognized as an internal or external command, operable program or batch file. C:\Users\ASUS ROG>ng --version 'ng' is ...

Higher Order Component for JSX element - displaying JSX with wrapped component

I am looking to utilize a ReactJS HOC in order to implement a tooltip around JSX content. The function call should look similar to this: withTooltip(JSX, "very nice") To achieve this, I have created the following function: import React from "re ...

Is the script failing to retrieve the innerHTML content?

Here is a snippet of HTML code: <div id="team_players"> <h3>Players</h3> <button class="bold-btn" onclick="teamAct('player_list');">Refresh List ↻</button> <table> <thead> <tr> ...

Reviewing user input for any inappropriate characters using jQuery's functionality

When a username is inputted into the input box, I want to make sure that only valid characters are accepted. The following code shows what I have so far; but what should I replace "SOMETHING" with in the regular expression? var numbers = new RegExp( ...

Can we create a collection of Vue highcharts components in a library format?

I am in search of an answer, with hope that it lies here. I have used the following: "vite": "^2.7.13", "highcharts": "^9.3.3", "highcharts-vue": "^1.4.0" I aim to create a library of Vue compon ...

Continuously send HTTP requests to the page until receiving a status code of 200, then proceed to redirect to the same page

I am new to JavaScript and seeking advice on how to tackle this task. I have attempted the following approach, but it is not functioning correctly: while (status !== 200) { checkPage(); } function checkPage() { var xhr = new XMLHttpReque ...

Guide on utilizing the carousel component in Bootstrap and populating it with data generated from Node.js

My goal is to design a carousel that displays 5 different pieces of information pulled from a separate app.js file. I attempted to implement a forEach loop, but encountered issues when trying to create a second Bootstrap carousel container. Here's th ...

Oops! The connection timed out while trying to format the error in SMTPConnection

Encountering an error with a connection timeout while trying to send emails using nodemailer. Seeking assistance as the console keeps showing this error message: Error: Connection timeout at SMTPConnection._formatError (/home/codabae/Desktop/mailmonster/B ...

"Exploring the various configurations for session handling in NodeJs

I am trying to implement a login system using the express-session module. I'm unsure if I have set everything up correctly, especially when it comes to the secret option. Currently, my initialization code for express-session looks like this: app.use( ...

The power of relative URLs in AJAX calls

Why does Javascript handle relative URLs differently than standard HTML? Consider the URL provided: http://en.wikipedia.org/wiki/Rome. Launch a Firebug console (or any other Javascript console) and type in the following: var x = new XMLHttpRequest(); x.op ...

Retrieving information from Firebase after updating it

My goal is to automatically navigate to a specific ID after adding an item to my realtime database. Despite following the documentation's proposed solution, I am encountering issues with its implementation. Following the use of the push().set() meth ...

Decomposition of words in VueJS based on numerical values

Is there a way to handle word declension based on the number of items in VueJS? For example, displaying "0 skins", "1 skin", "2 skins", "3 skins", "4 skins", "5 skins", and so on. I currently have a basic code snippet with hardcoded text: <div class=&q ...

What is the rationale behind TypeScript's decision to permit omission of "this" in a method?

The TypeScript code below compiles without errors: class Something { name: string; constructor() { name = "test"; } } Although this code compiles successfully, it mistakenly assumes that the `name` variable exists. However, when co ...