Ejemplo n.º 1
0
  public void testAdded() throws Exception {
    prepareTestData();
    queryParser = createQueryParser("blurb");

    luceneQuery = queryParser.parse("eats");
    cacheQuery = Search.getSearchManager(cache2).getQuery(luceneQuery);
    List<Object> found = cacheQuery.list();

    AssertJUnit.assertEquals(2, found.size());
    assert found.contains(person2);
    assert found.contains(person3);
    assert !found.contains(person4) : "This should not contain object person4";

    person4 = new Person();
    person4.setName("Mighty Goat");
    person4.setBlurb("Also eats grass");

    cache1.put("mighty", person4);

    luceneQuery = queryParser.parse("eats");
    cacheQuery = Search.getSearchManager(cache2).getQuery(luceneQuery);
    found = cacheQuery.list();

    AssertJUnit.assertEquals(3, found.size());
    assert found.contains(person2);
    assert found.contains(person3);
    assert found.contains(person4) : "This should now contain object person4";
  }
Ejemplo n.º 2
0
  public void testModified() throws Exception {
    prepareTestData();
    assertQueryInterceptorPresent(cache2);

    queryParser = createQueryParser("blurb");
    luceneQuery = queryParser.parse("playing");
    cacheQuery = Search.getSearchManager(cache2).getQuery(luceneQuery);

    List<Object> found = cacheQuery.list();

    assert found.size() == 1 : "Expected list of size 1, was of size " + found.size();
    assert found.get(0).equals(person1);

    person1.setBlurb("Likes pizza");
    cache1.put("Navin", person1);

    queryParser = createQueryParser("blurb");
    luceneQuery = queryParser.parse("pizza");
    cacheQuery = Search.getSearchManager(cache2).getQuery(luceneQuery);

    found = cacheQuery.list();

    assert found.size() == 1;
    assert found.get(0).equals(person1);
  }
 protected void checkIndex(int expectedNumber, Class<?> entity) {
   for (Cache<?, ?> c : caches) {
     SearchManager searchManager = Search.getSearchManager(c);
     assertEquals(
         searchManager.getQuery(new MatchAllDocsQuery(), entity).getResultSize(), expectedNumber);
   }
 }
Ejemplo n.º 4
0
 @Override
 protected void rebuildIndexes() throws Exception {
   Cache cache = caches.get(0);
   SearchManager searchManager = Search.getSearchManager(cache);
   NotifyingFuture<Void> future = searchManager.getMassIndexer().startAsync();
   future.get();
 }
Ejemplo n.º 5
0
  @Override
  public byte[] query(AdvancedCache<byte[], byte[]> cache, byte[] query) {
    try {
      SerializationContext serCtx =
          ProtobufMetadataManagerImpl.getSerializationContextInternal(cache.getCacheManager());
      QueryRequest request =
          ProtobufUtil.fromByteArray(serCtx, query, 0, query.length, QueryRequest.class);

      Configuration cacheConfiguration = SecurityActions.getCacheConfiguration(cache);
      boolean isIndexed = cacheConfiguration.indexing().index().isEnabled();
      boolean isCompatMode = cacheConfiguration.compatibility().enabled();
      SearchManager searchManager =
          isIndexed ? Search.getSearchManager(cache) : null; // this also checks access permissions
      RemoteQueryEngine queryEngine =
          new RemoteQueryEngine(cache, searchManager, isCompatMode, serCtx);

      long startOffset = request.getStartOffset() == null ? -1 : request.getStartOffset();
      int maxResults = request.getMaxResults() == null ? -1 : request.getMaxResults();
      Map<String, Object> namedParameters = getNamedParameters(request);
      Query q =
          queryEngine.buildQuery(
              null, request.getJpqlString(), namedParameters, startOffset, maxResults);

      QueryResponse response = makeResponse(q);
      return ProtobufUtil.toByteArray(serCtx, response);
    } catch (IOException e) {
      throw log.errorExecutingQuery(e);
    }
  }
