UpStart Commerce Search
Query & Query Rules
29min
query query is the most crucial part of the search platform they are the input provided by the user to match things in a certain indexed document it is an opensearch concept the queries can be simple text entries or highly filtered ones with multiple parameters such as boosts, burying, facets, and other filters to modify the search accordingly the parameters of queries can be vendor specific as well query rules query rules are used to define the conditions based on which a query matches the available information in the index and thus triggers a specific action in response they are a pure nochannel search concept each query rule is specific to the site there are four different types of query rules 1\ filter according to this query rule, you can create filters for a query based on various properties such as brand name, manufacturer name, prices, and other similar fields the following code block is an example of how to create a query rule with filters in it, the value of the filter is applied to the type name of the product the value is set to "mattress" this means every product that has the term mattress in the type name in it, will be shown in the results post https //api upstartcommerce com/v1/search/filter "filter" { "displayname" "filter mattress", "metadata" {}, "priority" 1, "status" "active", "condition" { "value" "mattress", "field" { "type" "anyfield" }, "type" "querycontainsvalue" }, "filters" \[ { "term" { "typename keyword" { "value" "mattress" } } } ] } important parameters in the code block are condition this code block defines the condition based on which a query rule is to be applied in the case of the example above, if the value of the query is "mattress", then the respective filter will become applicable to understand the functionality of conditions, it is important to know the query algebra docid\ pvas1s ry8ay3y u8lwx filters this code block defines the filter that will be applied in the example above, if a query corresponds to "mattress" then the filter has to return all the products with the same type name in them using a similar code structure, you can update, delete, and perform other crud operations accordingly the only change would be in defining the type field which will change its value to the operation being performed attach the attach function allows filters to exist independently of a query pipeline this means that users can attach one filter with multiple query pipelines the api call to attach the created filter is post https //api upstartcommerce com/v1/search/filter/attach/{resourceid}/to/{querypipelineid} notice that the filter's name/id is added before 'to' and after it, the query pipeline name/id is defined detach the detach function allows users to remove a particular filter from the query pipeline the api call to detach the created filter is post https //api upstartcommerce com/v1/search/filter/detach/{resourceid}/to/{querypipelineid} notice that the id of the filter is added before 'from' and after it, the query pipeline name/id is defined using filter query rule in search via routes following is the representation of the filter query rule in the search code block "search" { "query" { "bool" { "must" \[ { "query string" { "query" "mattress" } } ] } }, "from" 0, "size" 20, "aggs" { "typename term" { "terms" { "field" "typename keyword", "size" 5000, "min doc count" 1 } } } } notice that the term in the query field is "mattress" we created our filter query rule for this term the total number of results is also specified in the search block response without filter to understand the functionality of the filter better, consider the following portion of the response in it, the filter is inactive "result" { "id" "catalog live", "displayname" "live catalog items", "access" "public", "indexes" { "catalog live" { "hits" { "total" { "value" 86, "relation" "eq" }, } } } } notice that the total number of returned values without applying the filter returns 86 this means that there are 86 products that have the term mattress in their description, name, brand name, or other similar fields response with filter "result" { "id" "catalog live", "displayname" "live catalog items", "access" "public", "indexes" { "catalog live" { "hits" { "total" { "value" 48, "relation" "eq" }, }, }, }, } notice that the total number of returned values after applying the filter has changed to 48 this means that there are 48 products that have the term mattress in their typename 2\ boost in this type of query rule, specific terms are given a boost this means that if the specified boosted term is present in the query, then the results with the boosted term will appear on top users can apply boosts on multiple terms in the same query the results will be arranged based on boost priority following is an example of how to create a boost based query rule a boost query rule called protector is created and the value of the boost is set to 100 this means that if a user makes a query that contains the word protector in it, all the items with the word "protector" in them will be shown at the top of the search results post https //api upstartcommerce com/v1/search/boosts "boost" { "displayname" "boost protector", "metadata" {}, "priority" 0, "status" "active", "condition" { "terms" \[ { "field" "siteids", "value" "201cb789 4198 488b a5eb 4e7df0fb4bee", "type" "queryhasfield" }, "value" "mattress protector", "field" { "type" "emptyfield" }, "type" "queryhasvalue" ], "type" "and" }, "boosts" { "terms" \[ "protector" ], "boost" 100 0 } } other important fields are condition block here you will notice that the value is set to mattress protector boosts block in the boosts code block, the list of terms has "protector" a boost of "100 0" is set in the next field this means that the "protector" term will get a 100 times boost attach the attach function allows boosts to exist independently of a query pipeline this means that users can attach one boost with multiple query pipelines the api call to attach the created boost is post https //api upstartcommerce com/v1/search/boosts/attach/{resourceid}/to/{querypipelineid} notice that the boost's name/id is added before 'to' and after it the query pipeline name/id is defined detach the detach function allows users to remove a particular boost from the query pipeline the api call to detach the created boost is post https //api upstartcommerce com/v1/search/boosts/detach/{resourceid}/from/{querypipelineid} notice that the id of the boost is added before 'from' and after it the query pipeline name/id is defined using boost query rule in search via routes following is the representation of the boost query rule in the search code block "search" { "query" { "bool" { "must" \[ { "query string" { "query" "mattress protector" } } ] } }, "from" 0, "size" 3 } notice that the term in the query field is "mattress protector" we created our boost query rule for this term the total number of search results to be displayed is also specified in the search block which is 3 response with boost the following code block represents a snippet of the response generated when a boost is applied "result" { "id" "catalog live", "displayname" "live catalog items", "access" "public", "indexes" { "catalog live" { "hits" \[ { " source" { "type" "product", "typename" "mattress", "name" "your choice mattress protector", "brandname" "abcmattress", "manufacturername" "mjematt", }, " source" { "type" "product", "typename" "mattress", "name" "euro comfort top mattress", "brandname" "lmnmattress", "manufacturername" "xyz mattress", }, " source" { "type" "product", "typename" "mattress", "name" "top easy mattress", "brandname" "pqrmattress", "manufacturername" "xyz mattress", }, }, ], }, } } notice that the field "name" of the product "your choice mattress protector" contains the word "mattress protector" (the boosted word) this is why it appears on the top of all the other products response without boost the following code block represents a snippet of the response generated without any applied boost "result" { "id" "catalog live", "displayname" "live catalog items", "access" "public", "indexes" { "catalog live" { "hits" \[ { " source" { "type" "product", "typename" "mattress", "name" "euro comfort top mattress", "brandname" "lmnmattress", "manufacturername" "xyz mattress", }, " source" { "type" "product", "typename" "mattress", "name" "top easy mattress", "brandname" "pqrmattress", "manufacturername" "xyz mattress", }, " source" { "type" "product", "typename" "mattress", "name" "your choice mattress protector", "brandname" "abcmattress", "manufacturername" "mjematt", }, } ] } } } notice that the field "name" of the product "your choice mattress protector" no longer appears at the top because the boost is not applied in this case 3\ redirect according to this query rule, you can redirect users to a product page for a specific query based on various properties such as brand name, manufacturer name, prices, and other similar fields for example, if there is a redirect rule in place for the search term "iphone", and a query with the search term iphone is made, it will be redirected to the specified iphone's brand page consider the following code block in which a redirect query rule is created every time there is a query with the term iphone in it, the search results will be redirected to the specified path post https //api upstartcommerce com/v1/search/redirect "redirect" { "displayname" "redirect iphone", "metadata" {}, "priority" 1, "status" "active", "condition" { "value" "iphone", "field" { "type" "emptyfield" }, "type" "queryhasvalue" }, "redirect" { "path" "/apple/phones/iphone", "type" "relativeredirect" } }, the important field is redirect the redirect code block defines the url path that the customer will be redirected to when the above conditions i e iphone are searched in the query it also defines the type of redirect which is relativeredirect in the example above attach the attach function allows redirects to exist independently of a query pipeline this means that users can attach one redirect with multiple query pipelines the api call to attach the created redirect is post https //api upstartcommerce com/v1/search/redirect/attach/redirect id/to/querypipeline notice that the redirect's name/id is added before 'to' and after it, the query pipeline name/id is defined detach the detach function allows users to remove a particular redirect from the query pipeline the api call to detach the created redirect is post https //api upstartcommerce com/v1/search/redirect/detach/redirect id/to/querypipeline notice that the id of the redirect is added before 'from' and after it the query pipeline name/id is defined using redirect query rule in search via routes the following code block represents the search code with the query being iphone "search" { "query" { "bool" { "must" \[ { "query string" { "query" "iphone" } } ] } }, "from" 0, "size" 20, "aggs" {} } notice that the term in the query field is "iphone" we created our redirect query rule for this term generated response the following code block represents a snippet of the response generated if the redirect query rule is created successfully "result" { "id" "catalog live", "displayname" "live catalog items", "access" "public", "indexes" { "catalog live" { "queryid" "79f7b2a5 02eb 45a7 9919 df4d4f315c6f", "redirect" { "path" "/apple/phones/iphone", "type" "relativeredirect" }, "type" "searchresponseredirect" } } } 4\ aggregation appenders in this type of query rule, aggregations are appended if a certain condition is satisfied this means that if a particular term is searched in the query, a specific aggregation will be appended in the search results following is an example of a code block to create an aggregation appender query rule in the example below, every time there is a query containing "my mattress" an aggregation will be appended and will create a bucket of the products based on type name post https //api upstartcommerce com/v1/search/aggregation appender "aggregationappender" { "displayname" "aggregation typename", "metadata" {}, "status" "active", "priority" 1, "condition" { "value" "my mattress", "field" { "type" "emptyfield" }, "type" "queryhasvalue" }, "aggregations" \[ { "name" "typename term", "priority" 1, "metadata" { "type" "terms" }, "aggregation" { "terms" { "field" "typename keyword", "size" 10, "order" { " key" "asc" } }, "aggs" {} }, "type" "managedaggregation" } ] }, the important fields are condition block the value is set to "my mattress" which means that the aggregation appender will be triggered if the search query is my mattress aggregations in the aggregations' block, a bucket of aggregations is defined in the field for typename attach the attach function allows aggregation appenders to exist independently of a query pipeline this means that users can attach one aggregation appender with multiple query pipelines the api call to attach the created aggregation appender is post https //api upstartcommerce com/v1/search/aggregations appender/attach/aggregationappender id/to/querypipeline notice that the aggregation appender's name/id is added before 'to' and after it, the query pipeline name/id is defined detach the detach function allows users to remove a particular aggregation appender from the query pipeline the api call to detach the created aggregation appender is post https //api upstartcommerce com/v1/search/aggregations appenders/detach/aggregationsappenders id/from/querypipeline notice that the name or id of the aggregation appender is added before 'from' and after it, the query pipeline name/id is defined using aggregation appenders query rule in search via routes the following code block represents the search code with the query being iphone the result size is set to 2 { "search" { "query" { "bool" { "must" \[ { "query string" { "query" "my mattress" } } ] } }, "from" 0, "size" 2 } } response without aggregation appender query rule the following code block represents a snippet from the generated response once the aggregation appender is applied "result" { "id" "catalog live", "displayname" "live catalog items", "indexes" { "catalog live" { "hits" { "total" { "value" 48, "relation" "eq" }, "aggregations" {}, "type" "searchresponseanswer" } } } } in the example above, it is obvious that since no aggregation appender was applied, no aggregation in the response is empty response with aggregation appender query rule the following code block represents a snippet from the generated once the aggregation appender is applied "result" { "id" "catalog live", "displayname" "live catalog items", "indexes" { "catalog live" { "aggregations" { "typename term" { "doc count error upper bound" 0, "sum other doc count" 0, "buckets" \[ { "key" "mattress", "doc count" 48 } ] } } }, "aggregations" { "typename term" { "priority" 1, "metadata" { "type" "terms" } } }, "type" "searchresponseanswer" } } in the example above, there is an active aggregation appender rule, which is why buckets are created with a "mattress" key the doc count shows that the word "mattress" appears 48 times in the field of the type name of the document