Skip to content

Highlighting Support #93

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ private String nextHit() {
final ElasticHit hit = searchHitIterator.next();
final SimpleFeatureType type = getFeatureType();
final Map<String, Object> source = hit.getSource();
final Map<String, List<String>> highlights = hit.getHighlight();

final Float score;
final Float relativeScore;
Expand All @@ -126,6 +127,12 @@ private String nextHit() {
final String sourceName = (String) descriptor.getUserData().get(FULL_NAME);

List<Object> values = hit.field(sourceName);

if (values == null && highlights != null && highlights.containsKey(sourceName)) {
// read field from source
values = Collections.unmodifiableList(highlights.get(sourceName));
}

if (values == null && source != null) {
// read field from source
values = parserUtil.readField(source, sourceName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@


import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
Expand Down Expand Up @@ -217,6 +218,18 @@ private ElasticRequest prepareSearchRequest(Query query, boolean scroll) throws
searchRequest.setSize(0);
}

if (filterToElastic.gethighlights() != null) {
searchRequest.setHighlights(filterToElastic.gethighlights());

List<String> source = searchRequest.getSourceIncludes();
for (String key : filterToElastic.gethighlights().keySet()) {
if (source.contains(key))
{
source.remove(key);
}
}
}

return searchRequest;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class ElasticHit {
@JsonProperty("fields")
private Map<String,List<Object>> fields;

private Map<String,List<String>> highlight;

public String getIndex() {
return index;
}
Expand Down Expand Up @@ -60,4 +62,11 @@ public List<Object> field(String name) {
return this.fields != null ? this.fields.get(name) : null;
}

public Map<String, List<String>> getHighlight() {
return highlight;
}

public void setHighlight(Map<String, List<String>> highlight) {
this.highlight = highlight;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class ElasticRequest {

private Map<String,Map<String,Map<String,Object>>> aggregations;

private Map<String, Object> highlights;

private Integer size;

private Integer from;
Expand Down Expand Up @@ -49,6 +51,14 @@ public void setAggregations(Map<String,Map<String,Map<String,Object>>> aggregati
this.aggregations = aggregations;
}

public Map<String, Object> getHighlights() {
return highlights;
}

public void setHighlights(Map<String, Object> highlights) {
this.highlights = highlights;
}

public Integer getSize() {
return size;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ class FilterToElastic implements FilterVisitor, ExpressionVisitor {
Map<String,Map<String,Map<String,Object>>> aggregations;

private final FilterToElasticHelper helper;
private Map<String, Object> highlights;

private String key;

Expand Down Expand Up @@ -1232,6 +1233,20 @@ void addViewParams(Query query) {
throw new FilterToElasticException("Unable to parse aggregation", e);
}
}
} else if (entry.getKey().equalsIgnoreCase("highlight")) {
final ObjectMapper mapper = new ObjectMapper();
final TypeReference<Map<String, Object>> type;
type = new TypeReference<Map<String, Object>>() {};
final String value = entry.getValue();
try {
this.highlights = mapper.readValue(value, type);
} catch (Exception e) {
try {
this.highlights = mapper.readValue(ElasticParserUtil.urlDecode(value), type);
} catch (Exception e2) {
throw new FilterToElasticException("Unable to parse aggregation", e);
}
}
}
}
}
Expand Down Expand Up @@ -1318,4 +1333,7 @@ public Map<String,Map<String,Map<String,Object>>> getAggregations() {
return aggregations;
}

public Map<String, Object> gethighlights() {
return highlights;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ public ElasticResponse search(String searchIndices, String type, ElasticRequest
requestBody.put("aggregations", request.getAggregations());
}

if (request.getHighlights() != null) {
requestBody.put("highlight", request.getHighlights());
}

return parseResponse(performRequest("POST", pathBuilder.toString(), requestBody));
}

Expand Down