Ejemplo n.º 6
0
  public static List<Object> performSearch(
      Select command, Class<?> type, String cacheName, CacheContainerWrapper cache)
      throws TranslatorException {

    LogManager.logTrace(LogConstants.CTX_CONNECTOR, "Using Lucene Searching."); // $NON-NLS-1$

    // Map<?, ?> cache,
    SearchManager searchManager = Search.getSearchManager((Cache<?, ?>) cache.getCache(cacheName));

    QueryBuilder queryBuilder = searchManager.buildQueryBuilderForClass(type).get();

    BooleanJunction<BooleanJunction> junction = queryBuilder.bool();
    boolean createdQueries = buildQueryFromWhereClause(command.getWhere(), junction, queryBuilder);

    Query query = null;
    if (createdQueries) {
      query = junction.createQuery();
    } else {
      query = queryBuilder.all().createQuery();
    }

    CacheQuery cacheQuery = searchManager.getQuery(query, type); // rootNodeType

    List<Object> results = cacheQuery.list();
    if (results == null || results.isEmpty()) {
      return Collections.emptyList();
    }

    return results;
  }
 public void setCache(Cache<?, ?> cache, EmbeddedCacheManager cacheManager) {
   this.cacheManager = cacheManager;
   this.cache = cache;
   SearchManager searchManager = Search.getSearchManager(cache);
   SearchFactory searchFactory = searchManager.getSearchFactory();
   searchFactoryImplementor = (SearchFactoryImplementor) searchFactory;
   queryInterceptor = ComponentRegistryUtils.getQueryInterceptor(cache);
 }
Ejemplo n.º 8
0
 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;
 }
 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();
 }
Ejemplo n.º 10
0
  public void testRemoved() throws Exception {
    prepareTestData();
    queryParser = createQueryParser("blurb");
    luceneQuery = queryParser.parse("eats");
    cacheQuery = Search.getSearchManager(cache2).getQuery(luceneQuery);
    List<Object> found = cacheQuery.list();

    assert found.size() == 2;
    assert found.contains(person2);
    assert found.contains(person3) : "This should still contain object person3";

    cache1.remove(key3);

    queryParser = createQueryParser("blurb");
    luceneQuery = queryParser.parse("eats");
    cacheQuery = Search.getSearchManager(cache2).getQuery(luceneQuery);
    found = cacheQuery.list();
  }
Ejemplo n.º 11
0
  public void testGetResultSize() throws Exception {
    prepareTestData();
    queryParser = createQueryParser("blurb");
    luceneQuery = queryParser.parse("playing");
    cacheQuery = Search.getSearchManager(cache2).getQuery(luceneQuery);
    List<Object> found = cacheQuery.list();

    AssertJUnit.assertEquals(1, found.size());
  }
Ejemplo n.º 12
0
 @BeforeClass
 public void prepareSearchFactory() throws Exception {
   qf = Search.getSearchManager(cache);
   cache.put("195 Inter", new Car("Ferrari 195 Inter", "Rosso corsa", 2341));
   cache.put("212 Inter", new Car("Ferrari 212 Inter", "black", 4000));
   cache.put("500_Superfast", new Car("Ferrari 500_Superfast", "Rosso corsa", 4000));
   // test for duplication:
   cache.put("500_Superfast", new Car("Ferrari 500_Superfast", "Rosso corsa", 4000));
 }
Ejemplo n.º 13
0
 private CacheQuery queryByNameField(String name, Class<?> entity) {
   SearchManager sm = Search.getSearchManager(cache);
   Query query =
       sm.buildQueryBuilderForClass(entity)
           .get()
           .keyword()
           .onField("name")
           .matching(name)
           .createQuery();
   return sm.getQuery(query);
 }
Ejemplo n.º 14
0
 /**
  * This method filters task based on the input
  *
  * @param input - string to filter on
  * @return
  */
 public Collection<Task> filter(String input) {
   SearchManager sm = Search.getSearchManager(cache);
   QueryBuilder qb = sm.buildQueryBuilderForClass(Task.class).get();
   Query q = qb.keyword().onField("title").matching(input).createQuery();
   CacheQuery cq = sm.getQuery(q, Task.class);
   List<Task> tasks = new ArrayList<Task>();
   for (Object object : cq) {
     tasks.add((Task) object);
   }
   return tasks;
 }
