use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder in project elasticsearch by elastic.
the class ChildQuerySearchIT method testHasChildInnerHitsHighlighting.
public void testHasChildInnerHitsHighlighting() throws Exception {
assertAcked(prepareCreate("test").addMapping("parent").addMapping("child", "_parent", "type=parent"));
ensureGreen();
client().prepareIndex("test", "parent", "1").setSource("p_field", 1).get();
client().prepareIndex("test", "child", "2").setParent("1").setSource("c_field", "foo bar").get();
client().admin().indices().prepareFlush("test").get();
SearchResponse searchResponse = client().prepareSearch("test").setQuery(hasChildQuery("child", matchQuery("c_field", "foo"), ScoreMode.None).innerHit(new InnerHitBuilder().setHighlightBuilder(new HighlightBuilder().field(new Field("c_field").highlightQuery(QueryBuilders.matchQuery("c_field", "bar")))), false)).get();
assertNoFailures(searchResponse);
assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L));
assertThat(searchResponse.getHits().getHits()[0].getId(), equalTo("1"));
SearchHit[] searchHits = searchResponse.getHits().getHits()[0].getInnerHits().get("child").getHits();
assertThat(searchHits.length, equalTo(1));
assertThat(searchHits[0].getHighlightFields().get("c_field").getFragments().length, equalTo(1));
assertThat(searchHits[0].getHighlightFields().get("c_field").getFragments()[0].string(), equalTo("foo <em>bar</em>"));
}
use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder in project elasticsearch by elastic.
the class ChildQuerySearchIT method testHighlightersIgnoreParentChild.
public void testHighlightersIgnoreParentChild() {
assertAcked(prepareCreate("test").addMapping("parent-type", "searchText", "type=text,term_vector=with_positions_offsets,index_options=offsets").addMapping("child-type", "_parent", "type=parent-type", "searchText", "type=text,term_vector=with_positions_offsets,index_options=offsets"));
client().prepareIndex("test", "parent-type", "parent-id").setSource("searchText", "quick brown fox").get();
client().prepareIndex("test", "child-type", "child-id").setParent("parent-id").setSource("searchText", "quick brown fox").get();
refresh();
String[] highlightTypes = new String[] { "plain", "fvh", "postings" };
for (String highlightType : highlightTypes) {
logger.info("Testing with highlight type [{}]", highlightType);
SearchResponse searchResponse = client().prepareSearch("test").setQuery(new BoolQueryBuilder().must(new MatchQueryBuilder("searchText", "fox")).must(new HasChildQueryBuilder("child-type", new MatchAllQueryBuilder(), ScoreMode.None))).highlighter(new HighlightBuilder().field(new HighlightBuilder.Field("searchText").highlighterType(highlightType))).get();
assertHitCount(searchResponse, 1);
assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("parent-id"));
HighlightField highlightField = searchResponse.getHits().getAt(0).getHighlightFields().get("searchText");
assertThat(highlightField.getFragments()[0].string(), equalTo("quick brown <em>fox</em>"));
searchResponse = client().prepareSearch("test").setQuery(new BoolQueryBuilder().must(new MatchQueryBuilder("searchText", "fox")).must(new HasParentQueryBuilder("parent-type", new MatchAllQueryBuilder(), false))).highlighter(new HighlightBuilder().field(new HighlightBuilder.Field("searchText").highlighterType(highlightType))).get();
assertHitCount(searchResponse, 1);
assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("child-id"));
highlightField = searchResponse.getHits().getAt(0).getHighlightFields().get("searchText");
assertThat(highlightField.getFragments()[0].string(), equalTo("quick brown <em>fox</em>"));
}
}
use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder in project herd by FINRAOS.
the class IndexSearchDaoImpl method buildHighlightQuery.
/**
* Builds a {@link HighlightBuilder} based on (pre/post)tags and fields fetched from the DB config which is added to the main {@link SearchRequestBuilder}
*
* @param preTag The specified pre-tag to be used for highlighting
* @param postTag The specified post-tag to be used for highlighting
* @param match the set of match fields that are to be searched upon in the index search
*
* @return A configured {@link HighlightBuilder} object
*/
private HighlightBuilder buildHighlightQuery(String preTag, String postTag, Set<String> match) {
HighlightBuilder highlightBuilder = new HighlightBuilder();
// Field matching is not needed since we are matching on multiple 'type' fields like stemmed and ngrams and enabling highlighting on all those fields
// will yield duplicates
highlightBuilder.requireFieldMatch(false);
// Set the configured value for pre-tags for highlighting
highlightBuilder.preTags(preTag);
// Set the configured value for post-tags for highlighting
highlightBuilder.postTags(postTag);
// Get highlight fields value from configuration
String highlightFieldsValue;
// If the match column is included
if (match != null && match.contains(MATCH_COLUMN)) {
highlightFieldsValue = configurationHelper.getProperty(ConfigurationValue.ELASTICSEARCH_COLUMN_MATCH_HIGHLIGHT_FIELDS);
} else {
highlightFieldsValue = configurationHelper.getProperty(ConfigurationValue.ELASTICSEARCH_HIGHLIGHT_FIELDS);
}
try {
@SuppressWarnings("unchecked") IndexSearchHighlightFields highlightFieldsConfig = jsonHelper.unmarshallJsonToObject(IndexSearchHighlightFields.class, highlightFieldsValue);
highlightFieldsConfig.getHighlightFields().forEach(highlightFieldConfig -> {
// set the field name to the configured value
HighlightBuilder.Field highlightField = new HighlightBuilder.Field(highlightFieldConfig.getFieldName());
// set matched_fields to the configured list of fields, this accounts for 'multifields' that analyze the same string in different ways
if (CollectionUtils.isNotEmpty(highlightFieldConfig.getMatchedFields())) {
highlightField.matchedFields(highlightFieldConfig.getMatchedFields().toArray(new String[0]));
}
// set fragment size to the configured value
if (highlightFieldConfig.getFragmentSize() != null) {
highlightField.fragmentSize(highlightFieldConfig.getFragmentSize());
}
// set the number of desired fragments to the configured value
if (highlightFieldConfig.getNumOfFragments() != null) {
highlightField.numOfFragments(highlightFieldConfig.getNumOfFragments());
}
highlightBuilder.field(highlightField);
});
} catch (IOException e) {
LOGGER.warn("Could not parse the configured value for highlight fields: {}", highlightFieldsValue, e);
}
return highlightBuilder;
}
use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder in project vertigo by KleeGroup.
the class ESSearchRequestBuilder method appendFacetDefinition.
private static void appendFacetDefinition(final SearchQuery searchQuery, final SearchRequestBuilder searchRequestBuilder, final SearchIndexDefinition myIndexDefinition, final DtListState myListState, final boolean useHighlight) {
Assertion.checkNotNull(searchRequestBuilder);
// On ajoute le cluster, si présent
if (searchQuery.isClusteringFacet()) {
// si il y a un cluster on le place en premier
final FacetDefinition clusteringFacetDefinition = searchQuery.getClusteringFacetDefinition();
final AggregationBuilder aggregationBuilder = facetToAggregationBuilder(clusteringFacetDefinition);
final TopHitsAggregationBuilder topHitsBuilder = AggregationBuilders.topHits(TOPHITS_SUBAGGREGATION_NAME).size(myListState.getMaxRows().orElse(TOPHITS_SUBAGGREGATION_SIZE)).from(myListState.getSkipRows());
if (useHighlight) {
// .addHighlightedField("*"); HOW TO ?
topHitsBuilder.highlighter(new HighlightBuilder().numOfFragments(3));
}
if (myListState.getSortFieldName().isPresent()) {
topHitsBuilder.sort(getFieldSortBuilder(myIndexDefinition, myListState));
}
aggregationBuilder.subAggregation(topHitsBuilder);
// We fetch source, because it's our only source to create result list
searchRequestBuilder.addAggregation(aggregationBuilder);
}
// Puis les facettes liées à la query, si présent
if (searchQuery.getFacetedQuery().isPresent()) {
final FacetedQueryDefinition facetedQueryDefinition = searchQuery.getFacetedQuery().get().getDefinition();
final Collection<FacetDefinition> facetDefinitions = new ArrayList<>(facetedQueryDefinition.getFacetDefinitions());
if (searchQuery.isClusteringFacet() && facetDefinitions.contains(searchQuery.getClusteringFacetDefinition())) {
facetDefinitions.remove(searchQuery.getClusteringFacetDefinition());
}
for (final FacetDefinition facetDefinition : facetDefinitions) {
final AggregationBuilder aggregationBuilder = facetToAggregationBuilder(facetDefinition);
final BoolQueryBuilder aggsFilterBoolQueryBuilder = QueryBuilders.boolQuery();
for (final FacetDefinition filterFacetDefinition : searchQuery.getFacetedQuery().get().getDefinition().getFacetDefinitions()) {
if (filterFacetDefinition.isMultiSelectable() && !facetDefinition.equals(filterFacetDefinition)) {
// on ne doit refiltrer que les multiSelectable (les autres sont dans le filter de la request), sauf la facet qu'on est entrain de traiter
appendSelectedFacetValuesFilter(aggsFilterBoolQueryBuilder, searchQuery.getFacetedQuery().get().getSelectedFacetValues().getFacetValues(filterFacetDefinition));
}
}
if (aggsFilterBoolQueryBuilder.hasClauses()) {
final AggregationBuilder filterAggregationBuilder = AggregationBuilders.filter(facetDefinition.getName() + "_FILTER", aggsFilterBoolQueryBuilder);
filterAggregationBuilder.subAggregation(aggregationBuilder);
searchRequestBuilder.addAggregation(filterAggregationBuilder);
} else {
searchRequestBuilder.addAggregation(aggregationBuilder);
}
}
}
}
use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder in project yacy_grid_mcp by yacy.
the class YaCySearchService method serviceImpl.
@Override
public ServiceResponse serviceImpl(Query call, HttpServletResponse response) {
String callback = call.get("callback", "");
boolean jsonp = callback != null && callback.length() > 0;
boolean minified = call.get("minified", false);
boolean explain = call.get("explain", false);
String q = call.get("query", "");
Classification.ContentDomain contentdom = Classification.ContentDomain.contentdomParser(call.get("contentdom", "all"));
// important: call arguments may overrule parsed collection values if not empty. This can be used for authentified indexes!
String collection = call.get("collection", "");
// to be compatible with the site-operator of GSA, we use a vertical pipe symbol here to divide collections.
collection = collection.replace(',', '|');
String[] collections = collection.length() == 0 ? new String[0] : collection.split("\\|");
int maximumRecords = call.get("maximumRecords", call.get("rows", call.get("num", 10)));
int startRecord = call.get("startRecord", call.get("start", 0));
// int meanCount = call.get("meanCount", 5);
int timezoneOffset = call.get("timezoneOffset", -1);
// String nav = call.get("nav", "");
// String prefermaskfilter = call.get("prefermaskfilter", "");
// String constraint = call.get("constraint", "");
int facetLimit = call.get("facetLimit", 10);
String facetFields = call.get("facetFields", YaCyQuery.FACET_DEFAULT_PARAMETER);
List<WebMapping> facetFieldMapping = new ArrayList<>();
for (String s : facetFields.split(",")) facetFieldMapping.add(WebMapping.valueOf(s));
Sort sort = new Sort(call.get("sort", ""));
YaCyQuery yq = new YaCyQuery(q, collections, contentdom, timezoneOffset);
ElasticsearchClient ec = Data.gridIndex.getElasticClient();
HighlightBuilder hb = new HighlightBuilder().field(WebMapping.text_t.getMapping().name()).preTags("").postTags("").fragmentSize(140);
ElasticsearchClient.Query query = ec.query("web", null, yq.queryBuilder, null, sort, hb, timezoneOffset, startRecord, maximumRecords, facetLimit, explain, facetFieldMapping.toArray(new WebMapping[facetFieldMapping.size()]));
JSONObject json = new JSONObject(true);
JSONArray channels = new JSONArray();
json.put("channels", channels);
JSONObject channel = new JSONObject(true);
channels.put(channel);
JSONArray items = new JSONArray();
channel.put("title", "Search for " + q);
channel.put("description", "Search for " + q);
channel.put("startIndex", "" + startRecord);
channel.put("itemsPerPage", "" + items.length());
channel.put("searchTerms", q);
channel.put("totalResults", Integer.toString(query.hitCount));
channel.put("items", items);
List<Map<String, Object>> result = query.results;
List<String> explanations = query.explanations;
for (int hitc = 0; hitc < result.size(); hitc++) {
WebDocument doc = new WebDocument(result.get(hitc));
JSONObject hit = new JSONObject(true);
String titleString = doc.getTitle();
String link = doc.getLink();
if (Classification.ContentDomain.IMAGE == contentdom) {
// the url before we extract the link
hit.put("url", link);
link = doc.pickImage((String) link);
hit.put("icon", link);
hit.put("image", link);
}
String snippet = doc.getSnippet(query.highlights.get(hitc), yq);
Date last_modified_date = doc.getDate();
int size = doc.getSize();
int sizekb = size / 1024;
int sizemb = sizekb / 1024;
String size_string = sizemb > 0 ? (Integer.toString(sizemb) + " mbyte") : sizekb > 0 ? (Integer.toString(sizekb) + " kbyte") : (Integer.toString(size) + " byte");
String host = doc.getHost();
hit.put("title", titleString);
hit.put("link", link.toString());
hit.put("description", snippet);
hit.put("pubDate", DateParser.formatRFC1123(last_modified_date));
hit.put("size", Integer.toString(size));
hit.put("sizename", size_string);
hit.put("host", host);
if (explain) {
hit.put("explanation", explanations.get(hitc));
}
items.put(hit);
}
;
JSONArray navigation = new JSONArray();
channel.put("navigation", navigation);
Map<String, List<Map.Entry<String, Long>>> aggregations = query.aggregations;
for (Map.Entry<String, List<Map.Entry<String, Long>>> fe : aggregations.entrySet()) {
String facetname = fe.getKey();
WebMapping mapping = WebMapping.valueOf(facetname);
JSONObject facetobject = new JSONObject(true);
facetobject.put("facetname", mapping.getMapping().getFacetname());
facetobject.put("displayname", mapping.getMapping().getDisplayname());
facetobject.put("type", mapping.getMapping().getFacettype());
facetobject.put("min", "0");
facetobject.put("max", "0");
facetobject.put("mean", "0");
facetobject.put("count", fe.getValue().size());
JSONArray elements = new JSONArray();
facetobject.put("elements", elements);
for (Map.Entry<String, Long> element : fe.getValue()) {
JSONObject elementEntry = new JSONObject(true);
elementEntry.put("name", element.getKey());
elementEntry.put("count", element.getValue().toString());
elementEntry.put("modifier", mapping.getMapping().getFacetmodifier() + ":" + element.getKey());
elements.put(elementEntry);
}
navigation.put(facetobject);
}
if (jsonp) {
StringBuilder sb = new StringBuilder(1024);
sb.append(callback).append("([").append(json.toString(minified ? 0 : 2)).append("]);");
return new ServiceResponse(sb.toString());
} else {
return new ServiceResponse(json);
}
}
Aggregations