Refinements not working as expected

We are using searchDisjunctiveFacetingAsync method with disjuctiveFacests and refinements but it is not working as expected.

Query with setFilters
[“inStock:1”,“approvalStatus:1”,“allCategories:home”,“badge.title.en:Clearance”,[“badge.title.en:Clearance”,“badge.title.en:New”]]

the same request how to send by using refinements as Map<String, List>?

because duplicate key values not allowed in HashMap. please suggest me to proceed further development.

Thanks

Hi @raghu.gangireddy123,

Thank you for posting on discourse! Here is an example of how you can use searchDisjunctiveFacetingAsync:

final Query query = new Query("phone").setFacets("brand", "category", "stars");
final List<String> disjunctiveFacets = Collections.singletonList("brand");
final Map<String, List<String>> refinements = new HashMap<>();
refinements.put("brand", Arrays.asList("Apple", "Samsung")); // disjunctive facet
refinements.put("category", Collections.singletonList("device")); // conjunctive facet
index.searchDisjunctiveFacetingAsync(query, disjunctiveFacets, refinements, new AssertCompletionHandler() {
        @Override
        public void doRequestCompleted(JSONObject content, AlgoliaException error) {
            if (error != null) {
                fail(error.getMessage());
            } else {
                // Logic here using content
            }
        }
    });
}

Hi Guy Daher,

Thanks for your reply but if i pass the values as below it is not working as expected.

final Query query = new Query(“phone”).setFacets(“inStock”, “approvalStatus”, “allCategories”, “badge.title.en”);
final List disjunctiveFacets = Collections.singletonList(“badge.title.en”);
final Map<String, List> refinements = new HashMap<>();
refinements.put(“badge.title.en”, Arrays.asList(“Clearance”, “New”)); // disjunctive facet
refinements.put(“inStock”, Collections.singletonList(“1”)); // conjunctive facet
refinements.put(“approvalStatus”, Collections.singletonList(“1”)); // conjunctive facet
refinements.put(“allCategories”, Collections.singletonList(“home”)); // conjunctive facet
index.searchDisjunctiveFacetingAsync(query, disjunctiveFacets, refinements, new AssertCompletionHandler() {
@Override
public void doRequestCompleted(JSONObject content, AlgoliaException error) {
if (error != null) {
fail(error.getMessage());
} else {
// Logic here using content
}
}
});
}

Maybe because you forgot to put brand in your list of facets in setFacets ?

we put facet in query as below. I have updated above request and we don’t have brand as facet and we only have “badge.title.en”
query.setFacets("*");

Hi,

It is working if pass as below.

final Query query = new Query(“phone”).setFacets(*);
final List disjunctiveFacets = Collections.singletonList(“badge.title.en”);
final Map<String, List> refinements = new HashMap<>();
refinements.put(“badge.title.en”, Arrays.asList(“Clearance”)); // disjunctive facet
refinements.put(“inStock”, Collections.singletonList(“1”)); // conjunctive facet
refinements.put(“approvalStatus”, Collections.singletonList(“1”)); // conjunctive facet
refinements.put(“allCategories”, Collections.singletonList(“home”)); // conjunctive facet
index.searchDisjunctiveFacetingAsync(query, disjunctiveFacets, refinements, new AssertCompletionHandler() {
@Override
public void doRequestCompleted(JSONObject content, AlgoliaException error) {
if (error != null) {
fail(error.getMessage());
} else {
// Logic here using content
}
}
});
}

but not working if we pass list of values in disjunctive facet as below

final Query query = new Query(“phone”).setFacets(*);
final List disjunctiveFacets = Collections.singletonList(“badge.title.en”);
final Map<String, List> refinements = new HashMap<>();
refinements.put(“badge.title.en”, Arrays.asList(“Clearance”, “New”)); // disjunctive facet
refinements.put(“inStock”, Collections.singletonList(“1”)); // conjunctive facet
refinements.put(“approvalStatus”, Collections.singletonList(“1”)); // conjunctive facet
refinements.put(“allCategories”, Collections.singletonList(“home”)); // conjunctive facet
index.searchDisjunctiveFacetingAsync(query, disjunctiveFacets, refinements, new AssertCompletionHandler() {
@Override
public void doRequestCompleted(JSONObject content, AlgoliaException error) {
if (error != null) {
fail(error.getMessage());
} else {
// Logic here using content
}
}
});
}

Hmm that’s odd… How about use setFacets as described in the guide here. Also check if you didn’t forget to do something from there.

Although it should work… Did you check also your attributesForFaceting?

it is giving response but not correct as compare to another platforms(iOS, Web) because of it is expecting “badge.title.en:Clearance” but we can’t put multiple key values with same name in HashMap.
we are using query.setAttributesToRetrieve(*);

Our question is why are you guys expecting same key values as below. why do we need to pass two times as “badge.title.en:Clearance”?

facetFilters:
[“inStock:1”,“approvalStatus:1”,“allCategories:home”,“badge.title.en:Clearance”,[“badge.title.en:Clearance”,“badge.title.en:New”]]

You do not need to pass in 2 times, one time should be sufficient.

Can you please tell me what differences you’re getting?

See the responses for the same request in both Android and Web respectively as below. in Web they are passing two times (“badge.title.en:Clearance”) by using setFilters with the query but in Android not feasible to send two times because HashMap will not allow duplicate key values.

