use of com.enonic.xp.highlight.HighlightedProperties in project xp by enonic.
the class FindContentByQueryCommand method execute.
FindContentByQueryResult execute() {
final NodeQuery nodeQuery = ContentQueryNodeQueryTranslator.translate(this.params.getContentQuery()).addQueryFilters(createFilters()).build();
final FindNodesByQueryResult result = nodeService.findByQuery(nodeQuery);
final NodeIds nodeIds = result.getNodeIds();
final Map<ContentId, HighlightedProperties> highlight = result.getNodeHits().stream().filter(nodeHit -> nodeHit.getHighlight() != null && nodeHit.getHighlight().size() > 0).collect(Collectors.toMap(hit -> ContentId.from(hit.getNodeId().toString()), NodeHit::getHighlight));
final Nodes foundNodes = this.nodeService.getByIds(nodeIds);
Contents contents = this.translator.fromNodes(foundNodes, true);
return FindContentByQueryResult.create().contents(contents).aggregations(result.getAggregations()).hits(result.getHits()).totalHits(result.getTotalHits()).highlight(highlight).build();
}
use of com.enonic.xp.highlight.HighlightedProperties in project xp by enonic.
the class FindContentIdsByQueryCommand method execute.
FindContentIdsByQueryResult execute() {
final NodeQuery nodeQuery = ContentQueryNodeQueryTranslator.translate(this.query).addQueryFilters(createFilters()).build();
final Map<ContentId, HighlightedProperties> highlight = new LinkedHashMap<>();
final Map<ContentId, SortValuesProperty> sortValues = new LinkedHashMap<>();
final Map<ContentId, Float> scoreValues = new LinkedHashMap<>();
final FindNodesByQueryResult result = nodeService.findByQuery(nodeQuery);
result.getNodeHits().forEach(nodeHit -> {
final ContentId contentId = ContentId.from(nodeHit.getNodeId().toString());
scoreValues.put(contentId, nodeHit.getScore());
if (nodeHit.getHighlight() != null && !nodeHit.getHighlight().isEmpty()) {
highlight.put(contentId, nodeHit.getHighlight());
}
if (nodeHit.getSort() != null && nodeHit.getSort().getValues() != null && !nodeHit.getSort().getValues().isEmpty()) {
sortValues.put(contentId, nodeHit.getSort());
}
});
return FindContentIdsByQueryResult.create().contents(ContentNodeHelper.toContentIds(result.getNodeIds())).aggregations(result.getAggregations()).highlight(highlight).sort(sortValues).hits(result.getHits()).score(scoreValues).totalHits(result.getTotalHits()).build();
}
use of com.enonic.xp.highlight.HighlightedProperties in project xp by enonic.
the class HighlightedPropertiesFactory method create.
public static HighlightedProperties create(final Map<String, HighlightField> highlightPropertyMap) {
if (highlightPropertyMap == null) {
return null;
}
final HighlightedProperties.Builder result = HighlightedProperties.create();
for (final Map.Entry<String, HighlightField> propertyEntry : highlightPropertyMap.entrySet()) {
final String propertyName = removePostfix(propertyEntry.getKey());
final HighlightedProperty.Builder builder = HighlightedProperty.create().name(propertyName);
for (Text fragment : propertyEntry.getValue().fragments()) {
builder.addFragment(fragment.string());
}
result.add(builder.build());
}
return result.build();
}
use of com.enonic.xp.highlight.HighlightedProperties in project xp by enonic.
the class HighlightedPropertiesFactoryTest method create_null.
@Test
public void create_null() {
final HighlightedProperties highlightedProperties = HighlightedPropertiesFactory.create(null);
assertNull(highlightedProperties);
}
use of com.enonic.xp.highlight.HighlightedProperties in project xp by enonic.
the class HighlightedPropertiesFactoryTest method create.
@Test
public void create() {
final HighlightField highlightField1 = new HighlightField("name1", new Text[] { new Text("fragment1_1"), new Text("fragment1_2") });
final HighlightField highlightField2 = new HighlightField("name2", new Text[] { new Text("fragment2_1"), new Text("fragment2_2") });
final Map<String, HighlightField> paramsMap = Map.of("name1", highlightField1, "name2", highlightField2);
final HighlightedProperties highlightedProperties = HighlightedPropertiesFactory.create(paramsMap);
assertNotNull(highlightedProperties);
assertEquals(2, highlightedProperties.size());
assertEquals(2, highlightedProperties.get("name1").getFragments().size());
assertTrue(highlightedProperties.get("name1").getFragments().contains("fragment1_1"));
assertTrue(highlightedProperties.get("name1").getFragments().contains("fragment1_2"));
assertEquals(2, highlightedProperties.get("name2").getFragments().size());
assertTrue(highlightedProperties.get("name2").getFragments().contains("fragment2_2"));
assertTrue(highlightedProperties.get("name2").getFragments().contains("fragment2_1"));
}
Aggregations