Search in sources :

Example 1 with FunctionExpr

use of com.enonic.xp.query.expr.FunctionExpr in project xp by enonic.

the class DynamicSortBuilderFactory method create.

public SortBuilder create(final DynamicOrderExpr orderExpr) {
    final FunctionExpr function = orderExpr.getFunction();
    final String functionName = function.getName();
    switch(functionName) {
        case "geoDistance":
            return GeoDistanceSortFunction.create(orderExpr);
        default:
            throw new FunctionQueryBuilderException("Not valid sort function: '" + functionName + "'");
    }
}
Also used : FunctionExpr(com.enonic.xp.query.expr.FunctionExpr)

Example 2 with FunctionExpr

use of com.enonic.xp.query.expr.FunctionExpr in project xp by enonic.

the class GeoDistanceSortFunction method create.

public static SortBuilder create(final DynamicOrderExpr orderExpr) {
    final FunctionExpr function = orderExpr.getFunction();
    GeoDistanceSortFunctionArguments arguments = new GeoDistanceSortFunctionArguments(function.getArguments());
    final String baseFieldName = arguments.getFieldName();
    final String queryFieldName = new SearchQueryFieldNameResolver().resolve(baseFieldName, IndexValueType.GEO_POINT);
    GeoDistanceSortBuilder builder = new GeoDistanceSortBuilder(queryFieldName).point(arguments.getLatitude(), arguments.getLongitude());
    if (orderExpr.getDirection() != null) {
        builder.order(SortOrder.valueOf(orderExpr.getDirection().name()));
    }
    if (arguments.getUnit() != null) {
        builder.unit(DistanceUnit.fromString(arguments.getUnit()));
    }
    return builder;
}
Also used : FunctionExpr(com.enonic.xp.query.expr.FunctionExpr) SearchQueryFieldNameResolver(com.enonic.xp.repo.impl.elasticsearch.query.translator.resolver.SearchQueryFieldNameResolver) GeoDistanceSortBuilder(org.elasticsearch.search.sort.GeoDistanceSortBuilder)

Example 3 with FunctionExpr

use of com.enonic.xp.query.expr.FunctionExpr in project xp by enonic.

the class FunctionExpressionBuilderTest method test_fulltext.

@Test
public void test_fulltext() {
    List<ValueExpr> arguments = List.of(ValueExpr.string("myField"), ValueExpr.string("mySearchString"), ValueExpr.string("OR"));
    final QueryBuilder fulltext = FunctionExpressionBuilder.build(new FunctionExpr("fulltext", arguments));
    assertTrue(fulltext instanceof SimpleQueryStringBuilder);
}
Also used : ValueExpr(com.enonic.xp.query.expr.ValueExpr) FunctionExpr(com.enonic.xp.query.expr.FunctionExpr) SimpleQueryStringBuilder(org.elasticsearch.index.query.SimpleQueryStringBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) RangeQueryBuilder(org.elasticsearch.index.query.RangeQueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) Test(org.junit.jupiter.api.Test)

Example 4 with FunctionExpr

use of com.enonic.xp.query.expr.FunctionExpr in project xp by enonic.

the class NodeOrderTest method score_sorting.

@Test
public void score_sorting() throws Exception {
    final Node node1 = createNode("node1", ValueFactory.newString("denne har en fisk"));
    final Node node2 = createNode("node2", ValueFactory.newString("denne har fisk og ost"));
    final Node node3 = createNode("node3", ValueFactory.newString("mens denne har både fisk, ost og pølse"));
    final Node node4 = createNode("node4", ValueFactory.newString("denne vinner, siden den har alle sammen: fisk, ost, pølse og pai"));
    final FunctionExpr fulltextExpression = FunctionExpr.from("fulltext", ValueExpr.string("my-value"), ValueExpr.string("pai fisk pølse ost"), ValueExpr.string("OR"));
    final NodeQuery fulltextQuery = NodeQuery.create().query(QueryExpr.from(new DynamicConstraintExpr(fulltextExpression), FieldOrderExpr.create(IndexPath.from("_score"), OrderExpr.Direction.DESC))).build();
    printContentRepoIndex();
    final SearchResult result = searchService.query(fulltextQuery, SingleRepoSearchSource.from(ContextAccessor.current()));
    assertEquals(4, result.getNumberOfHits());
    final Iterator<String> iterator = result.getIds().iterator();
    assertEquals(node4.id(), NodeId.from(iterator.next()));
    assertEquals(node3.id(), NodeId.from(iterator.next()));
    assertEquals(node2.id(), NodeId.from(iterator.next()));
    assertEquals(node1.id(), NodeId.from(iterator.next()));
}
Also used : FunctionExpr(com.enonic.xp.query.expr.FunctionExpr) DynamicConstraintExpr(com.enonic.xp.query.expr.DynamicConstraintExpr) Node(com.enonic.xp.node.Node) NodeQuery(com.enonic.xp.node.NodeQuery) SearchResult(com.enonic.xp.repo.impl.search.result.SearchResult) Test(org.junit.jupiter.api.Test)

