Package org.infinispan.query

Examples of org.infinispan.query.SearchManager


         }

         Cache cache = cacheManager.getCache(CACHE_NAME);

         // check that our settings are not ignored
         SearchManager searchManager = Search.getSearchManager(cache);
         assertTrue(searchManager.getSearchFactory().getStatistics().isStatisticsEnabled());

         // add some test data
         for(int i = 0; i < numberOfEntries; i++) {
            Person person = new Person();
            person.setName("key" + i);
            person.setAge(i);
            person.setBlurb("value " + i);
            person.setNonSearchableField("i: " + i);

            cache.put("key" + i, person);
         }

         // after adding more classes and reconfiguring the SearchFactory it might happen isStatisticsEnabled is reset, so we check again
         assertTrue(searchManager.getSearchFactory().getStatistics().isStatisticsEnabled());

         assertEquals(0L, server.getAttribute(name, "SearchQueryExecutionCount"));

         QueryParser queryParser = createQueryParser("blurb");
         Query luceneQuery = queryParser.parse("value");
         CacheQuery cacheQuery = searchManager.getQuery(luceneQuery);
         List<Object> found = cacheQuery.list();

         assertEquals(1L, server.getAttribute(name, "SearchQueryExecutionCount"));

         assertEquals(numberOfEntries, found.size());
         assertEquals(numberOfEntries, server.invoke(name, "getNumberOfIndexedEntities",
                                       new Object[]{Person.class.getCanonicalName()},
                                       new String[]{String.class.getCanonicalName()}));

         assertEquals(1, searchManager.getSearchFactory().getStatistics().indexedEntitiesCount().size());

         // add more test data
         AnotherGrassEater anotherGrassEater = new AnotherGrassEater("Another grass-eater", "Eats grass");
         cache.put("key101", anotherGrassEater);

         cacheQuery = searchManager.getQuery(luceneQuery);
         found = cacheQuery.list();
         assertEquals(numberOfEntries, found.size());

         assertEquals(1, server.invoke(name, "getNumberOfIndexedEntities",
                                       new Object[]{AnotherGrassEater.class.getCanonicalName()},
                                       new String[]{String.class.getCanonicalName()}));

         Set<String> classNames = (Set<String>) server.getAttribute(name, "IndexedClassNames");
         assertEquals(2, classNames.size());
         assertTrue("The set should contain the Person class name.", classNames.contains(Person.class.getCanonicalName()));
         assertTrue("The set should contain the AnotherGrassEater class name.", classNames.contains(AnotherGrassEater.class.getCanonicalName()));
         assertEquals(2, searchManager.getSearchFactory().getStatistics().indexedEntitiesCount().size());

         // check the statistics and see they have reasonable values
         assertTrue("The query execution total time should be > 0.", (Long) server.getAttribute(name, "SearchQueryTotalTime") > 0);
         assertEquals(2L, server.getAttribute(name, "SearchQueryExecutionCount"));
         assertEquals("blurb:value", server.getAttribute(name, "SearchQueryExecutionMaxTimeQueryString"));
View Full Code Here


   }

   public static CacheQuery createCacheQuery(Cache m_cache, String fieldName, String searchString) throws ParseException {
      QueryParser qp = createQueryParser(fieldName);
      Query parsedQuery = qp.parse(searchString);
      SearchManager queryFactory = Search.getSearchManager(m_cache);
      CacheQuery cacheQuery = queryFactory.getQuery(parsedQuery);
      return cacheQuery;
   }
View Full Code Here

      ProtobufValueWrapper wrapper2 = new ProtobufValueWrapper(createMarshalledUser("John", "Batman"));

      cache.put(1, wrapper1);   //todo how do we index if the key is a byte array?
      cache.put(2, wrapper2);

      SearchManager qf = Search.getSearchManager(cache);

      SearchFactoryImplementor searchFactory = (SearchFactoryImplementor) qf.getSearchFactory();
      assertNotNull(searchFactory.getIndexManagerHolder().getIndexManager(ProtobufValueWrapper.class.getName()));

      Query luceneQuery = qf.buildQueryBuilderForClass(ProtobufValueWrapper.class)
            .get()
            .keyword()
            .onField("name")
            .ignoreFieldBridge()   //todo [anistor] ignoring the field bridge is a shameless hack!
            .matching("Adrian")
            .createQuery();

      List<Object> list = qf.getQuery(luceneQuery).list();
      assertEquals(1, list.size());

      // the alternative ....

      QueryBuilder guestQueryBuilder = searchFactory.buildQueryBuilder().forEntity(ProtobufValueWrapper.class).get();
