Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Es autocomplete #95

Merged
merged 6 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/java/com/strandls/esmodule/ApiConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/strandls/esmodule/controllers/ESController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
*
Expand All @@ -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> observationNearBy(String index, String type, Double lat, Double lon) throws IOException;

Expand Down Expand Up @@ -307,10 +308,11 @@ GeoHashAggregationData getNewGeoAggregation(String index, String type, MapSearch

public List<ObservationLatLon> 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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
*
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -1194,6 +1198,38 @@ public List<String> 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<MapDocument> 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) {
Expand Down Expand Up @@ -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));
Expand All @@ -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;
Expand Down
Loading