Ejemplo n.º 15
0
  public void testSearchKeyTransformer() throws Exception {
    Cache cache1 = caches().get(0);
    Cache cache2 = caches().get(1);

    SearchManager manager = Search.getSearchManager(cache1);
    SearchManager manager1 = Search.getSearchManager(cache2);
    manager.registerKeyTransformer(CustomKey3.class, CustomKey3Transformer.class);
    manager1.registerKeyTransformer(CustomKey3.class, CustomKey3Transformer.class);

    prepareTestedObjects();

    TransactionManager transactionManager = cache1.getAdvancedCache().getTransactionManager();

    CustomKey3 customeKey1 = new CustomKey3(key1);
    CustomKey3 customeKey2 = new CustomKey3(key2);
    CustomKey3 customeKey3 = new CustomKey3(key3);

    // Put the 3 created objects in the cache1.
    if (transactionsEnabled()) transactionManager.begin();
    cache1.put(customeKey1, person1);
    cache1.put(customeKey2, person2);
    cache2.put(customeKey3, person3);
    if (transactionsEnabled()) transactionManager.commit();

    queryParser = createQueryParser("blurb");
    Query luceneQuery = queryParser.parse("Eats");

    CacheQuery cacheQuery = manager.getQuery(luceneQuery);

    ResultIterator found =
        cacheQuery.iterator(new FetchOptions().fetchMode(FetchOptions.FetchMode.LAZY));

    int counter = 0;
    while (found.hasNext()) {
      found.next();
      counter++;
    }

    AssertJUnit.assertEquals(2, counter);
  }
Ejemplo n.º 16
0
  public void testClear() throws Exception {
    prepareTestData();
    queryParser = createQueryParser("blurb");
    luceneQuery = queryParser.parse("eats");
    cacheQuery = Search.getSearchManager(cache1).getQuery(luceneQuery);

    Query[] queries = new Query[2];
    queries[0] = luceneQuery;

    luceneQuery = queryParser.parse("playing");
    queries[1] = luceneQuery;

    Query luceneQuery = queries[0].combine(queries);
    CacheQuery cacheQuery = Search.getSearchManager(cache1).getQuery(luceneQuery);
    AssertJUnit.assertEquals(3, cacheQuery.getResultSize());

    cache2.clear();

    AssertJUnit.assertEquals(3, cacheQuery.getResultSize());
    cacheQuery = Search.getSearchManager(cache1).getQuery(luceneQuery);

    AssertJUnit.assertEquals(0, cacheQuery.getResultSize());
  }
Ejemplo n.º 17
0
 private void queryTest() {
   cache.put("jekyll", new TestEntity("Henry", "Jekyll", 1, "dissociate identity disorder"));
   cache.put("hyde", new TestEntity("Edward", "Hyde", 2, "dissociate identity disorder"));
   SearchManager sm = Search.getSearchManager(cache);
   Query query =
       sm.buildQueryBuilderForClass(TestEntity.class)
           .get()
           .keyword()
           .onField("name")
           .matching("Henry")
           .createQuery();
   CacheQuery q = sm.getQuery(query);
   assertEquals(1, q.getResultSize());
   assertEquals(TestEntity.class, q.list().get(0).getClass());
 }
Ejemplo n.º 18
0
 private void recreateCacheManager() {
   ConfigurationBuilder cfg = new ConfigurationBuilder();
   cfg.loaders()
       .preload(true)
       .passivation(true)
       .addStore()
       .cacheStore(new DummyInMemoryCacheStore())
       .purgeOnStartup(true)
       .indexing()
       .enable()
       .addProperty("default.directory_provider", "ram")
       .addProperty("lucene_version", "LUCENE_CURRENT");
   cm = TestCacheManagerFactory.createCacheManager(cfg);
   cache = cm.getCache();
   store = TestingUtil.extractComponent(cache, CacheLoaderManager.class).getCacheStore();
   search = Search.getSearchManager(cache);
 }
  private void checkIndexPresence(Cache cache) {
    SearchIntegrator searchFactory = Search.getSearchManager(cache).unwrap(SearchIntegrator.class);

    assertTrue(searchFactory.getIndexedTypes().contains(getModelFactory().getUserImplClass()));
    assertNotNull(searchFactory.getIndexManager(getModelFactory().getUserImplClass().getName()));

    assertTrue(searchFactory.getIndexedTypes().contains(getModelFactory().getAccountImplClass()));
    assertNotNull(searchFactory.getIndexManager(getModelFactory().getAccountImplClass().getName()));

    assertTrue(
        searchFactory.getIndexedTypes().contains(getModelFactory().getTransactionImplClass()));
    assertNotNull(
        searchFactory.getIndexManager(getModelFactory().getTransactionImplClass().getName()));

    assertFalse(searchFactory.getIndexedTypes().contains(getModelFactory().getAddressImplClass()));
    assertNull(searchFactory.getIndexManager(getModelFactory().getAddressImplClass().getName()));
  }
