on 2022 Jul 14 3:08 PM
Hi Need to search products with specific code and specific brand
Example
product with code 1010 and brand XYZ => OK
product with code 1010 and brand ABC => NOK
i have create SolrSearchQueryTemplate but it form query based on OR operator which does not serve my purpose
current query being formed q=(code_string:1010)+OR+(brand_string:XYZ)
expected query to be formed q=(code_string:1010)+AND+(brand_string:XYZ)
please find below implementation i have done for this
$productType=testProductType
$ftsQueryBuilder=multiFieldFreeTextQueryBuilder
INSERT_UPDATE SolrSearchQueryTemplate;name[unique=true];ftsQueryBuilder[default=$ftsQueryBuilder];showFacets;restrictFieldsInResponse;indexedType(Identifier)[default=$productType]
;THIRDPARTYINTEGRATION;;false;true;
INSERT_UPDATE SolrSearchQueryProperty;ftsQuery;indexedProperty(name)[unique=true];searchQueryTemplate(name)[unique=true]
;true;code;THIRDPARTYINTEGRATION
;true;brand;THIRDPARTYINTEGRATION<br>
final PageableData pageableData = createPageableData(0, getSearchPageSize(), null, ShowMode.Page);
final SearchStateData searchState = new SearchStateData();
final SearchQueryData searchQueryData = new SearchQueryData();
searchQueryData.setValue(XSSFilterUtil.filter(prodIdentifier.get(0)));
searchQueryData.setSearchQueryContext(SearchQueryContext.THIRDPARTYINTEGRATION);
searchState.setQuery(searchQueryData);
pageableData.setFlow(MyConstants.THIRDPARTYINTEGRATION);
final ProductSearchPageData<SearchStateData, de.hybris.platform.commercefacades.product.data.ProductData> searchPageData = testCloudProductSearchFacade
.getProductByCodeMafIdBrand(searchState, pageableData, MyConstants.THIRDPARTYINTEGRATION);
List<ProductData> prodFromSolr = searchPageData.getResults();<br>
Note currently i am using multiFieldFreeTextQueryBuilder for my SolrSearchQueryTemplate.
Let me know if any other OOTB builder can serve this purpose or else what modifications are required to be done in this in order to achieve this
Request clarification before answering.
I found two ways to deal with this scenario
Problem statement : to have mandatory fields attached to solr query with AND operator
in this case mandatory attribute is Brand
Example: product with code 1010 AND brand XYZ
Approach 1 : Using Search Query Post-Processors Populator
This populator would get called while solr query is being prepared
In this we can pass fq parameter (Filter Query). fq parameter has fields that are mandatory to be satisfied
Step 1 : prepare query string as given below
brandCode_string:(brandA OR brandB)
Step 2 : add fq param to SolrQuery
query.add("fq", "brandCode_string:(brandA OR brandB)");
Approach 2 : Extend OOTB Query Builder and assign it to SolrSearchQueryTemplate
Step 1 : extend OOTB Query Builder which you wish to use with your SolrSearchQueryTemplate
public class CustomMultiFieldFreeTextQueryBuilder extends MultiFieldFreeTextQueryBuilder
Step 2 : Override super class method
protected String buildQuery(SearchQuery searchQuery, Map<String, List<String>> queryFields)
Step 3 : Prepare query as per requirement
queryFields has fields and its values, you can play with it to form query string by placing AND/OR operators the way you want
Step 4 : Assign this new query builder to your SolrSearchQueryTemplate
$productType=testProductType
$ftsQueryBuilder=customMultiFieldFreeTextQueryBuilder
INSERT_UPDATE SolrSearchQueryTemplate;name[unique=true];ftsQueryBuilder[default=$ftsQueryBuilder];showFacets;restrictFieldsInResponse;indexedType(Identifier)[default=$productType]
;THIRDPARTYINTEGRATION;;false;true;
Let me know if there is any even more better approach for this
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
19 | |
17 | |
3 | |
2 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.