-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
735: implement facet search r=curquiza a=the-sinner # Pull Request ## Related issue Fixes #635 ## What does this PR do? - implement facet search ## PR checklist Please check if your PR fulfills the following requirements: - [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)? - [x] Have you read the contributing guidelines? - [x] Have you made sure that the title is accurate and descriptive of the changes? Thank you so much for contributing to Meilisearch! Co-authored-by: Shalabh Agarwal <shalabhagarwal1024@gmail.com> Co-authored-by: Clémentine <clementine@meilisearch.com>
- Loading branch information
Showing
6 changed files
with
207 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.meilisearch.sdk; | ||
|
||
import com.meilisearch.sdk.exceptions.MeilisearchException; | ||
import com.meilisearch.sdk.model.FacetSearchResult; | ||
import com.meilisearch.sdk.model.FacetSearchable; | ||
|
||
/** | ||
* Class used for performing facet searching on Meilisearch indexes | ||
* | ||
* @see <a href="https://www.meilisearch.com/docs/reference/api/facet_search">API specification</a> | ||
*/ | ||
public class FacetSearch { | ||
private final HttpClient httpClient; | ||
|
||
/** | ||
* Constructor for the Meilisearch Facet Search object | ||
* | ||
* @param config Meilisearch configuration | ||
*/ | ||
protected FacetSearch(Config config) { | ||
httpClient = config.httpClient; | ||
} | ||
|
||
/** | ||
* Performs a facet search on a given index with a given query | ||
* | ||
* @param uid Index identifier | ||
* @param fsr FacetSearchRequest to search on index | ||
* @return search results, as raw data | ||
* @throws MeilisearchException Search Exception or Client Error | ||
*/ | ||
String rawSearch(String uid, FacetSearchRequest fsr) throws MeilisearchException { | ||
String requestQuery = "/indexes/" + uid + "/facet-search"; | ||
if (fsr.getFacetName() == null) { | ||
throw new MeilisearchException("Facet name is required for a facet search"); | ||
} | ||
return httpClient.post(requestQuery, fsr.toString(), String.class); | ||
} | ||
|
||
FacetSearchable facetSearch(String uid, FacetSearchRequest fsr) throws MeilisearchException { | ||
return httpClient.jsonHandler.decode(rawSearch(uid, fsr), FacetSearchResult.class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.meilisearch.sdk; | ||
|
||
import com.meilisearch.sdk.model.MatchingStrategy; | ||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
import lombok.experimental.Accessors; | ||
import org.json.JSONObject; | ||
|
||
/** Facet search request query string builder */ | ||
@Builder | ||
@AllArgsConstructor(access = AccessLevel.PACKAGE) | ||
@NoArgsConstructor(access = AccessLevel.PACKAGE) | ||
@Getter | ||
@Setter | ||
@Accessors(chain = true) | ||
public class FacetSearchRequest { | ||
private String facetName; | ||
private String facetQuery; | ||
private String q; | ||
private MatchingStrategy matchingStrategy; | ||
private String[] attributesToSearchOn; | ||
private String[] filter; | ||
private String[][] filterArray; | ||
|
||
/** | ||
* Constructor for FacetSearchRequest for building facet search queries with the default values: | ||
* facetQuery: null, query: null, matchingStrategy: null, attributesToSearchOn: null, filter: | ||
* null | ||
* | ||
* @param facetName FacetName String | ||
*/ | ||
public FacetSearchRequest(String facetName) { | ||
this(); | ||
this.facetName = facetName; | ||
} | ||
|
||
/** | ||
* Method to set the Query String | ||
* | ||
* <p>This method is an alias of setQ() | ||
* | ||
* @param q Query String | ||
* @return SearchRequest | ||
*/ | ||
public FacetSearchRequest setQuery(String q) { | ||
return setQ(q); | ||
} | ||
|
||
/** | ||
* Method that returns the JSON String of the SearchRequest | ||
* | ||
* @return JSON String of the SearchRequest query | ||
*/ | ||
@Override | ||
public String toString() { | ||
JSONObject jsonObject = | ||
new JSONObject() | ||
.put("facetName", this.facetName) | ||
.put("facetQuery", this.facetQuery) | ||
.put("q", this.q) | ||
.put( | ||
"matchingStrategy", | ||
this.matchingStrategy == null | ||
? null | ||
: this.matchingStrategy.toString()) | ||
.putOpt("attributesToSearchOn", this.attributesToSearchOn) | ||
.putOpt("filter", this.filter) | ||
.putOpt("filter", this.filterArray); | ||
|
||
return jsonObject.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/main/java/com/meilisearch/sdk/model/FacetSearchResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.meilisearch.sdk.model; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import lombok.Getter; | ||
import lombok.ToString; | ||
|
||
/** | ||
* Meilisearch facet search response data structure | ||
* | ||
* @see <a href="https://www.meilisearch.com/docs/reference/api/facet_search#response">API | ||
* specification</a> | ||
*/ | ||
@Getter | ||
@ToString | ||
public class FacetSearchResult implements FacetSearchable { | ||
ArrayList<HashMap<String, Object>> facetHits; | ||
int processingTimeMs; | ||
String facetQuery; | ||
|
||
public FacetSearchResult() {} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/meilisearch/sdk/model/FacetSearchable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.meilisearch.sdk.model; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
|
||
/** | ||
* Meilisearch facet search response data structure | ||
* | ||
* @see <a href="https://www.meilisearch.com/docs/reference/api/facet_search">API specification</a> | ||
*/ | ||
public interface FacetSearchable { | ||
ArrayList<HashMap<String, Object>> getFacetHits(); | ||
|
||
int getProcessingTimeMs(); | ||
|
||
String getFacetQuery(); | ||
} |