Ejemplo n.º 20
0
  public void testPutMap() throws Exception {
    prepareTestData();
    SearchManager searchManager = Search.getSearchManager(cache2);
    QueryBuilder queryBuilder = searchManager.buildQueryBuilderForClass(Person.class).get();
    Query allQuery = queryBuilder.all().createQuery();
    assert searchManager.getQuery(allQuery, Person.class).list().size() == 3;

    Map<String, Person> allWrites = new HashMap<String, Person>();
    allWrites.put(key1, person1);
    allWrites.put(key2, person2);
    allWrites.put(key3, person3);

    cache2.putAll(allWrites);
    List found = searchManager.getQuery(allQuery, Person.class).list();
    AssertJUnit.assertEquals(3, found.size());

    cache2.putAll(allWrites);
    found = searchManager.getQuery(allQuery, Person.class).list();
    AssertJUnit.assertEquals(3, found.size());
  }
Ejemplo n.º 21
0
  public void testFullTextFilterOnOff() throws Exception {
    prepareTestData();
    queryParser = createQueryParser("blurb");
    Query luceneQuery = queryParser.parse("eats");

    CacheQuery query = Search.getSearchManager(cache1).getQuery(luceneQuery);
    FullTextFilter filter = query.enableFullTextFilter("personFilter");
    filter.setParameter("blurbText", "cheese");

    AssertJUnit.assertEquals(1, query.getResultSize());
    List result = query.list();

    Person person = (Person) result.get(0);
    AssertJUnit.assertEquals("MiniGoat", person.getName());
    AssertJUnit.assertEquals("Eats cheese", person.getBlurb());

    // Disabling the fullTextFilter.
    query.disableFullTextFilter("personFilter");
    AssertJUnit.assertEquals(2, query.getResultSize());
  }
Ejemplo n.º 22
0
 @Override
 protected void createCacheManagers() throws Throwable {
   ConfigurationBuilder cacheCfg = getDefaultClusteredCacheConfig(CACHE_MODE, TX_ENABLED);
   cacheCfg
       .clustering()
       .sync()
       .replTimeout(120000)
       .indexing()
       .index(Index.LOCAL)
       .addProperty("default.directory_provider", DIRECTORY_PROVIDER.toString())
       .addProperty("default.indexmanager", INDEX_MANAGER.toString())
       .addProperty("default.indexwriter.merge_factor", MERGE_FACTOR)
       .addProperty("hibernate.search.default.worker.execution", WORKER_MODE.toString())
       .addProperty("error_handler", "org.infinispan.query.helper.StaticTestingErrorHandler")
       .addProperty("lucene_version", "LUCENE_CURRENT");
   ;
   List<Cache<Integer, Transaction>> caches = createClusteredCaches(2, cacheCfg);
   cache1 = caches.get(0);
   cache2 = caches.get(1);
   massIndexer = Search.getSearchManager(cache1).getMassIndexer();
 }
  @Test
  public void testIndexAndQuery() throws Exception {
    SearchManager searchManager = Search.getSearchManager(cache);

    cache.put(123405, new Bond(new Date(System.currentTimeMillis()), 450L));
    assertEfficientIndexingUsed(searchManager.unwrap(SearchIntegrator.class), Bond.class);

    cache.put(123502, new Debenture("GB", 116d));
    assertEfficientIndexingUsed(searchManager.unwrap(SearchIntegrator.class), Debenture.class);

    cache.put(223456, new Bond(new Date(System.currentTimeMillis()), 550L));
    assertEfficientIndexingUsed(searchManager.unwrap(SearchIntegrator.class), Bond.class);

    CacheQuery query = searchManager.getQuery(new MatchAllDocsQuery(), Bond.class, Debenture.class);
    assertEquals(query.list().size(), 3);

    CacheQuery queryBond = searchManager.getQuery(new MatchAllDocsQuery(), Bond.class);
    assertEquals(queryBond.getResultSize(), 2);

    CacheQuery queryDeb = searchManager.getQuery(new MatchAllDocsQuery(), Debenture.class);
    assertEquals(queryDeb.getResultSize(), 1);
  }
