Search in sources :

Example 1 with ElasticHighlightQuery

use of com.enonic.xp.repo.impl.elasticsearch.query.ElasticHighlightQuery in project xp by enonic.

the class ElasticHighlightQueryBuilderFactory method create.

public ElasticHighlightQuery create(final HighlightQuery highlightQuery) {
    if (highlightQuery == null) {
        return ElasticHighlightQuery.empty();
    }
    ElasticHighlightQuery.Builder result = ElasticHighlightQuery.create().settings(highlightQuery.getSettings());
    for (HighlightQueryProperty highlightQueryProperty : highlightQuery.getProperties()) {
        final String normalizedFieldName = IndexFieldNameNormalizer.normalize(highlightQueryProperty.getName());
        final String normalizedFieldNameWithPostFix = normalizedFieldName + IndexValueType.INDEX_VALUE_TYPE_SEPARATOR + "_*";
        final HighlightBuilder.Field rawHighlightField = createField(normalizedFieldName, highlightQueryProperty);
        final HighlightBuilder.Field analyzedHighlightField = createField(normalizedFieldNameWithPostFix, highlightQueryProperty);
        result.addField(rawHighlightField);
        result.addField(analyzedHighlightField);
    }
    return result.build();
}
Also used : HighlightQueryProperty(com.enonic.xp.query.highlight.HighlightQueryProperty) ElasticHighlightQuery(com.enonic.xp.repo.impl.elasticsearch.query.ElasticHighlightQuery) HighlightBuilder(org.elasticsearch.search.highlight.HighlightBuilder)

Example 2 with ElasticHighlightQuery

use of com.enonic.xp.repo.impl.elasticsearch.query.ElasticHighlightQuery in project xp by enonic.

the class ESQueryTranslator method doTranslate.

private static ElasticsearchQuery doTranslate(final SearchRequest request, final QueryTypeTranslator queryTypeTranslator) {
    final ESSource esSource = ESSourceFactory.create(request.getSearchSource());
    final QueryBuilder queryBuilder = queryTypeTranslator.createQueryBuilder(esSource.getFilters());
    final Query query = request.getQuery();
    final Set<AbstractAggregationBuilder> aggregations = new AggregationQueryBuilderFactory(queryTypeTranslator.getFieldNameResolver()).create(query.getAggregationQueries());
    final Set<SuggestBuilder.SuggestionBuilder> suggestions = new SuggestionQueryBuilderFactory(queryTypeTranslator.getFieldNameResolver()).create(query.getSuggestionQueries());
    final ElasticHighlightQuery highlight = new ElasticHighlightQueryBuilderFactory().create(query.getHighlight());
    final List<SortBuilder> sortBuilders = new SortQueryBuilderFactory(queryTypeTranslator.getFieldNameResolver()).create(query.getOrderBys());
    final QueryBuilder filterBuilder = new FilterBuilderFactory(queryTypeTranslator.getFieldNameResolver()).create(query.getPostFilters());
    return ElasticsearchQuery.create().addIndexNames(esSource.getIndexNames()).addIndexTypes(esSource.getIndexTypes()).query(queryBuilder).from(query.getFrom()).size(query.getSize()).explain(query.isExplain()).setReturnFields(request.getReturnFields()).setAggregations(aggregations).setSuggestions(suggestions).setHighlight(highlight).sortBuilders(sortBuilders).filter(filterBuilder).batchSize(queryTypeTranslator.getBatchSize()).searchMode(query.getSearchMode()).searchOptimizer(queryTypeTranslator.getSearchOptimizer()).searchPreference(request.getSearchPreference()).build();
}
Also used : SortBuilder(org.elasticsearch.search.sort.SortBuilder) ESSource(com.enonic.xp.repo.impl.elasticsearch.query.source.ESSource) NodeVersionQuery(com.enonic.xp.node.NodeVersionQuery) NodeVersionDiffQuery(com.enonic.xp.repo.impl.version.search.NodeVersionDiffQuery) ElasticHighlightQuery(com.enonic.xp.repo.impl.elasticsearch.query.ElasticHighlightQuery) ElasticsearchQuery(com.enonic.xp.repo.impl.elasticsearch.query.ElasticsearchQuery) NodeCommitQuery(com.enonic.xp.node.NodeCommitQuery) Query(com.enonic.xp.query.Query) NodeQuery(com.enonic.xp.node.NodeQuery) NodeBranchQuery(com.enonic.xp.repo.impl.branch.search.NodeBranchQuery) AbstractAggregationBuilder(org.elasticsearch.search.aggregations.AbstractAggregationBuilder) ElasticHighlightQuery(com.enonic.xp.repo.impl.elasticsearch.query.ElasticHighlightQuery) SuggestionQueryBuilderFactory(com.enonic.xp.repo.impl.elasticsearch.suggistion.query.SuggestionQueryBuilderFactory) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) FilterBuilderFactory(com.enonic.xp.repo.impl.elasticsearch.query.translator.factory.FilterBuilderFactory) ElasticHighlightQueryBuilderFactory(com.enonic.xp.repo.impl.elasticsearch.highlight.ElasticHighlightQueryBuilderFactory) SortQueryBuilderFactory(com.enonic.xp.repo.impl.elasticsearch.query.translator.factory.SortQueryBuilderFactory) AggregationQueryBuilderFactory(com.enonic.xp.repo.impl.elasticsearch.aggregation.query.AggregationQueryBuilderFactory)

