Package com.gentics.cr.lucene.facets.taxonomy.taxonomyaccessor

Examples of com.gentics.cr.lucene.facets.taxonomy.taxonomyaccessor.TaxonomyAccessor


    fillBoostValues(config.getString(BOOSTED_ATTRIBUTES_KEY));

    IndexAccessor indexAccessor = null;
    IndexWriter indexWriter = null;
    IndexReader indexReader = null;
    TaxonomyAccessor taxonomyAccessor = null;
    TaxonomyWriter taxonomyWriter = null;
    LuceneIndexUpdateChecker luceneIndexUpdateChecker = null;
    boolean finishedIndexJobSuccessfull = false;
    boolean finishedIndexJobWithError = false;

    try {
      indexLocation.checkLock();
      Collection<CRResolvableBean> slice = null;
      try {
        status.setCurrentStatusString("Writer accquired. Starting" + "index job.");

        if (rp == null) {
          throw new CRException("FATAL ERROR", "RequestProcessor not available");
        }

        String bsString = (String) config.get(BATCH_SIZE_KEY);

        int crBatchSize = batchSize;

        if (bsString != null) {
          try {
            crBatchSize = Integer.parseInt(bsString);
          } catch (NumberFormatException e) {
            log.error("The configured " + BATCH_SIZE_KEY + " for the Current CR" + " did not contain a parsable integer. ", e);
          }
        }

        // and get the current rule
        String rule = (String) config.get(RULE_KEY);

        if (rule == null) {
          rule = "";
        }
        if (rule.length() == 0) {
          rule = "(1 == 1)";
        } else {
          rule = "(" + rule + ")";
        }

        List<ContentTransformer> transformerlist = ContentTransformer.getTransformerList(config);

        boolean create = true;

        if (indexLocation.isContainingIndex()) {
          create = false;
          log.debug("Index already exists.");
        }
        if (indexLocation instanceof LuceneIndexLocation) {
          luceneIndexUpdateChecker = new LuceneIndexUpdateChecker((LuceneIndexLocation) indexLocation, CR_FIELD_KEY, crid,
              idAttribute);
        } else {
          log.error("IndexLocation is not created for Lucene. " + "Using the " + CRLuceneIndexJob.class.getName()
              + " requires that you use the " + LuceneIndexLocation.class.getName()
              + ". You can configure another Job by setting the " + IndexLocation.UPDATEJOBCLASS_KEY + " key in your config.");
          throw new CRException(new CRError("Error", "IndexLocation is not created for Lucene."));
        }
        Collection<CRResolvableBean> objectsToIndex = null;
        //Clear Index and remove stale Documents
        //if (!create) {
        log.debug("Will do differential index.");
        try {
          CRRequest req = new CRRequest();
          req.setRequestFilter(rule);
          req.set(CR_FIELD_KEY, crid);
          status.setCurrentStatusString("Get objects to update " + "in the index ...");
          objectsToIndex = getObjectsToUpdate(req, rp, false, luceneIndexUpdateChecker);
        } catch (Exception e) {
          log.error("ERROR while cleaning index", e);
        }
        //}
        //Obtain accessor and writer after clean
        if (indexLocation instanceof LuceneIndexLocation) {
          indexAccessor = ((LuceneIndexLocation) indexLocation).getAccessor();
          indexWriter = indexAccessor.getWriter();
          indexReader = indexAccessor.getReader(false);
          useFacets = ((LuceneIndexLocation) indexLocation).useFacets();
          if (useFacets) {
            taxonomyAccessor = ((LuceneIndexLocation) indexLocation).getTaxonomyAccessor();
            taxonomyWriter = taxonomyAccessor.getTaxonomyWriter();
          }
        } else {
          log.error("IndexLocation is not created for Lucene. " + "Using the " + CRLuceneIndexJob.class.getName()
              + " requires that you use the " + LuceneIndexLocation.class.getName()
              + ". You can configure another Job by setting the " + IndexLocation.UPDATEJOBCLASS_KEY + " key in your config.");
          throw new CRException(new CRError("Error", "IndexLocation is not created for Lucene."));
        }
        log.debug("Using rule: " + rule);
        // prepare the map of indexed/stored attributes
        Map<String, Boolean> attributes = new HashMap<String, Boolean>();
        List<String> containedAttributes = IndexerUtil.getListFromString(config.getString(CONTAINED_ATTRIBUTES_KEY), ",");
        List<String> indexedAttributes = IndexerUtil.getListFromString(config.getString(INDEXED_ATTRIBUTES_KEY), ",");
        List<String> reverseAttributes = ((LuceneIndexLocation) indexLocation).getReverseAttributes();
        // first put all indexed attributes into the map
        for (String name : indexedAttributes) {
          attributes.put(name, Boolean.FALSE);
        }

        // now put all contained attributes
        for (String name : containedAttributes) {
          attributes.put(name, Boolean.TRUE);
        }
        // finally, put the "contentid" (always contained)
        attributes.put(idAttribute, Boolean.TRUE);

        if (objectsToIndex == null) {
          log.debug("Rule returned no objects to index. Skipping...");
          return;
        }

        status.setObjectCount(objectsToIndex.size());
        log.debug(" index job with " + objectsToIndex.size() + " objects to index.");
        // now get the first batch of objects from the collection
        // (remove them from the original collection) and index them
        slice = new Vector(crBatchSize);
        int sliceCounter = 0;

        status.setCurrentStatusString("Starting to index slices.");
        boolean interrupted = Thread.currentThread().isInterrupted();
        for (Iterator<CRResolvableBean> iterator = objectsToIndex.iterator(); iterator.hasNext();) {
          CRResolvableBean obj = iterator.next();
          slice.add(obj);
          iterator.remove();
          sliceCounter++;
          if (Thread.currentThread().isInterrupted()) {
            interrupted = true;
            break;
          }
          if (sliceCounter == crBatchSize) {
            // index the current slice
            log.debug("Indexing slice with " + slice.size() + " objects.");
            indexSlice(
              crid,
              indexWriter,
              indexReader,
              slice,
              attributes,
              rp,
              create,
              config,
              transformerlist,
              reverseAttributes,
              taxonomyWriter,
              taxonomyAccessor);
            // clear the slice and reset the counter
            slice.clear();
            sliceCounter = 0;
          }
        }

        if (!slice.isEmpty()) {
          // index the last slice
          indexSlice(
            crid,
            indexWriter,
            indexReader,
            slice,
            attributes,
            rp,
            create,
            config,
            transformerlist,
            reverseAttributes,
            taxonomyWriter,
            taxonomyAccessor);
        }
        if (!interrupted) {
          // Only Optimize the Index if the thread
          // has not been interrupted
          if (optimize) {
            log.debug("Executing optimize command.");
            UseCase uc = MonitorFactory.startUseCase("optimize(" + crid + ")");
            try {
              indexWriter.optimize();
            } finally {
              uc.stop();
            }
          } else if (maxSegmentsString != null) {
            log.debug("Executing optimize command with max" + " segments: " + maxSegmentsString);
            int maxs = Integer.parseInt(maxSegmentsString);
            UseCase uc = MonitorFactory.startUseCase("optimize(" + crid + ")");
            try {
              indexWriter.optimize(maxs);
            } finally {
              uc.stop();
            }
          }
        } else {
          log.debug("Job has been interrupted and will now be closed." + " Missing objects " + "will be reindexed next run.");
        }
        finishedIndexJobSuccessfull = true;
      } catch (Exception ex) {
        log.error("Could not complete index run... indexed Objects: " + status.getObjectsDone()
            + ", trying to close index and remove lock.", ex);
        finishedIndexJobWithError = true;
        status.setError("Could not complete index run... indexed " + "Objects: " + status.getObjectsDone()
            + ", trying to close index and remove lock.");
      } finally {
        if (!finishedIndexJobSuccessfull && !finishedIndexJobWithError) {
          log.fatal("There seems to be a run time exception from this" + " index job.\nLast slice was: " + slice);
        }
        //Set status for job if it was not locked
        status.setCurrentStatusString("Finished job.");
        int objectCount = status.getObjectsDone();
        log.debug("Indexed " + objectCount + " objects...");

        if (taxonomyAccessor != null && taxonomyWriter != null) {
          taxonomyAccessor.release(taxonomyWriter);
        }

        if (indexAccessor != null && indexWriter != null) {
          indexAccessor.release(indexWriter);
        }
View Full Code Here


    if (indexLocation instanceof LuceneIndexLocation) {
      log.debug("Starting to clear index.");
      LuceneIndexLocation luceneIndexLoccation = (LuceneIndexLocation) indexLocation;
      IndexAccessor ia = luceneIndexLoccation.getAccessor();
      IndexWriter writer = null;
      TaxonomyAccessor ta = null;
      if (luceneIndexLoccation.useFacets()) {
        ta = luceneIndexLoccation.getTaxonomyAccessor();
      }
      try {
        writer = ia.getWriter();
        writer.deleteAll();
        if (ta != null) {
          ta.clearTaxonomy();
        }
        luceneIndexLoccation.resetIndexJobCreationTimes();
      } catch (IOException e) {
        log.error("Could not clear index", e);
      } finally {
        ia.release(writer);
        if (ta != null) {
          ta.refresh();
        }
      }
      log.debug("Finished clearing index.");
    } else {
      log.error("Index does not seem to be a Lucene index. Therfore no " + "clearing will be done.");
View Full Code Here

   *
   * @return taxonomy accessor for this index, null when facets are not activated
   */
  public final TaxonomyAccessor getTaxonomyAccessor() {
    if (useFacets()) {
      TaxonomyAccessor accessor = getTaxonomyAccessorInstance();
      return accessor;
    } else {
      return null;
    }
  }
View Full Code Here

    if (indexAccessor == null) {
      log.error("IndexAccessor is null. Search will not work.");
    }

    // Resources needed for faceted search
    TaxonomyAccessor taAccessor = null;
    TaxonomyReader taReader = null;
    IndexReader facetsIndexReader = null;
    IndexReader uniqueMimeTypesIndexReader = null;
   
    List<String> uniqueMimeTypes = null;
    if (retrieveUniqueMimeTypes) {
      // retrieve all possible file types
      uniqueMimeTypesIndexReader = indexAccessor.getReader(false);
      final TermEnum termEnum = uniqueMimeTypesIndexReader.terms(new Term(LUCENE_INDEX_MIMETYPE, ""));
      uniqueMimeTypes = new ArrayList<String>();
      while (termEnum.next() && termEnum.term().field().equals(LUCENE_INDEX_MIMETYPE)) {
        uniqueMimeTypes.add(termEnum.term().text());
      }
    }

    // get accessors and reader only if facets are activated
    if (facetsSearch.useFacets()) {
      facetsIndexReader = indexAccessor.getReader(false);
      taAccessor = idsLocation.getTaxonomyAccessor();
      taReader = taAccessor.getTaxonomyReader();
    }

    searcher = indexAccessor.getPrioritizedSearcher();
    Object userPermissionsObject = request.get(CRRequest.PERMISSIONS_KEY);
    String[] userPermissions = new String[0];
    if (userPermissionsObject instanceof String[]) {
      userPermissions = (String[]) userPermissionsObject;
    }
    TopDocsCollector<?> collector = createCollector(searcher, hits, sorting, computescores, userPermissions);
    HashMap<String, Object> result = null;
    try {
      analyzer = LuceneAnalyzerFactory.createAnalyzer(config);

      if (searchedAttributes != null && searchedAttributes.length > 0 && query != null && !query.equals("")) {
        QueryParser parser = CRQueryParserFactory.getConfiguredParser(searchedAttributes, analyzer, request, config);

        Query parsedQuery = parser.parse(query);
        // GENERATE A NATIVE QUERY

        parsedQuery = searcher.rewrite(parsedQuery);

        result = new HashMap<String, Object>(3);
        result.put(RESULT_QUERY_KEY, parsedQuery);

        // when facets are active create a FacetsCollector
        FacetsCollector facetsCollector = null;
        if (facetsSearch.useFacets()) {
          facetsCollector = facetsSearch.createFacetsCollector(facetsIndexReader, taAccessor, taReader);
        }

        Map<String, Object> ret = executeSearcher(collector, searcher, parsedQuery, explain, count, start, facetsCollector);
        if (log.isDebugEnabled()) {
          for (Object res : ret.values()) {
            if (res instanceof LinkedHashMap) {
              LinkedHashMap<Document, Float> documents = (LinkedHashMap<Document, Float>) res;
              if (documents != null) {
                for (Entry doc : documents.entrySet()) {
                  Document doCument = (Document) doc.getKey();
                  log.debug("CRSearcher.search: "
                  /* + doCument.toString() */
                  + " Contentid: " + doCument.get("contentid"));
                }
              }
            }
          }
        }
        if (ret != null) {
          LinkedHashMap<Document, Float> coll = (LinkedHashMap<Document, Float>) ret.get(RESULT_RESULT_KEY);
          float maxScore = (Float) ret.get(RESULT_MAXSCORE_KEY);
          result.put(RESULT_RESULT_KEY, coll);
          int totalhits = collector.getTotalHits();

          result.put(RESULT_HITS_KEY, totalhits);
          result.put(RESULT_MAXSCORE_KEY, maxScore);

          if (retrieveUniqueMimeTypes) {
            // add unique extensions
            result.put(RESULT_UNIQUE_MIMETYPES_KEY, uniqueMimeTypes);
          }
          if (retrieveCollector) {
            result.put(RESULT_COLLECTOR_KEY, ret.get(RESULT_COLLECTOR_KEY));
          }

          // PLUG IN DIDYOUMEAN
          boolean didyoumeanEnabledForRequest = StringUtils.getBoolean(request.get(DIDYOUMEAN_ENABLED_KEY), true);
          if (start == 0 && didyoumeanenabled && didyoumeanEnabledForRequest
              && (totalhits <= didyoumeanactivatelimit || didyoumeanactivatelimit == -1 || maxScore < didyoumeanminscore)) {

            HashMap<String, Object> didyoumeanResult = didyoumean(
              query,
              parsedQuery,
              indexAccessor,
              parser,
              searcher,
              sorting,
              userPermissions);
            result.putAll(didyoumeanResult);
          }

          // PLUG IN DIDYOUMEAN END

          // if a facetsCollector was created, store the faceted search results in the meta resolveable
          if (facetsCollector != null) {
            result.put(FacetsSearchConfigKeys.RESULT_FACETS_LIST_KEY, facetsSearch.getFacetsResults(facetsCollector));
          }

          int size = 0;
          if (coll != null) {
            size = coll.size();
          }
          log.debug("Fetched " + size + " objects with query: " + query);
        } else {
          result = null;
        }
      }
    } catch (Exception e) {
      if (config.getBoolean("FAILONMAXCLAUSES")) {
        log.debug("Error getting the results.", e);
        throw new CRException(e);
      } else {
        log.error("Error getting the results.", e);
        result = null;
      }

    } finally {
       /*
        * if facets are activated cleanup the resources
        * needed for faceted search
        *
        * Always cleanup/release the taxonomy Reader/Writer
        * before the Reader/Writers of the main index!
        */
      if (taAccessor != null && taReader != null) {
        taAccessor.release(taReader);
      }
      if (facetsIndexReader != null) {
        indexAccessor.release(facetsIndexReader, false);
      }
      if (uniqueMimeTypesIndexReader != null) {
View Full Code Here

  }

  @Override
  protected TaxonomyAccessor getTaxonomyAccessorInstance() {
    Directory directory = this.getTaxonomyDirectory();
    TaxonomyAccessor accessor = TaxonomyAccessorFactory.getInstance().getAccessor(directory);
    return accessor;
  }
View Full Code Here

TOP

Related Classes of com.gentics.cr.lucene.facets.taxonomy.taxonomyaccessor.TaxonomyAccessor

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.