Skip to content

Commit

Permalink
Update facets parameters to fit latest API changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
houdejun214 committed Mar 29, 2017
1 parent 4d8f59c commit 0d4c50c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 30 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<groupId>com.visenze</groupId>
<artifactId>visearch-java-sdk</artifactId>
<name>ViSearch Java SDK</name>
<version>1.4.3</version>
<version>1.5.0</version>
<packaging>jar</packaging>
<url>https://github.com/visenze/visearch-sdk-java</url>
<description>ViSearch Java SDK</description>
Expand Down
59 changes: 36 additions & 23 deletions src/main/java/com/visenze/visearch/BaseSearchParams.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.visenze.visearch;

import com.google.common.base.Joiner;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.collect.HashMultimap;
Expand All @@ -16,7 +17,6 @@
*/
public class BaseSearchParams<P extends BaseSearchParams<P>> {

private static final Boolean DEFAULT_FACET = false;
private static final List<String> DEFAULT_FACET_FIELD = Lists.newArrayList();
private static final Boolean DEFAULT_SCORE = false;
private static final Map<String, String> DEFAULT_FQ = new HashMap<String, String>();
Expand All @@ -28,8 +28,9 @@ public class BaseSearchParams<P extends BaseSearchParams<P>> {

protected Optional<Integer> page = Optional.absent();
protected Optional<Integer> limit = Optional.absent();
protected Optional<Boolean> facet = Optional.absent();
protected Optional<List<String>> facetField = Optional.absent();
protected Optional<List<String>> facets = Optional.absent();
protected Optional<Integer> facetsLimit = Optional.absent();
protected Optional<Boolean> facetsShowCount = Optional.absent();
protected Optional<Boolean> score = Optional.absent();
protected Optional<Float> scoreMin = Optional.absent();
protected Optional<Float> scoreMax = Optional.absent();
Expand All @@ -55,14 +56,8 @@ public P setLimit(Integer limit) {
}

@SuppressWarnings("unchecked")
public P setFacet(Boolean facet) {
this.facet = Optional.fromNullable(facet);
return (P) this;
}

@SuppressWarnings("unchecked")
public P setFacetField(List<String> facetField) {
this.facetField = Optional.fromNullable(facetField);
public P setFacets(List<String> facetField) {
this.facets = Optional.fromNullable(facetField);
return (P) this;
}

Expand Down Expand Up @@ -126,6 +121,18 @@ public P setDedupThreshold(Float dedupThreshold) {
return (P) this;
}

@SuppressWarnings("unchecked")
public P setFacetsLimit(Integer facetsLimit) {
this.facetsLimit = Optional.fromNullable(facetsLimit);
return (P) this;
}

@SuppressWarnings("unchecked")
public P setFacetsShowCount(Boolean facetsShowCount) {
this.facetsShowCount = Optional.fromNullable(facetsShowCount);
return (P) this;
}

public Integer getPage() {
return page.orNull();
}
Expand All @@ -134,12 +141,8 @@ public Integer getLimit() {
return limit.orNull();
}

public boolean isFacet() {
return facet.or(DEFAULT_FACET);
}

public List<String> getFacetField() {
return facetField.or(DEFAULT_FACET_FIELD);
public List<String> getFacets() {
return facets.or(DEFAULT_FACET_FIELD);
}

public boolean isScore() {
Expand Down Expand Up @@ -183,6 +186,14 @@ public Float getDedupThreshold() {
return dedupThreshold.orNull();
}

public Integer getFacetsLimit() {
return facetsLimit.orNull();
}

public Boolean getFacetsShowCount() {
return facetsShowCount.orNull();
}

public Multimap<String, String> toMap() {
Multimap<String, String> map = HashMultimap.create();

Expand All @@ -193,13 +204,15 @@ public Multimap<String, String> toMap() {
map.put("limit", getLimit().toString());
}

if (isFacet() && !getFacetField().isEmpty()) {
map.put("facet", "true");
for (String facetFieldItem : getFacetField()) {
map.put("facet_field", facetFieldItem);
}
if (!getFacets().isEmpty()) {
map.put("facets", Joiner.on(",").join(getFacets()));
}
if (facetsLimit.isPresent()) {
map.put("facets_limit", facetsLimit.get().toString());
}
if (facetsShowCount.isPresent()) {
map.put("facets_show_count", facetsShowCount.get().toString());
}

if (isScore()) {
map.put("score", "true");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,15 @@ public void testSearchParamsFacet() {
when(mockClient.get(anyString(), Matchers.<Multimap<String, String>>any())).thenReturn(response);
SearchOperations searchOperations = new SearchOperationsImpl(mockClient, objectMapper);
SearchParams searchParams = new SearchParams("test_im")
.setFacet(true)
.setFacetField(Lists.newArrayList("brand"));
.setFacets(Lists.newArrayList("brand"))
.setFacetsLimit(10)
.setFacetsShowCount(true);
searchOperations.search(searchParams);
Multimap<String, String> expectedParams = HashMultimap.create();
expectedParams.put("im_name", "test_im");
expectedParams.put("facet", "true");
expectedParams.put("facet_field", "brand");
expectedParams.put("facets", "brand");
expectedParams.put("facets_limit", "10");
expectedParams.put("facets_show_count", "true");
verify(mockClient).get("/search", expectedParams);
}

Expand Down Expand Up @@ -150,8 +152,7 @@ public void testSearchResponseFacet() {
when(mockClient.get(anyString(), Matchers.<Multimap<String, String>>any())).thenReturn(response);
SearchOperations searchOperations = new SearchOperationsImpl(mockClient, objectMapper);
SearchParams searchParams = new SearchParams("test_im")
.setFacet(true)
.setFacetField(Lists.newArrayList("brand"));
.setFacets(Lists.newArrayList("brand"));
PagedSearchResult pagedResult = searchOperations.search(searchParams);
assertEquals(new Integer(1), pagedResult.getPage());
assertEquals(new Integer(1), pagedResult.getLimit());
Expand Down

0 comments on commit 0d4c50c

Please sign in to comment.