Example 3 with ElasticHighlightQuery

use of com.enonic.xp.repo.impl.elasticsearch.query.ElasticHighlightQuery in project xp by enonic.

the class ElasticHighlightQueryBuilderFactoryTest method create.

@Test
public void create() {
    final HighlightQuery query = HighlightQuery.create().property(HighlightQueryProperty.create("propertyToHighlight").build()).build();
    final ElasticHighlightQuery elasticHighlightQuery = highlightQueryBuilderFactory.create(query);
    assertNotNull(elasticHighlightQuery);
    assertEquals(2, elasticHighlightQuery.getFields().size());
    final List<String> names = elasticHighlightQuery.getFields().stream().map(HighlightBuilder.Field::name).collect(Collectors.toList());
    assertTrue(names.containsAll(List.of("propertytohighlight._*", "propertytohighlight")));
}
Also used : ElasticHighlightQuery(com.enonic.xp.repo.impl.elasticsearch.query.ElasticHighlightQuery) HighlightQuery(com.enonic.xp.query.highlight.HighlightQuery) ElasticHighlightQuery(com.enonic.xp.repo.impl.elasticsearch.query.ElasticHighlightQuery) HighlightBuilder(org.elasticsearch.search.highlight.HighlightBuilder) Test(org.junit.jupiter.api.Test)

Example 4 with ElasticHighlightQuery

use of com.enonic.xp.repo.impl.elasticsearch.query.ElasticHighlightQuery in project xp by enonic.

the class ElasticHighlightQueryBuilderFactoryTest method create_null.

@Test
public void create_null() {
    final ElasticHighlightQuery elasticHighlightQuery = highlightQueryBuilderFactory.create(null);
    assertEquals(ElasticHighlightQuery.empty(), elasticHighlightQuery);
}
Also used : ElasticHighlightQuery(com.enonic.xp.repo.impl.elasticsearch.query.ElasticHighlightQuery) Test(org.junit.jupiter.api.Test)

Example 5 with ElasticHighlightQuery

use of com.enonic.xp.repo.impl.elasticsearch.query.ElasticHighlightQuery in project xp by enonic.

the class ElasticHighlightQueryBuilderFactoryTest method create_with_settings.