Ejemplo n.º 24
0
      @Override
      void execute(Exchange exchange) {
        if (configuration.getQueryBuilderStrategy() == null) {
          throw new RuntimeException("QueryBuilderStrategy is required for executing queries!");
        }

        QueryFactory factory;
        if (cache instanceof RemoteCache) {
          factory = Search.getQueryFactory((RemoteCache) cache);
        } else {
          SearchManager searchManager = org.infinispan.query.Search.getSearchManager((Cache) cache);
          factory = searchManager.getQueryFactory();
        }

        QueryBuilder queryBuilder =
            configuration.getQueryBuilderStrategy().createQueryBuilder(factory);
        if (queryBuilder == null) {
          throw new RuntimeException("QueryBuilder not created!");
        }
        List<?> result = queryBuilder.build().list();
        setResult(result, exchange);
      }
  @Override
  public List<Comment> getCommentsBySongId(String songId) {
    if (songId == null) {
      throw new IllegalArgumentException("Song id is null.");
    }
    ArrayList<Comment> comments = new ArrayList<Comment>();
    commentCache = provider.getCacheContainer().getCache("commentcache");
    SearchManager sm = Search.getSearchManager((Cache<String, Object>) commentCache);
    QueryBuilder queryBuilder = sm.buildQueryBuilderForClass(Comment.class).get();

    Query q = queryBuilder.keyword().onField("songId").matching(songId).createQuery();
    logger.debug("Lucene query: " + q);

    CacheQuery cq = sm.getQuery(q, Comment.class);

    for (Object o : cq.list()) {
      if (o instanceof Comment) {
        comments.add(((Comment) o));
      }
    }
    return comments;
  }
  public CapedwarfLogService() {
    String appId = Application.getAppId();
    store =
        InfinispanUtils.<String, CapedwarfLogElement>getCache(appId, CacheName.LOGS)
            .getAdvancedCache()
            .withFlags(Flag.IGNORE_RETURN_VALUES);
    this.searchManager = Search.getSearchManager(store);

    final Compatibility instance = Compatibility.getRawInstance();
    if (instance != null) {
      logToFile = instance.getValue(Compatibility.Feature.LOG_TO_FILE);
      ignoreLogging = instance.isEnabled(Compatibility.Feature.IGNORE_LOGGING);
      if (instance.isEnabled(Compatibility.Feature.ASYNC_LOGGING)) {
        logWriter = new AsyncLogWriter();
      } else {
        logWriter = new SyncLogWriter();
      }
    } else {
      logToFile = null;
      ignoreLogging = true;
      logWriter = NoopLogWriter.INSTANCE;
    }
  }
  @Test
  public void testReplaceSimpleSearchable() {
    TestEntity se1 =
        new TestEntity("ISPN-1949", "Allow non-indexed values in indexed caches", 10, "note");
    cache.put(se1.getId(), se1);
    cache.put("name2", "some string value");
    cache.put("name3", "some string value");
    cache.put("name3", new NotIndexedType("some string value"));

    SearchManager qf = Search.getSearchManager(cache);

    Query ispnIssueQuery =
        qf.buildQueryBuilderForClass(TestEntity.class)
            .get()
            .keyword()
            .onField("name")
            .ignoreAnalyzer()
            .matching("ISPN-1949")
            .createQuery();

    assertEquals(1, qf.getQuery(ispnIssueQuery).list().size());

    cache.put(se1.getId(), "some string value");

    assertEquals(0, qf.getQuery(ispnIssueQuery).list().size());

    AnotherTestEntity indexBEntity = new AnotherTestEntity("ISPN-1949");
    cache.put("name", indexBEntity);
    assertEquals(1, qf.getQuery(ispnIssueQuery).list().size());

    TestEntity se2 =
        new TestEntity(
            "HSEARCH-1077",
            "Mutable SearchFactory should return which classes are actually going to be indexed",
            10,
            "note");
    cache.replace("name", indexBEntity, se2);
    assertEquals(0, qf.getQuery(ispnIssueQuery).list().size());

    Query searchIssueQuery =
        qf.buildQueryBuilderForClass(TestEntity.class)
            .get()
            .keyword()
            .onField("name")
            .ignoreAnalyzer()
            .matching("HSEARCH-1077")
            .createQuery();
    assertEquals(1, qf.getQuery(searchIssueQuery).list().size());

    // a failing atomic replace should not change the index:
    cache.replace("name", "notMatching", "notImportant");
    assertEquals(1, qf.getQuery(searchIssueQuery).list().size());
    assertEquals(0, qf.getQuery(ispnIssueQuery).list().size());

    cache.remove("name");
    assertEquals(0, qf.getQuery(searchIssueQuery).list().size());

    cache.put("name", se2);
    assertEquals(1, qf.getQuery(searchIssueQuery).list().size());
    cache.put("name", "replacement String");
    assertEquals(0, qf.getQuery(searchIssueQuery).list().size());

    cache.put("name", se1);
    assertEquals(1, qf.getQuery(ispnIssueQuery).list().size());
    cache.put("second name", se1);
    assertEquals(2, qf.getQuery(ispnIssueQuery).list().size());
    assertEquals(2, qf.getQuery(ispnIssueQuery, TestEntity.class).list().size());

    // now actually replace one with a different indexed type (matches same query)
    cache.replace("name", se1, indexBEntity);
    assertEquals(2, qf.getQuery(ispnIssueQuery).list().size());
    assertEquals(1, qf.getQuery(ispnIssueQuery, TestEntity.class).list().size());
    assertEquals(1, qf.getQuery(ispnIssueQuery, AnotherTestEntity.class).list().size());

    // replace with a non indexed type
    cache.replace("name", indexBEntity, new NotIndexedType("this is not indexed"));
    assertEquals(1, qf.getQuery(ispnIssueQuery).list().size());
  }
