Search in sources :

Example 1 with HighlightedProperties

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();
}
Also used : NodeHit(com.enonic.xp.node.NodeHit) Nodes(com.enonic.xp.node.Nodes) FindContentByQueryParams(com.enonic.xp.content.FindContentByQueryParams) Collectors(java.util.stream.Collectors) HighlightedProperties(com.enonic.xp.highlight.HighlightedProperties) ContentId(com.enonic.xp.content.ContentId) Contents(com.enonic.xp.content.Contents) Map(java.util.Map) FindContentByQueryResult(com.enonic.xp.content.FindContentByQueryResult) Preconditions(com.google.common.base.Preconditions) NodeQuery(com.enonic.xp.node.NodeQuery) NodeIds(com.enonic.xp.node.NodeIds) FindNodesByQueryResult(com.enonic.xp.node.FindNodesByQueryResult) Contents(com.enonic.xp.content.Contents) FindNodesByQueryResult(com.enonic.xp.node.FindNodesByQueryResult) NodeIds(com.enonic.xp.node.NodeIds) HighlightedProperties(com.enonic.xp.highlight.HighlightedProperties) NodeQuery(com.enonic.xp.node.NodeQuery) ContentId(com.enonic.xp.content.ContentId) Nodes(com.enonic.xp.node.Nodes)

Example 2 with HighlightedProperties

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();
}
Also used : FindNodesByQueryResult(com.enonic.xp.node.FindNodesByQueryResult) HighlightedProperties(com.enonic.xp.highlight.HighlightedProperties) NodeQuery(com.enonic.xp.node.NodeQuery) SortValuesProperty(com.enonic.xp.sortvalues.SortValuesProperty) ContentId(com.enonic.xp.content.ContentId) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with HighlightedProperties

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();
}
Also used : HighlightedProperties(com.enonic.xp.highlight.HighlightedProperties) HighlightField(org.elasticsearch.search.highlight.HighlightField) Text(org.elasticsearch.common.text.Text) Map(java.util.Map) HighlightedProperty(com.enonic.xp.highlight.HighlightedProperty)

Example 4 with HighlightedProperties

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);
}
Also used : HighlightedProperties(com.enonic.xp.highlight.HighlightedProperties) Test(org.junit.jupiter.api.Test)

Example 5 with 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"));
}
Also used : HighlightedProperties(com.enonic.xp.highlight.HighlightedProperties) HighlightField(org.elasticsearch.search.highlight.HighlightField) Text(org.elasticsearch.common.text.Text) Test(org.junit.jupiter.api.Test)

Aggregations

HighlightedProperties (com.enonic.xp.highlight.HighlightedProperties)6 ContentId (com.enonic.xp.content.ContentId)3 Contents (com.enonic.xp.content.Contents)2 FindNodesByQueryResult (com.enonic.xp.node.FindNodesByQueryResult)2 NodeQuery (com.enonic.xp.node.NodeQuery)2 Map (java.util.Map)2 Text (org.elasticsearch.common.text.Text)2 HighlightField (org.elasticsearch.search.highlight.HighlightField)2 Test (org.junit.jupiter.api.Test)2 Aggregations (com.enonic.xp.aggregation.Aggregations)1 BucketAggregation (com.enonic.xp.aggregation.BucketAggregation)1 Buckets (com.enonic.xp.aggregation.Buckets)1 StatsAggregation (com.enonic.xp.aggregation.StatsAggregation)1 ContentQuery (com.enonic.xp.content.ContentQuery)1 FindContentByQueryParams (com.enonic.xp.content.FindContentByQueryParams)1 FindContentByQueryResult (com.enonic.xp.content.FindContentByQueryResult)1 FindContentIdsByQueryResult (com.enonic.xp.content.FindContentIdsByQueryResult)1 GetContentByIdsParams (com.enonic.xp.content.GetContentByIdsParams)1 HighlightedProperty (com.enonic.xp.highlight.HighlightedProperty)1 NodeHit (com.enonic.xp.node.NodeHit)1