@Test
public void create_with_settings() {
    final HighlightQuery query = HighlightQuery.create().property(HighlightQueryProperty.create("propertyToHighlight").build()).settings(HighlightQuerySettings.create().encoder(Encoder.HTML).fragmenter(Fragmenter.SIMPLE).fragmentSize(1).noMatchSize(2).numOfFragments(3).order(Order.SCORE).addPreTags(List.of("<a>", "<b>")).addPostTags(List.of("<c>", "<d>")).requireFieldMatch(true).tagsSchema(TagsSchema.STYLED).build()).build();
    final ElasticHighlightQuery elasticHighlightQuery = highlightQueryBuilderFactory.create(query);
    assertNotNull(elasticHighlightQuery);
    assertEquals(2, elasticHighlightQuery.getFields().size());
    assertEquals(Encoder.HTML, elasticHighlightQuery.getEncoder());
    assertEquals(1, (int) elasticHighlightQuery.getFragmentSize());
    assertEquals(2, (int) elasticHighlightQuery.getNoMatchSize());
    assertEquals(3, (int) elasticHighlightQuery.getNumOfFragments());
    assertEquals(List.of("<a>", "<b>"), elasticHighlightQuery.getPreTags());
    assertEquals(List.of("<c>", "<d>"), elasticHighlightQuery.getPostTags());
    assertEquals(true, elasticHighlightQuery.getRequireFieldMatch());
    assertEquals(TagsSchema.STYLED, elasticHighlightQuery.getTagsSchema());
}
Also used : ElasticHighlightQuery(com.enonic.xp.repo.impl.elasticsearch.query.ElasticHighlightQuery) HighlightQuery(com.enonic.xp.query.highlight.HighlightQuery) ElasticHighlightQuery(com.enonic.xp.repo.impl.elasticsearch.query.ElasticHighlightQuery) Test(org.junit.jupiter.api.Test)

Aggregations

ElasticHighlightQuery (com.enonic.xp.repo.impl.elasticsearch.query.ElasticHighlightQuery)5 Test (org.junit.jupiter.api.Test)3 HighlightQuery (com.enonic.xp.query.highlight.HighlightQuery)2 HighlightBuilder (org.elasticsearch.search.highlight.HighlightBuilder)2 NodeCommitQuery (com.enonic.xp.node.NodeCommitQuery)1 NodeQuery (com.enonic.xp.node.NodeQuery)1 NodeVersionQuery (com.enonic.xp.node.NodeVersionQuery)1 Query (com.enonic.xp.query.Query)1 HighlightQueryProperty (com.enonic.xp.query.highlight.HighlightQueryProperty)1 NodeBranchQuery (com.enonic.xp.repo.impl.branch.search.NodeBranchQuery)1 AggregationQueryBuilderFactory (com.enonic.xp.repo.impl.elasticsearch.aggregation.query.AggregationQueryBuilderFactory)1 ElasticHighlightQueryBuilderFactory (com.enonic.xp.repo.impl.elasticsearch.highlight.ElasticHighlightQueryBuilderFactory)1 ElasticsearchQuery (com.enonic.xp.repo.impl.elasticsearch.query.ElasticsearchQuery)1 ESSource (com.enonic.xp.repo.impl.elasticsearch.query.source.ESSource)1 FilterBuilderFactory (com.enonic.xp.repo.impl.elasticsearch.query.translator.factory.FilterBuilderFactory)1 SortQueryBuilderFactory (com.enonic.xp.repo.impl.elasticsearch.query.translator.factory.SortQueryBuilderFactory)1 SuggestionQueryBuilderFactory (com.enonic.xp.repo.impl.elasticsearch.suggistion.query.SuggestionQueryBuilderFactory)1 NodeVersionDiffQuery (com.enonic.xp.repo.impl.version.search.NodeVersionDiffQuery)1 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)1 AbstractAggregationBuilder (org.elasticsearch.search.aggregations.AbstractAggregationBuilder)1