Example 5 with FunctionExpr

use of com.enonic.xp.query.expr.FunctionExpr in project xp by enonic.

the class SortBuilderFactoryTest method createGeoDistance.

@Test
public void createGeoDistance() {
    final Set<OrderExpr> orderExprs = new HashSet<>();
    orderExprs.add(new DynamicOrderExpr(new FunctionExpr("geoDistance", List.of(ValueExpr.string("myField"), ValueExpr.geoPoint("-50,40"), ValueExpr.string("km"))), OrderExpr.Direction.ASC));
    final List<SortBuilder> sortBuilders = new SortQueryBuilderFactory(new SearchQueryFieldNameResolver()).create(orderExprs);
    assertEquals(1, sortBuilders.size());
    assertTrue(sortBuilders.iterator().next() instanceof GeoDistanceSortBuilder);
}
Also used : FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder) GeoDistanceSortBuilder(org.elasticsearch.search.sort.GeoDistanceSortBuilder) SortBuilder(org.elasticsearch.search.sort.SortBuilder) FunctionExpr(com.enonic.xp.query.expr.FunctionExpr) DynamicOrderExpr(com.enonic.xp.query.expr.DynamicOrderExpr) OrderExpr(com.enonic.xp.query.expr.OrderExpr) DslOrderExpr(com.enonic.xp.query.expr.DslOrderExpr) FieldOrderExpr(com.enonic.xp.query.expr.FieldOrderExpr) DynamicOrderExpr(com.enonic.xp.query.expr.DynamicOrderExpr) SearchQueryFieldNameResolver(com.enonic.xp.repo.impl.elasticsearch.query.translator.resolver.SearchQueryFieldNameResolver) HashSet(java.util.HashSet) GeoDistanceSortBuilder(org.elasticsearch.search.sort.GeoDistanceSortBuilder) Test(org.junit.jupiter.api.Test)

Aggregations

FunctionExpr (com.enonic.xp.query.expr.FunctionExpr)9 Test (org.junit.jupiter.api.Test)6 DynamicConstraintExpr (com.enonic.xp.query.expr.DynamicConstraintExpr)4 FieldOrderExpr (com.enonic.xp.query.expr.FieldOrderExpr)4 OrderExpr (com.enonic.xp.query.expr.OrderExpr)4 SearchQueryFieldNameResolver (com.enonic.xp.repo.impl.elasticsearch.query.translator.resolver.SearchQueryFieldNameResolver)3 GeoDistanceSortBuilder (org.elasticsearch.search.sort.GeoDistanceSortBuilder)3 Content (com.enonic.xp.content.Content)2 ContentQuery (com.enonic.xp.content.ContentQuery)2 FindContentByQueryResult (com.enonic.xp.content.FindContentByQueryResult)2 DslOrderExpr (com.enonic.xp.query.expr.DslOrderExpr)2 DynamicOrderExpr (com.enonic.xp.query.expr.DynamicOrderExpr)2 HashSet (java.util.HashSet)2 FieldSortBuilder (org.elasticsearch.search.sort.FieldSortBuilder)2 SortBuilder (org.elasticsearch.search.sort.SortBuilder)2 Node (com.enonic.xp.node.Node)1 NodeQuery (com.enonic.xp.node.NodeQuery)1 LogicalExpr (com.enonic.xp.query.expr.LogicalExpr)1 QueryExpr (com.enonic.xp.query.expr.QueryExpr)1 ValueExpr (com.enonic.xp.query.expr.ValueExpr)1