Search in sources :

Example 1 with DynamicOrderExpr

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

the class FindContentIdsByQueryCommandTest method test.

@Test
public void test() {
    FindNodesByQueryResult nodesByQueryResult = FindNodesByQueryResult.create().addNodeHit(NodeHit.create().nodeId(NodeId.from("nodeId")).score(1.0f).sort(SortValuesProperty.create().values(84).build()).highlight(HighlightedProperties.create().add(HighlightedProperty.create().name("name").addFragment("fragment").build()).build()).build()).hits(1).build();
    Mockito.when(nodeService.findByQuery(Mockito.any(NodeQuery.class))).thenReturn(nodesByQueryResult);
    FindContentIdsByQueryCommand command = FindContentIdsByQueryCommand.create().translator(translator).nodeService(nodeService).contentTypeService(contentTypeService).eventPublisher(eventPublisher).query(ContentQuery.create().queryExpr(QueryExpr.from(null, new DynamicOrderExpr(FunctionExpr.from("geoDistance", ValueExpr.string("my-value"), ValueExpr.geoPoint("83,80"), ValueExpr.string("km")), OrderExpr.Direction.ASC))).build()).build();
    FindContentIdsByQueryResult result = command.execute();
    Assertions.assertFalse(result.getSort().isEmpty());
    Assertions.assertEquals(1, result.getSort().size());
    Assertions.assertEquals(1, result.getScore().size());
}
Also used : FindContentIdsByQueryResult(com.enonic.xp.content.FindContentIdsByQueryResult) FindNodesByQueryResult(com.enonic.xp.node.FindNodesByQueryResult) DynamicOrderExpr(com.enonic.xp.query.expr.DynamicOrderExpr) NodeQuery(com.enonic.xp.node.NodeQuery) Test(org.junit.jupiter.api.Test)

Example 2 with DynamicOrderExpr

use of com.enonic.xp.query.expr.DynamicOrderExpr 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)

Example 3 with DynamicOrderExpr

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

the class SortBuilderFactoryTest method createGeoDistanceWithoutDirection.

@Test
public void createGeoDistanceWithoutDirection() {
    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"))), null));
    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)

Example 4 with DynamicOrderExpr

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

the class NodeOrderTest method geo_distance_sorting.

@Test
public void geo_distance_sorting() throws Exception {
    final Node node1 = createNode("node1", ValueFactory.newGeoPoint(GeoPoint.from("80,80")));
    final Node node2 = createNode("node2", ValueFactory.newGeoPoint(GeoPoint.from("81,80")));
    final Node node3 = createNode("node3", ValueFactory.newGeoPoint(GeoPoint.from("82,80")));
    final Node node4 = createNode("node4", ValueFactory.newGeoPoint(GeoPoint.from("83,80")));
    final NodeQuery distanceQuery = NodeQuery.create().query(QueryExpr.from(null, new DynamicOrderExpr(FunctionExpr.from("geoDistance", ValueExpr.string("my-value"), ValueExpr.geoPoint("83,80")), OrderExpr.Direction.ASC))).build();
    final SearchResult result = searchService.query(distanceQuery, SingleRepoSearchSource.from(ContextAccessor.current()));
    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 : DynamicOrderExpr(com.enonic.xp.query.expr.DynamicOrderExpr) 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)

Aggregations

DynamicOrderExpr (com.enonic.xp.query.expr.DynamicOrderExpr)4 Test (org.junit.jupiter.api.Test)4 NodeQuery (com.enonic.xp.node.NodeQuery)2 DslOrderExpr (com.enonic.xp.query.expr.DslOrderExpr)2 FieldOrderExpr (com.enonic.xp.query.expr.FieldOrderExpr)2 FunctionExpr (com.enonic.xp.query.expr.FunctionExpr)2 OrderExpr (com.enonic.xp.query.expr.OrderExpr)2 SearchQueryFieldNameResolver (com.enonic.xp.repo.impl.elasticsearch.query.translator.resolver.SearchQueryFieldNameResolver)2 HashSet (java.util.HashSet)2 FieldSortBuilder (org.elasticsearch.search.sort.FieldSortBuilder)2 GeoDistanceSortBuilder (org.elasticsearch.search.sort.GeoDistanceSortBuilder)2 SortBuilder (org.elasticsearch.search.sort.SortBuilder)2 FindContentIdsByQueryResult (com.enonic.xp.content.FindContentIdsByQueryResult)1 FindNodesByQueryResult (com.enonic.xp.node.FindNodesByQueryResult)1 Node (com.enonic.xp.node.Node)1 SearchResult (com.enonic.xp.repo.impl.search.result.SearchResult)1