Ejemplo n.º 28
0
 protected int countIndex() {
   SearchManager searchManager = Search.getSearchManager(cache1);
   CacheQuery q = searchManager.getQuery(new MatchAllDocsQuery(), Transaction.class);
   return q.getResultSize();
 }
Ejemplo n.º 29
0
  /** Execute Lucene index query. */
  private QueryResponse executeQuery(
      AdvancedCache<byte[], byte[]> cache, SerializationContext serCtx, QueryRequest request) {
    final SearchManager searchManager = Search.getSearchManager(cache);
    final SearchIntegrator searchFactory = searchManager.getSearchFactory();
    final QueryCache queryCache = ComponentRegistryUtils.getQueryCache(cache); // optional component

    LuceneQueryParsingResult parsingResult;
    Query luceneQuery;

    if (queryCache != null) {
      KeyValuePair<String, Class> queryCacheKey =
          new KeyValuePair<String, Class>(request.getJpqlString(), LuceneQueryParsingResult.class);
      parsingResult = queryCache.get(queryCacheKey);
      if (parsingResult == null) {
        parsingResult = parseQuery(cache, serCtx, request.getJpqlString(), searchFactory);
        queryCache.put(queryCacheKey, parsingResult);
      }
    } else {
      parsingResult = parseQuery(cache, serCtx, request.getJpqlString(), searchFactory);
    }

    luceneQuery = parsingResult.getQuery();

    if (!cache.getCacheConfiguration().compatibility().enabled()) {
      // restrict on entity type
      QueryBuilder qb =
          searchFactory.buildQueryBuilder().forEntity(parsingResult.getTargetEntity()).get();
      luceneQuery =
          qb.bool()
              .must(
                  qb.keyword()
                      .onField(TYPE_FIELD_NAME)
                      .ignoreFieldBridge()
                      .ignoreAnalyzer()
                      .matching(parsingResult.getTargetEntityName())
                      .createQuery())
              .must(luceneQuery)
              .createQuery();
    }

    CacheQuery cacheQuery = searchManager.getQuery(luceneQuery, parsingResult.getTargetEntity());

    if (parsingResult.getSort() != null) {
      cacheQuery = cacheQuery.sort(parsingResult.getSort());
    }

    int projSize = 0;
    if (parsingResult.getProjections() != null && !parsingResult.getProjections().isEmpty()) {
      projSize = parsingResult.getProjections().size();
      cacheQuery =
          cacheQuery.projection(parsingResult.getProjections().toArray(new String[projSize]));
    }
    if (request.getStartOffset() > 0) {
      cacheQuery = cacheQuery.firstResult((int) request.getStartOffset());
    }
    if (request.getMaxResults() > 0) {
      cacheQuery = cacheQuery.maxResults(request.getMaxResults());
    }

    List<?> list = cacheQuery.list();
    List<WrappedMessage> results =
        new ArrayList<WrappedMessage>(projSize == 0 ? list.size() : list.size() * projSize);
    for (Object o : list) {
      if (projSize == 0) {
        results.add(new WrappedMessage(o));
      } else {
        Object[] row = (Object[]) o;
        for (int j = 0; j < projSize; j++) {
          results.add(new WrappedMessage(row[j]));
        }
      }
    }

    QueryResponse response = new QueryResponse();
    response.setTotalResults(cacheQuery.getResultSize());
    response.setNumResults(list.size());
    response.setProjectionSize(projSize);
    response.setResults(results);

    return response;
  }
Ejemplo n.º 30
0
 protected void clearIndex() {
   SearchManager searchManager = Search.getSearchManager(cache1);
   searchManager.purge(Transaction.class);
 }