I want to display products that are relevant to their query using Elasticsearch. I created system but failing to get products like iPhone 15 and all bcoz in my implementation I am trying to find the closeness of user's query with product's description that leads to results such as 15 litre utensil and all
how to solve this?
EDIT:
actually i have lot of products in db and I am trying to defined generalize query that can be used to fetch relevant data to users query
Query is followed like this:
query: {
bool: {
should: [
{ match: { name: { query: query } } },
{ match: { search_text: { query: query, boost: 1 } } },
{ match_phrase_prefix: { name: { query: query } } },
{ match_phrase: { name: { query: query, slop: 5 } } },
{ match: { category: { query: query } } },
{ match: { brand: { query: query } } },
{ term: { name: { value: query, boost: 10 } } },
{ term: { category: { value: query, boost: 5 } } },
{ term: { brand: { value: query, boost: 10 } } }
] + should_body,
must_not: must_not_clauses,
filter: filter_body,
minimum_should_match: [should_body.length, 3].min
}
}
Hello,
If you want to return iphone 15, maybe we can use bool query and it must match both the tokens iphone & 15. This will only return iphone 15 but to provide a better solution need to understand the data & what fields are there , what is the current query used?
Also can boost a particular field so that score can be increased for that field.
Thanks!!
Often the best approach is to have a mix of strict and lax interpretations of what the user types combined in the “should” clause of a bool query.
Matches on strict clauses can be rewarded with your choice of a high “boost” setting while laxer clauses can be given lower boosts. In order of strictness the clause types you could combine are:
- rule query
- phrase query (all the words in the right order)
- match query with AND operator (matches all the words)
- match query with OR operator (matches any of the words
The rule query offers most control because it can avoid the problem of “iPhone 15” searches returning iPhone 15 cases above iPhone 15s. Using the rule queries for your most popular searches you can pick precisely the products you need to see in responses.
updated my question added some info actually I already defined some rules in query to fetch products but getting wrong results for iphone as it is defined under mobile category and brand is apple so
The _explain api is your friend here to understand scoring.
Your queries across multiple fields likely suffer from the default IDF scoring - rewarding rare words more highly than common ones. This is a sensible policy when searching one field but when searching multiple fields it tends to favour matching the most bizarre context for a term. For example, the word “apple” will be a common entry in the “brand” field but if it appears once in a “category” field (e.g. “apple peelers”) then that product would be rewarded most highly when searching for apple.
The cross fields setting helps tackle that.
Whatever you do you should start with a good understanding of the problem you are trying to fix and the _explain api will help with this.