ANDROID
{
“hits”:,
“nbHits”:500,
“page”:0,
“nbPages”:25,
“hitsPerPage”:20,
“processingTimeMS”:5,
“facets”:{},
“facets_stats”:{},
“exhaustiveFacetsCount”:true,
“exhaustiveNbHits”:true,
“query”:"",
“queryAfterRemoval”:"",
“params”:“facetFilters=%28inStock%3A1%29%2C%28allCategories%3Ahome%29%2C%28badge.title.en%3AClearance%2Cbadge.title.en%3ANew%29%2C%28approvalStatus%3A1%29&facets=%5B%22*%22%5D&getRankingInfo=true&hitsPerPage=20&numericFilters=%5B%22price%20%3E%201%22%2C%22price%20%3E%201%22%5D&page=0&tagFilters=%5B%22lifestyle%22%5D”,
“index”:“prod_uae_Product”,
“serverUsed”:“d4-sg-2.algolia.net”,
“parsedQuery”:"",
“timeoutCounts”:false,
“timeoutHits”:false,
“disjunctiveFacets”:{}
}

WEB

{
“results”:[
{
“hits”:,
“nbHits”:22,
“page”:0,
“nbPages”:1,
“hitsPerPage”:42,
“processingTimeMS”:2,
“facets”:{},
“facets_stats”:{},
“exhaustiveFacetsCount”:true,
“exhaustiveNbHits”:true,
“query”:"",
“queryAfterRemoval”:"
",
“params”:“query=&hitsPerPage=42&page=0&facets=&facetFilters=%5B%22inStock%3A1%22%2C%22approvalStatus%3A1%22%2C%22allCategories%3Ahome%22%2C%22badge.title.en%3AClearance%22%2C%5B%22badge.title.en%3AClearance%22%2C%22badge.title.en%3ANew%22%5D%5D&getRankingInfo=1&attributesToHighlight=null&attributesToRetrieve=concept%2CmanufacturerName%2Curl%2C333WX493H%2C345WX345H%2C505WX316H%2Cbadge%2Cname%2CwasPrice%2Cprice%2CemployeePrice%2CshowMoreColor&numericFilters=price%20%3E%201&query=*&maxValuesPerFacet=200&tagFilters=%5B%5B%22lifestyle%22%5D%5D”,
“index”:“prod_uae_Product”,
“serverUsed”:“t13-de-1.algolia.net”,
“parsedQuery”:"",
“timeoutCounts”:false,
“timeoutHits”:false
},
{}
]
}

1 Like

Comparing the 2 params of the 2 requests above:

"params":"facetFilters=(inStock:1),(allCategories:home),(badge.title.en:New),(approvalStatus:1)&facets=["*"]&getRankingInfo=true&hitsPerPage=20&numericFilters=["price > 1","price > 1"]&page=0&tagFilters=["lifestyle"]",

"params":"query=&hitsPerPage=42&page=0&facets=&facetFilters=["inStock:1","approvalStatus:1","allCategories:home","badge.title.en:Clearance",["badge.title.en:Clearance","badge.title.en:New"]]&getRankingInfo=1&attributesToHighlight=null&attributesToRetrieve=concept,manufacturerName,url,333WX493H,345WX345H,505WX316H,badge,name,wasPrice,price,employeePrice,showMoreColor&numericFilters=price > 1&query=*&maxValuesPerFacet=200&tagFilters=[["lifestyle"]]",

The web one has badge.title.en:Clearance while the Android one doesn’t have it in the params.

Just to be sure: did you use on Android this line:

refinements.put("badge.title.en", Arrays.asList("Clearance", "New")); // disjunctive facet

And you are getting the result above? If that’s the case, can you please open a Github Issue here referring to this discourse thread, and we’ll take a deeper look at it.

Thank you for reporting this!

I have updated Android response in above, please have a look into that and let me know.

Thanks

Ahhh okay now I understand what you mean by:

In Web they are passing two times (“badge.title.en:Clearance”) by using setFilters with the query but in Android not feasible to send two times because HashMap will not allow duplicate key values.

Are you sure you’re not making a mistake on the Web? My guess is that the Android query is correct, but the web one is incorrect. This part on the web params is weird:

"badge.title.en:Clearance",["badge.title.en:Clearance","badge.title.en:New"]

Why was the Clearance one repeated and not the New one? can you make sure you didn’t accidentally add the Clearance one twice on your web code?

Hey @raghu.gangireddy123, any updates on the above?

@guy.daher

how can i add map [“inStock:1”,“approvalStatus:1”,[“badge.title.en:Clearance”]] in android.

Hi @ajay.kancha,

Just to be clear about your section: are you referring to this answer or something else?

Hi Guy,

if the query is wrong or weird from the Web, how it will return response from Algolia API? it should thrown error/exception right? so response return by android query is correct?

@raghu.gangireddy123 Indeed, if the query is wrong, the Algolia API will throw an error. Check here to see how to do error handling.

Hi Guy,

  1. Okay thanks, As per your previous reply the query from the Web is incorrect or weird so can you please let us know is the results return by the Web is also incorrect?

  2. the response return by android query is correct?

Regards,
Raghu