Anubhav Kalra iDigi
Anubhav Kalra iDigi

Reputation: 21

Retrieve Liferay commerce products with any status using elastic search API

I need to show all the commerce products which lies in same catalog, have same type and have same vendor user irrespective of their statuses. To do this, I've written the following java code to query elastic. However, I'm only able to fetch the approved products.

void someMethod() {
    BooleanQuery booleanQuery = queries.booleanQuery();

    TermQuery catalogTermQuery = queries.term("commerceCatalogId", catalogId);
    booleanQuery.addMustQueryClauses(catalogTermQuery);

    TermQuery rcEligibleTermQuery = queries.term("rc_eligible_custom_field_name", Boolean.TRUE);
    booleanQuery.addMustQueryClauses(rcEligibleTermQuery);

    TermsQuery statusTermsQuery = queries.terms("status");
    statusTermsQuery.addValues("0", "1", "2", "3");

    booleanQuery.addShouldQueryClauses(statusTermsQuery);

    if (!isSuperAdmin) {
                MatchQuery vendorIdMatchQuery = queries.match("vendor_custom_field_name", vendorId);
                booleanQuery.addMustQueryClauses(vendorIdMatchQuery);
    }

    SearchResponse searchResponse = getSearchResponse(booleanQuery, companyId, start, end);

}

private SearchResponse getSearchResponse(BooleanQuery booleanQuery, long companyId, int start, int end) {

    SearchRequestBuilder searchRequestBuilder = searchRequestBuilderFactory.builder();

    searchRequestBuilder.emptySearchEnabled(Boolean.TRUE);

    searchRequestBuilder.withSearchContext(searchContext -> {
        searchContext.setCompanyId(companyId);
        searchContext.setEntryClassNames(new String[]{CPDefinition.class.getName()});
        searchContext.setStart(start);
        searchContext.setEnd(end);
        Sort sort = SortFactoryUtil.create("modified_sortable", Sort.STRING_TYPE, Boolean.TRUE);
        searchContext.setSorts(sort);
    });

    SearchRequest searchRequest = searchRequestBuilder.query(booleanQuery).postFilterQuery(booleanQuery).build();

    return searcher.search(searchRequest);

}

The above code is working fine for the approved products. But I need to fetch the draft and expired products as well. For which I wrote the below code which is not working, not sure whether the below code is correct or not:

TermsQuery statusTermsQuery = queries.terms("status");
statusTermsQuery.addValues("0", "1", "2", "3");
booleanQuery.addShouldQueryClauses(statusTermsQuery);

NOTE: 0, 1, 2, 3 are the status values for Approved, Pending, Draft, & Expired workflow statuses.

Search Request body generated after the execution of the above code:

{
   "from":0,
   "size":10000,
   "query":{
      "bool":{
         "must":[
            {
               "bool":{
                  "must":[
                     {
                        "term":{
                           "commerceCatalogId":{
                              "value":38051
                           }
                        }
                     },
                     {
                        "term":{
                           "expando__keyword__custom_fields__RC_Eligible":{
                              "value":true
                           }
                        }
                     }
                  ],
                  "should":[
                     {
                        "terms":{
                           "status":[
                              "0",
                              "1",
                              "2",
                              "3"
                           ]
                        }
                     }
                  ]
               }
            },
            {
               "bool":{
                  "filter":[
                     {
                        "bool":{
                           "must":[
                              {
                                 "term":{
                                    "companyId":{
                                       "value":"20096"
                                    }
                                 }
                              },
                              {
                                 "bool":{
                                    "should":[
                                       {
                                          "bool":{
                                             "must":[
                                                {
                                                   "term":{
                                                      "entryClassName":{
                                                         "value":"com.liferay.commerce.product.model.CPDefinition"
                                                      }
                                                   }
                                                },
                                                {
                                                   "term":{
                                                      "hidden":{
                                                         "value":"false"
                                                      }
                                                   }
                                                },
                                                {
                                                   "term":{
                                                      "hidden":{
                                                         "value":"false"
                                                      }
                                                   }
                                                },
                                                {
                                                   "terms":{
                                                      "status":[
                                                         "0"
                                                      ]
                                                   }
                                                },
                                                {
                                                   "terms":{
                                                      "status":[
                                                         "0"
                                                      ]
                                                   }
                                                },
                                                {
                                                   "bool":{
                                                      "must":[
                                                         {
                                                            "bool":{
                                                               "must_not":[
                                                                  {
                                                                     "exists":{
                                                                        "field":"ctCollectionId"
                                                                     }
                                                                  }
                                                               ]
                                                            }
                                                         }
                                                      ]
                                                   }
                                                },
                                                {
                                                   "bool":{
                                                      "must":[
                                                         {
                                                            "bool":{
                                                               "must_not":[
                                                                  {
                                                                     "exists":{
                                                                        "field":"ctCollectionId"
                                                                     }
                                                                  }
                                                               ]
                                                            }
                                                         }
                                                      ]
                                                   }
                                                }
                                             ]
                                          }
                                       }
                                    ]
                                 }
                              }
                           ],
                           "must_not":[
                              {
                                 "terms":{
                                    "groupId":[
                                       "38456",
                                       "38479"
                                    ]
                                 }
                              }
                           ]
                        }
                     }
                  ]
               }
            }
         ]
      }
   },
   "explain":false,
   "stored_fields":"*",
   "sort":[
      {
         "modified_sortable":{
            "order":"desc",
            "unmapped_type":"keyword"
         }
      }
   ],
   "track_scores":true,
   "track_total_hits":2147483647
}

I don't know why an extra Boolean query is appending in the search query.

Can someone help me out with this? As mentioned, I am expecting to get all the products irrespective of their statuses.

Upvotes: 1

Views: 47

Answers (0)

Related Questions