View Full Code Here

   private byte[] executeQuery(AdvancedCache<byte[], byte[]> cache, byte[] query) throws IOException {
      final SerializationContext serCtx = ProtobufMetadataManager.getSerializationContext(cache.getCacheManager());

      QueryRequest request = ProtobufUtil.fromByteArray(serCtx, query, 0, query.length, QueryRequest.class);

      SearchManager searchManager = Search.getSearchManager(cache);
      Query luceneQuery;
      List<String> projections;
      Class targetEntity;
      Descriptors.Descriptor messageDescriptor;

      QueryParser queryParser = new QueryParser();
      if (cache.getCacheConfiguration().compatibility().enabled()) {
         final QueryInterceptor queryInterceptor = ComponentRegistryUtils.getQueryInterceptor(cache);
         EntityNamesResolver entityNamesResolver = new EntityNamesResolver() {
            @Override
            public Class<?> getClassFromName(String entityName) {
               MessageMarshaller messageMarshaller = (MessageMarshaller) serCtx.getMarshaller(entityName);
               Class clazz = messageMarshaller.getJavaClass();
               Boolean isIndexed = queryInterceptor.getKnownClasses().get(clazz);
               return isIndexed != null && isIndexed ? clazz : null;
            }
         };

         LuceneProcessingChain processingChain = new LuceneProcessingChain((SearchFactoryIntegrator) searchManager.getSearchFactory(), entityNamesResolver, null);
         LuceneQueryParsingResult parsingResult = queryParser.parseQuery(request.getJpqlString(), processingChain);

         MessageMarshaller messageMarshaller = (MessageMarshaller) serCtx.getMarshaller(parsingResult.getTargetEntity());
         messageDescriptor = serCtx.getMessageDescriptor(messageMarshaller.getTypeName());
         targetEntity = parsingResult.getTargetEntity();
         projections = parsingResult.getProjections();
         luceneQuery = parsingResult.getQuery();
      } else {
         EntityNamesResolver entityNamesResolver = new EntityNamesResolver() {
            @Override
            public Class<?> getClassFromName(String entityName) {
               try {
                  //todo [anistor] this just checks if the type is known
                  serCtx.getMessageDescriptor(entityName);
               } catch (Exception e) {
                  return null;
               }
               return ProtobufValueWrapper.class;
            }
         };

         IspnLuceneProcessingChain processingChain = new IspnLuceneProcessingChain(serCtx, (SearchFactoryIntegrator) searchManager.getSearchFactory(), entityNamesResolver, null);
         IspnLuceneQueryParsingResult parsingResult = queryParser.parseQuery(request.getJpqlString(), processingChain);
         messageDescriptor = parsingResult.getTargetType();
         targetEntity = parsingResult.getTargetEntity();
         projections = parsingResult.getProjections();

         QueryBuilder qb = searchManager.getSearchFactory().buildQueryBuilder().forEntity(targetEntity).get();
         luceneQuery = qb.bool()
               .must(qb.keyword().onField(TYPE_FIELD_NAME).ignoreFieldBridge().matching(messageDescriptor.getFullName()).createQuery())
               .must(parsingResult.getQuery())
               .createQuery();
      }

      CacheQuery cacheQuery = searchManager.getQuery(luceneQuery, targetEntity);

      if (request.getSortCriteria() != null && !request.getSortCriteria().isEmpty()) {
         SortField[] sortField = new SortField[request.getSortCriteria().size()];
         int i = 0;
         for (QueryRequest.SortCriteria sc : request.getSortCriteria()) {
View Full Code Here

       return TestCacheManagerFactory.createCacheManager(builder);
    }

    private List searchByName(String name, Cache c) {
        SearchManager sm = Search.getSearchManager(c);
        CacheQuery q = sm.getQuery(SEntity.searchByName(name), SEntity.class);
        int resultSize = q.getResultSize();
        List l = q.list();
        assert l.size() == resultSize;
        return q.list();
    }
View Full Code Here

                     "Paolo Perrotta",
                     "The Pragmatic Programmers"));

      CountingClassLoader classLoader = new CountingClassLoader();
      AdvancedCache<Object, Object> applicationCache = cache.getAdvancedCache().with(classLoader);
      SearchManager qf = Search.getSearchManager(applicationCache);

      assert classLoader.countInvocations.get() == 0;

      Query query = qf.buildQueryBuilderForClass(Book.class)
         .get()
            .phrase()
               .onField("title")
               .sentence("in action")
            .createQuery();

      List<Object> list = qf.getQuery(query).list();
      assert list.size() == 2;
      int invocationsCount = classLoader.countInvocations.get();
      assert invocationsCount >= 1 : "Received instead " + invocationsCount + " invocations";
   }
View Full Code Here

      }
   }

   private boolean isMasterNode(Cache cache) {
      //Implicitly verifies the components are setup as configured by casting:
      SearchManager searchManager = Search.getSearchManager(cache);
      SearchFactoryImplementor searchFactory = (SearchFactoryImplementor) searchManager.getSearchFactory();
      InfinispanIndexManager indexManager = (InfinispanIndexManager) searchFactory.getAllIndexesManager().getIndexManager("person");
      InfinispanCommandsBackend commandsBackend = indexManager.getRemoteMaster();
      return commandsBackend.isMasterLocal();
   }
View Full Code Here

      return commandsBackend.isMasterLocal();
   }

   protected void assertIndexSize(int expectedIndexSize) {
      for (Cache cache : caches) {
         SearchManager searchManager = Search.getSearchManager(cache);
         CacheQuery query = searchManager.getQuery(new MatchAllDocsQuery(), Person.class);
         Assert.assertEquals(expectedIndexSize, query.list().size());
      }
   }
View Full Code Here

      Assert.fail("Expected to find interceptor " + interceptor + " among custom interceptors of cache, but it was not there.");
   }

   private static void assertFindBook(Cache<Object, Object> cache) {
      SearchManager searchManager = Search.getSearchManager(cache);
      QueryBuilder queryBuilder = searchManager.buildQueryBuilderForClass(Book.class).get();
      Query luceneQuery = queryBuilder.keyword().onField("title").matching("infinispan").createQuery();
      CacheQuery cacheQuery = searchManager.getQuery(luceneQuery);
      List<Object> list = cacheQuery.list();
      Assert.assertEquals(1, list.size());
   }
View Full Code Here

   @Test(expectedExceptions = IllegalArgumentException.class)
   public void testSearchWithWrongCache() throws ParseException {
      Cache cache = mock(CacheImpl.class);
      when(cache.getAdvancedCache()).thenReturn(null);

      SearchManager manager = Search.getSearchManager(cache);
   }
View Full Code Here

TOP

Related Classes of org.infinispan.query.SearchManager

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.