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

Add highlights to search query #2761

Merged
merged 4 commits into from
Jan 18, 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
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.tinkerpop.gremlin.process.traversal.Order;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

Expand Down Expand Up @@ -101,6 +102,8 @@ interface Result<V, E> {

DirectIndexQueryResult<V, E> getCollapseVertices(String key);

Map<String, List<String>> getHighLights();

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ public Integer getApproximateCount() {
public void setApproximateCount(Integer approximateCount) {
this.approximateCount = approximateCount;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,11 @@ public Set<String> getCollapseKeys() {
public DirectIndexQueryResult<AtlasJanusVertex, AtlasJanusEdge> getCollapseVertices(String key) {
return null;
}

@Override
public Map<String, List<String>> getHighLights() {
return new HashMap<>();
}
}


Expand Down Expand Up @@ -336,5 +341,14 @@ public double getScore() {
}
return Double.parseDouble(String.valueOf(hit.get("_score")));
}

@Override
public Map<String, List<String>> getHighLights() {
Object highlight = this.hit.get("highlight");
if(Objects.nonNull(highlight)) {
return (Map<String, List<String>>) highlight;
}
return new HashMap<>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
*/
package org.apache.atlas.repository.graphdb.janus;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.*;

import com.google.common.base.Preconditions;
import org.apache.atlas.exception.AtlasBaseException;
Expand Down Expand Up @@ -150,5 +148,10 @@ public Set<String> getCollapseKeys() {
public DirectIndexQueryResult<AtlasJanusVertex, AtlasJanusEdge> getCollapseVertices(String key) {
return null;
}

@Override
public Map<String, List<String>> getHighLights() {
return new HashMap<>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import org.apache.atlas.model.instance.AtlasEntityHeader;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;

import javax.xml.bind.annotation.XmlAccessType;
Expand Down Expand Up @@ -58,6 +59,10 @@ public class AtlasSearchResult implements Serializable {
private Map<String, Object> aggregations;
private Map<String,Double> searchScore;

private Map<String, ElasticsearchMetadata> searchMetadata;



public AtlasSearchResult() {}

public AtlasSearchResult(AtlasQueryType queryType) {
Expand Down Expand Up @@ -151,6 +156,19 @@ public void setSearchScore(HashMap<String, Double> searchScore) {

public void setNextMarker(String nextMarker) { this.nextMarker = nextMarker; }

public Map<String, ElasticsearchMetadata> getSearchMetadata() {
return searchMetadata;
}

public void addHighlights(String guid, Map<String, List<String>> highlights) {
if(MapUtils.isEmpty(this.searchMetadata)) {
this.searchMetadata = new HashMap<>();
}
ElasticsearchMetadata v = this.searchMetadata.getOrDefault(guid, new ElasticsearchMetadata());
v.addHighlights(highlights);
this.searchMetadata.put(guid, v);
}

@Override
public int hashCode() { return Objects.hash(queryType, searchParameters, queryText, type, classification, entities, attributes, fullTextResult, referredEntities, nextMarker); }

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.apache.atlas.model.discovery;

import org.apache.commons.collections.MapUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ElasticsearchMetadata {

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

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

public void addHighlights(Map<String, List<String>> highlights) {
if(MapUtils.isNotEmpty(highlights)) {
if (MapUtils.isEmpty(this.highlights)) {
this.highlights = new HashMap<>();
}
this.highlights.putAll(highlights);
}
}


@Override
public String toString() {
return "SearchMetadata{" +
"highlights=" + highlights +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class SearchParams {
boolean excludeClassifications;

RequestMetadata requestMetadata = new RequestMetadata();
boolean showHighlights;

public String getQuery() {
return getQuery();
Expand Down Expand Up @@ -117,6 +118,10 @@ public String getSearchInput() {
return this.requestMetadata.getSearchInput();
}

public boolean isShowHighlights() {
return showHighlights;
}

static class RequestMetadata {
private String searchInput;
private Set<String> utmTags;
Expand Down Expand Up @@ -146,4 +151,6 @@ public void setSaveSearchLog(boolean saveSearchLog) {
this.saveSearchLog = saveSearchLog;
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -1093,6 +1093,10 @@ private void prepareSearchResult(AtlasSearchResult ret, DirectIndexQueryResult i
}
}

if (searchParams.isShowHighlights()) {
ret.addHighlights(header.getGuid(), result.getHighLights());
}

ret.addEntity(header);
}
} catch (Exception e) {
Expand Down
Loading