ftq.setProjection("myContentText"); ftq.setMaxResults(20);
query.setProjection(projectionFields); query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
query.setProjection(projectionFields); query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
jpaQuery.setProjection("myId");
fullTextQuery.setProjection(ProjectionConstants.SCORE, ProjectionConstants.THIS); final List<T> classes = new ArrayList<>();
@SuppressWarnings("unchecked") public <T> List<Serializable> findId(Class<T> clazz, SearchParameters sp, List<SingularAttribute<?, ?>> availableProperties) { log.info("Searching {} with terms : {} with available Properties: {}", new Object[]{clazz.getSimpleName(), sp.getTerms(), availableProperties}); FullTextEntityManager fullTextEntityManager = getFullTextEntityManager(entityManager); Query query = sp.getLuceneQueryBuilder().build(fullTextEntityManager, sp, availableProperties); if (query == null) { return null; } FullTextQuery ftq = fullTextEntityManager.createFullTextQuery( // query, clazz); ftq.setProjection("id"); if (sp.getMaxResults() > 0) { ftq.setMaxResults(sp.getMaxResults()); } List<Serializable> ids = newArrayList(); List<Object[]> resultList = ftq.getResultList(); for (Object[] result : resultList) { ids.add((Serializable) result[0]); } return ids; }
/** * Same as {@link #find(Class, SearchParameters, String[])} but will return only the id */ @SuppressWarnings("unchecked") public <T> List<Serializable> findId(Class<T> clazz, SearchParameters sp, List<SingularAttribute<?, ?>> availableProperties) { log.info("Searching {} with terms : {} with available Properties: {}", new Object[] { clazz.getSimpleName(), sp.getTerms(), availableProperties }); FullTextEntityManager fullTextEntityManager = getFullTextEntityManager(entityManager); Query query = sp.getLuceneQueryBuilder().build(fullTextEntityManager, sp, availableProperties); if (query == null) { return null; } FullTextQuery ftq = fullTextEntityManager.createFullTextQuery( // query, clazz); ftq.setProjection("id"); if (sp.getMaxResults() > 0) { ftq.setMaxResults(sp.getMaxResults()); } List<Serializable> ids = newArrayList(); List<Object[]> resultList = ftq.getResultList(); for (Object[] result : resultList) { ids.add((Serializable) result[0]); } return ids; }
ftq.setProjection("myContentText"); ftq.setMaxResults(20);
fullTextQuery.setProjection("id"); List<Object[]> results = fullTextQuery.getResultList();
public List<String> searchName(String name) { FullTextEntityManager fullTextEm = Search.getFullTextEntityManager( em ); Query luceneQuery = fullTextEm.getSearchFactory().buildQueryBuilder() .forEntity( RegisteredMember.class ).get() .keyword().onField( "name" ).matching( name ).createQuery(); List<?> resultList = fullTextEm.createFullTextQuery( luceneQuery ).setProjection( "name" ).getResultList(); List<String> names = new ArrayList<>( resultList.size() ); for ( Object projection : resultList ) { names.add( (String) ( ( (Object[]) projection )[0] ) ); } return names; }
@Test public void testQueryViaEntityManager() { EntityManager em = factory.createEntityManager(); FullTextEntityManager ftem = Search.getFullTextEntityManager( em ); ftem.getTransaction().begin(); QueryDescriptor query = ElasticsearchQueries.fromJson( "{ 'query': { 'match' : { 'lastName' : 'Hergesheimer' } } }" ); Object[] result = (Object[]) ftem.createFullTextQuery( query, GolfPlayer.class ) .setProjection( ElasticsearchProjectionConstants.SOURCE ) .getSingleResult(); String source = (String) result[0]; JsonHelper.assertJsonEqualsIgnoringUnknownFields( "{" + "'lastName': 'Hergesheimer'," + "'fullName': 'Klaus Hergesheimer'," + "}", source ); ftem.getTransaction().commit(); ftem.close(); }
jpaQuery.setProjection("myId");
hibQuery.setProjection( FullTextQuery.THIS, FullTextQuery.SPATIAL_DISTANCE ); hibQuery.setSpatialParameters( centerLatitude, centerLongitude, "location" ); hibQuery.setSort( builder.sort().byField( "idSort" ).createSort() ); hibQuery.setProjection( FullTextQuery.THIS, FullTextQuery.SPATIAL_DISTANCE ); hibQuery.setSpatialParameters( centerLatitude, centerLongitude, Spatial.COORDINATES_DEFAULT_FIELD ); hibQuery.setSort( builder.sort().byField( "idSort" ).createSort() );
Sort distanceSort = new Sort( new DistanceSortField( centerLatitude, centerLongitude, "location" ) ); hibQuery.setSort( distanceSort ); hibQuery.setProjection( FullTextQuery.THIS, FullTextQuery.SPATIAL_DISTANCE ); hibQuery.setSpatialParameters( centerLatitude, centerLongitude, "location" ); List results = hibQuery.getResultList();
hibQuery.setProjection( FullTextQuery.THIS, FullTextQuery.SPATIAL_DISTANCE ); hibQuery.setSpatialParameters( centerLatitude, centerLongitude, "location" ); hibQuery.setSort( builder.sort().byField( "idSort" ).createSort() ); hibQuery.setProjection( FullTextQuery.THIS, FullTextQuery.SPATIAL_DISTANCE ); hibQuery.setSpatialParameters( centerLatitude, centerLongitude, "location" ); hibQuery.setSort( builder.sort().byField( "idSort" ).createSort() );
hibQuery.setProjection( FullTextQuery.THIS, FullTextQuery.SPATIAL_DISTANCE ); hibQuery.setSpatialParameters( centerLatitude, centerLongitude, "location" ); hibQuery.setSort( builder.sort().byField( "idSort" ).createSort() );
Sort distanceSort = new Sort( new DistanceSortField( centerLatitude, centerLongitude, "location" ) ); hibQuery.setSort( distanceSort ); hibQuery.setProjection( FullTextQuery.THIS, FullTextQuery.SPATIAL_DISTANCE ); hibQuery.setSpatialParameters( centerLatitude, centerLongitude, "location" ); List results = hibQuery.getResultList();
Sort distanceSort = new Sort( new DistanceSortField( centerLatitude, centerLongitude, "location" ) ); hibQuery.setSort( distanceSort ); hibQuery.setProjection( FullTextQuery.THIS, FullTextQuery.SPATIAL_DISTANCE ); hibQuery.setSpatialParameters( centerLatitude, centerLongitude, "location" ); List results = hibQuery.getResultList();
hibQuery.setSort( distanceSort ); hibQuery.setMaxResults( 1000 ); hibQuery.setProjection( FullTextQuery.THIS, FullTextQuery.SPATIAL_DISTANCE ); hibQuery.setSpatialParameters( centerLatitude, centerLongitude, "location" ); @SuppressWarnings("unchecked")
Sort distanceSort = new Sort( new DistanceSortField( centerLatitude, centerLongitude, "location" ) ); hibQuery.setSort( distanceSort ); hibQuery.setProjection( FullTextQuery.THIS, FullTextQuery.SPATIAL_DISTANCE );