diff --git a/src/main/java/com/strandls/esmodule/ApiConstants.java b/src/main/java/com/strandls/esmodule/ApiConstants.java index 706113a..874a5f5 100644 --- a/src/main/java/com/strandls/esmodule/ApiConstants.java +++ b/src/main/java/com/strandls/esmodule/ApiConstants.java @@ -40,6 +40,7 @@ private ApiConstants() { public static final String GETTOPUSERS = "/leaderboard"; public static final String GETUSERSCORE = "/userscore"; + public static final String USERIBP = "/userIbp"; public static final String REINDEX = "/reindex"; public static final String FILTERAUTOCOMPLETE = "/filterautocomplete"; public static final String LIST = "/list"; diff --git a/src/main/java/com/strandls/esmodule/controllers/ESController.java b/src/main/java/com/strandls/esmodule/controllers/ESController.java index 0963728..820d3a0 100644 --- a/src/main/java/com/strandls/esmodule/controllers/ESController.java +++ b/src/main/java/com/strandls/esmodule/controllers/ESController.java @@ -733,6 +733,19 @@ public Response getListPageFilterValue(@PathParam("index") String index, @PathPa return Response.status(Status.OK).entity(results).build(); } + @GET + @Path(ApiConstants.USERIBP + ApiConstants.AUTOCOMPLETE + "/{index}/{type}") + @Consumes(MediaType.TEXT_PLAIN) + @Produces(MediaType.APPLICATION_JSON) + @ApiOperation(value = "Auto complete username", notes = "Returns List of userIbp", response = MapResponse.class) + @ApiResponses(value = { @ApiResponse(code = 500, message = "ERROR", response = String.class) }) + public Response autocompleteUserIBP(@PathParam("index") String index, @PathParam("type") String type, + @QueryParam("userGroupId") String userGroupId, @QueryParam("name") String name) + throws IOException { + MapResponse results = elasticSearchService.autocompleteUserIBP(index, type, userGroupId, name); + return Response.status(Status.OK).entity(results).build(); + } + @GET @Path(ApiConstants.GETUSERSCORE) @Consumes(MediaType.TEXT_PLAIN) diff --git a/src/main/java/com/strandls/esmodule/services/ElasticSearchService.java b/src/main/java/com/strandls/esmodule/services/ElasticSearchService.java index 73aa73d..a8bf97e 100644 --- a/src/main/java/com/strandls/esmodule/services/ElasticSearchService.java +++ b/src/main/java/com/strandls/esmodule/services/ElasticSearchService.java @@ -218,7 +218,7 @@ MapDocument termsAggregation(String index, String type, String field, String sub * @return {@link AggregationResponse} */ AggregationResponse aggregation(String index, String type, MapSearchQuery serachQuery, String geoAggregationField, - String filter,String geoShapeFilterField) throws IOException; + String filter, String geoShapeFilterField) throws IOException; /** * @@ -227,7 +227,8 @@ AggregationResponse aggregation(String index, String type, MapSearchQuery serach * @param speciesName the name of SpeciesName * @return {@link ObservationInfo} */ - ObservationInfo getObservationRightPan(String index, String type, String id, Boolean isMaxVotedRecoId) throws IOException; + ObservationInfo getObservationRightPan(String index, String type, String id, Boolean isMaxVotedRecoId) + throws IOException; List observationNearBy(String index, String type, Double lat, Double lon) throws IOException; @@ -307,10 +308,11 @@ GeoHashAggregationData getNewGeoAggregation(String index, String type, MapSearch public List getSpeciesCoordinates(String index, String type, String speciesId); - - public String fetchIndex(); public AuthorUploadedObservationInfo getUserData(String index, String type, Long userId, Integer size, Long sGroup, Boolean hasMedia); + + public MapResponse autocompleteUserIBP(String index, String type, String userGroupId, String name) + throws IOException; } diff --git a/src/main/java/com/strandls/esmodule/services/impl/ElasticSearchServiceImpl.java b/src/main/java/com/strandls/esmodule/services/impl/ElasticSearchServiceImpl.java index 9ca59c9..4f45357 100644 --- a/src/main/java/com/strandls/esmodule/services/impl/ElasticSearchServiceImpl.java +++ b/src/main/java/com/strandls/esmodule/services/impl/ElasticSearchServiceImpl.java @@ -16,6 +16,7 @@ import javax.inject.Inject; +import org.apache.lucene.search.join.ScoreMode; import org.elasticsearch.action.bulk.BulkItemResponse; import org.elasticsearch.action.bulk.BulkRequest; import org.elasticsearch.action.bulk.BulkResponse; @@ -50,6 +51,7 @@ import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.RangeQueryBuilder; +import org.elasticsearch.index.query.TermQueryBuilder; import org.elasticsearch.script.Script; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.aggregations.Aggregation; @@ -140,6 +142,8 @@ public class ElasticSearchServiceImpl extends ElasticSearchQueryUtil implements private static final Integer TOTAL_USER_UPPER_BOUND = 20000; + private static final String USERGROUP = "userGroup"; + /* * (non-Javadoc) * @@ -857,7 +861,7 @@ private AggregationResponse groupAggregation(String index, AggregationBuilder ag for (Terms.Bucket entry : termResp.getBuckets()) { groupMonth.put(entry.getKey(), entry.getDocCount()); } - }else { + } else { Terms frommonth = response.getAggregations().get(filter); for (Terms.Bucket entry : frommonth.getBuckets()) { @@ -1194,6 +1198,38 @@ public List getListPageFilterValue(String index, String type, String fil return results; } + @Override + public MapResponse autocompleteUserIBP(String index, String type, String userGroupId, String name) + throws IOException { + + SearchRequest searchRequest = new SearchRequest(index); + SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); + searchSourceBuilder.size(10); + BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); + + // Add must_not nested query if userGroupId is provided + if (userGroupId != null && !userGroupId.isEmpty()) { + boolQueryBuilder.mustNot(QueryBuilders.nestedQuery(USERGROUP, + new TermQueryBuilder("userGroup.usergroupids", userGroupId), ScoreMode.None)); + } + boolQueryBuilder.must(QueryBuilders.matchPhrasePrefixQuery("user.name", name)); + + searchSourceBuilder.query(boolQueryBuilder); + searchRequest.source(searchSourceBuilder); + + SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); + List result = new ArrayList<>(); + + for (SearchHit hit : searchResponse.getHits().getHits()) { + result.add(new MapDocument(hit.getSourceAsString())); + } + + long totalHits = searchResponse.getHits().getTotalHits().value; + + return new MapResponse(result, totalHits, null); + + } + private String cleanAutoCompleteResponse(String[] resRegex, String text) { String resp = text; for (String filterString : resRegex) { @@ -1472,7 +1508,7 @@ public FilterPanelData getListPanel(String index, String type) { AggregationBuilder speciesGroupAggregation = AggregationBuilders.terms("speciesGroup") .field("sgroup_filter.keyword").size(100).order(BucketOrder.key(true)); - AggregationBuilder userGroupAgregation = AggregationBuilders.terms("userGroup") + AggregationBuilder userGroupAgregation = AggregationBuilders.terms(USERGROUP) .field("user_group_observations.ug_filter.keyword").size(100).order(BucketOrder.count(false)); AggregationBuilder stateAggregation = AggregationBuilders.terms("state") .field("location_information.state.keyword").size(100).order(BucketOrder.key(true)); @@ -1497,7 +1533,7 @@ public FilterPanelData getListPanel(String index, String type) { filterPanel.setSpeciesGroup(getAggregationSpeciesGroup(response.getAggregations().get("speciesGroup"))); filterPanel.setStates(getAggregationList(response.getAggregations().get("state"))); - filterPanel.setUserGroup(getAggregationUserGroup(response.getAggregations().get("userGroup"))); + filterPanel.setUserGroup(getAggregationUserGroup(response.getAggregations().get(USERGROUP))); filterPanel.setTraits(getTraits(response.getAggregations().get("trait"))); filterPanel.setCustomFields(getCustomFields(response.getAggregations().get("customField"))); return filterPanel;