Package org.fao.geonet.kernel.search

Examples of org.fao.geonet.kernel.search.MetaSearcher


    Element       params = buildParams();
    ServiceConfig config = new ServiceConfig();

    SearchManager searchMan = gc.getBean(SearchManager.class);
    MetaSearcher  searcher  = searchMan.newSearcher(SearchManager.LUCENE, Geonet.File.SEARCH_LUCENE);

    searcher.search(context, params, config);

    params.addContent(new Element("from").setText("1"));
    params.addContent(new Element("to").setText(searcher.getSize() +""));

    Element result = searcher.present(context, params, config);

    searcher.close();

    return result;
  }
View Full Code Here


  public static List<Integer> search(ServiceContext context, Element params) throws Exception
  {
    GeonetContext gc = (GeonetContext) context.getHandlerContext(Geonet.CONTEXT_NAME);
    SearchManager sm = gc.getBean(SearchManager.class);

    MetaSearcher searcher = sm.newSearcher(SearchManager.LUCENE, Geonet.File.SEARCH_LUCENE);

        if(context.isDebugEnabled()) context.debug("Searching with params:\n"+ Xml.getString(params));

    searcher.search(context, params, dummyConfig);

    params.addContent(new Element("fast").setText("true"));
    params.addContent(new Element("from").setText("1"));
    params.addContent(new Element("to").setText(searcher.getSize() +""));

    context.info("Records found : "+ searcher.getSize());

    Element records = searcher.present(context, params, dummyConfig);

    records.getChild("summary").detach();

    List<Integer> result = new ArrayList<Integer>();

    for (Object o : records.getChildren())
    {
      Element rec  = (Element) o;
      Element info = rec.getChild("info", Edit.NAMESPACE);

      result.add(Integer.parseInt(info.getChildText("id")));
    }

    searcher.close();

    return result;
  }
View Full Code Here

        if (serverResults.locallyRemoved > 0) {
            dataMan.flush();
        }

    // --- Search remote node
    MetaSearcher s = searchMan.newSearcher(SearchManager.Z3950, Geonet.File.SEARCH_Z3950_CLIENT);

    ServiceConfig config = new ServiceConfig();

    Element request = new Element("request");

    // --- Z39.50 servers from harvest params
    for (String id : params.getRepositories()) {
      request.addContent(new Element(Geonet.SearchResult.SERVERS).setText(id));
    }

    // --- Z39.50 query from harvest params
    request.addContent(new Element(Geonet.SearchResult.ZQUERY)
        .setText(params.query));

    // --- don't get html presentations (get them later)
    request.addContent(new Element(Geonet.SearchResult.SERVERHTML)
        .setText("off"))

    // --- set timeout to be 100 seconds
    request.addContent(new Element(Geonet.SearchResult.TIMEOUT).setText("100"))
    // --- set hitsPerPage
    request.addContent(new Element(Geonet.SearchResult.HITS_PER_PAGE).setText(groupSize+""))

    // --- do the search
    s.search(context, request, config);

    if (s.getSize() == 0) {
      log.error("Search failed or returned 0 results, trying again");
      s.search(context, request, config);
      if (s.getSize() == 0) {
        throw new Exception("Bad luck, Search failed or returned 0 results");
      }
    }

        if(log.isDebugEnabled()) log.debug("Search returned "+s.getSize()+" hits");

    // -- process the hits in groups of groupSize
    int numberOfHits = Math.min(Integer.parseInt(params.maximumHits),s.getSize());
    // -- add from and to placeholders to request

    request.addContent(new Element("from"));
    request.addContent(new Element("to"));

    Element repositories = new Info().getZRepositories(context, settingMan);
        if(log.isDebugEnabled()) {
            log.debug("repos "+Xml.getString(repositories));
        }

    // -- build a map of collection code versus repository name for
    // -- assigning the categories
    Map <String,String> codes = new HashMap<String,String>();
    Map <String,String> catCodes = new HashMap<String,String>();

        final MetadataCategoryRepository categoryRepository = this.context.getBean(MetadataCategoryRepository.class);
        // -- add new category for each repository
    boolean addcateg = false;
    for (String repo : params.getRepositories()) {
      Element repoElem = Xml.selectElement(repositories, "record[id='"+repo+"']");
      if (repoElem != null) {
        Element repoId  = repoElem.getChild("id");
        String repoName = repoElem.getChildText("name");
        codes.put(repoId.getAttributeValue("serverCode")+":"+repoId.getAttributeValue("code"), repoName);
        // create a result holder for this repository
        serverResults.getServerResult(repoName);

        // sanitize the name of the category
        String categName = repoName.replaceAll("[^\\w]","");
        categName = categName.toLowerCase();
        catCodes.put(repoId.getAttributeValue("serverCode")+":"+repoId.getAttributeValue("code"), categName);

        if (categoryRepository.findOneByNameIgnoreCase(categName) == null) {
                    MetadataCategory category = new MetadataCategory();
                    category.setName(categName);
                    categoryRepository.save(category);
          addcateg = true;
        }
      }
    }

    if (addcateg) {
            categoryRepository.flush();
        }

    // --- return only maximum hits as directed by the harvest params
    int nrGroups = (numberOfHits / groupSize) + 1;
    for (int i = 1; i <= nrGroups; i++) {
      int lower = ((i-1)*groupSize)+1
      int upper = Math.min((i*groupSize),numberOfHits);
      request.getChild("from").setText(""+lower)
      request.getChild("to").setText(""+upper)

      // --- Loading results
      List<Document> list = s.presentDocuments(context, request, config);

      // --- Loading categories and groups
      localCateg = new CategoryMapper(context);
      localGroups = new GroupMapper(context);

View Full Code Here

        SearchManager searchMan = gc.getBean(SearchManager.class);

        // perform the search
        if (Log.isDebugEnabled(Geonet.SEARCH_ENGINE))
            Log.debug(Geonet.SEARCH_ENGINE, "Searching for: " + type);
        MetaSearcher searcher = searchMan.newSearcher(SearchManager.LUCENE, Geonet.File.SEARCH_LUCENE);

        try {
            // Creating parameters for search, fast only to retrieve uuid
            Element parameters = new Element(Jeeves.Elem.REQUEST);
            if ("children".equals(type))
                parameters.addContent(new Element("parentUuid").setText(uuid));
            else if ("services".equals(type))
                parameters.addContent(new Element("operatesOn").setText(uuid));
            else if ("hasfeaturecat".equals(type))
                parameters.addContent(new Element("hasfeaturecat").setText(uuid));
            else if ("hassource".equals(type))
                parameters.addContent(new Element("hassource").setText(uuid));
            else if ("associated".equals(type))
                parameters.addContent(new Element("agg_associated").setText(uuid));
            else if ("datasets".equals(type) || "fcats".equals(type) ||
                    "sources".equals(type) || "siblings".equals(type) ||
                    "parent".equals(type))
                parameters.addContent(new Element("uuid").setText(uuid));

            parameters.addContent(new Element("fast").addContent("index"));
            parameters.addContent(new Element("sortBy").addContent("title"));
            parameters.addContent(new Element("sortOrder").addContent("reverse"));
            parameters.addContent(new Element("from").addContent(from));
            parameters.addContent(new Element("to").addContent(to));

            searcher.search(context, parameters, _config);

            Element response = new Element(type);
            Element relatedElement = searcher.present(context, parameters, _config);
            response.addContent(relatedElement);
            return response;
        } finally {
            searcher.close();
        }
    }
View Full Code Here

  {
    if (System.currentTimeMillis() > _lastUpdateTime + _timeBetweenUpdates)
    {
      GeonetContext gc = (GeonetContext) context.getHandlerContext(Geonet.CONTEXT_NAME);
      SearchManager searchMan = gc.getBean(SearchManager.class);
      MetaSearcher  searcher  = searchMan.newSearcher(SearchManager.LUCENE, Geonet.File.SEARCH_LUCENE);

      try {
        // FIXME: featured should be at metadata level, not at group level
        Element searchRequest = new Element("request");
        searchRequest.addContent(new Element(Geonet.SearchResult.BUILD_SUMMARY).setText("false"));
        searchRequest.addContent(new Element("featured").setText("true"));
        searchRequest.addContent(new Element("relation").setText(_relation));
        searchRequest.addContent(new Element("northBL").setText(_northBL));
        searchRequest.addContent(new Element("southBL").setText(_southBL));
        searchRequest.addContent(new Element("eastBL").setText(_eastBL));
        searchRequest.addContent(new Element("westBL").setText(_westBL));
 
                if(Log.isDebugEnabled(Geonet.SEARCH_ENGINE))
            Log.debug(Geonet.SEARCH_ENGINE, "RANDOM SEARCH CRITERIA:\n"+ Xml.getString(searchRequest));
       
        searcher.search(context, searchRequest, _config);
 
        Element presentRequest = new Element("request");
        presentRequest.addContent(new Element("fast").setText("true"));
        presentRequest.addContent(new Element("from").setText("1"));
        presentRequest.addContent(new Element("to").setText(searcher.getSize()+""));
             
        @SuppressWarnings("unchecked")
                List<Element> results = searcher.present(context, presentRequest, _config).getChildren();
 
        _response = new Element("response");
        for (int i = 0; i < _maxItems && results.size() > 1; i++) {
          Random rnd = new Random();
          int r = rnd.nextInt(results.size() - 1) + 1// skip summary
 
          Element mdInfo = (Element)results.remove(r);
          mdInfo.detach();
 
          Element info = mdInfo.getChild("info", Edit.NAMESPACE);
          String id = info.getChildText("id");
                    boolean forEditing = false, withValidationErrors = false, keepXlinkAttributes = false;
                    Element md = gc.getBean(DataManager.class).getMetadata(context, id, forEditing, withValidationErrors, keepXlinkAttributes);
          _response.addContent(md);
        }
        _lastUpdateTime = System.currentTimeMillis();
      }
            finally {
        searcher.close();
      }
    }
    return (Element)_response.clone();
  }
View Full Code Here

      _response = new Element(Jeeves.Elem.RESPONSE);

      // perform the search and return the results read from the index
      Log.info(Geonet.SEARCH_ENGINE, "Creating latest updates searcher");
      MetaSearcher searcher = searchMan.newSearcher(SearchManager.LUCENE, Geonet.File.SEARCH_LUCENE);
      searcher.search(context, _request, _config);
      Map<Integer,Metadata> allMdInfo = ((LuceneSearcher)searcher).getAllMdInfo(context, _maxItems);
      for (Integer id : allMdInfo.keySet()) {
        try {
          boolean forEditing = false;
          boolean withValidationErrors = false;
          boolean keepXlinkAttributes = false;
          Element md = dataMan.getMetadata(context, id+"", forEditing, withValidationErrors, keepXlinkAttributes);
          _response.addContent(md);
        } catch (Exception e) {
          Log.error(Geonet.SEARCH_ENGINE, "Exception in latest update searcher "+e.getMessage());
          e.printStackTrace();
        }
      }
      searcher.close();

      _lastUpdateTime = System.currentTimeMillis();
    }

    return (Element)_response.clone();
View Full Code Here

            Log.debug(Geonet.MEF, "Creating searcher to run request: " + Xml.getString(request));

    GeonetContext gc = (GeonetContext) context
        .getHandlerContext(Geonet.CONTEXT_NAME);
    SearchManager searchMan = gc.getBean(SearchManager.class);
    MetaSearcher searcher = searchMan.newSearcher(SearchManager.LUCENE,
        Geonet.File.SEARCH_LUCENE);

    Set<String> uuids = new HashSet<String>();

    // perform the search
    searcher.search(context, request, _config);

    // If element type found, then get their uuid
    if (searcher.getSize() != 0) {
            if(Log.isDebugEnabled(Geonet.MEF))
                Log.debug(Geonet.MEF, "  Exporting record(s) found for metadata: " + uuid);
      Element elt = searcher.present(context, request, _config);

      // Get ISO records only
      @SuppressWarnings("unchecked")
            List<Element> isoElt = elt.getChildren();
      for (Element md : isoElt) {
        // -- Only metadata record should be processed
        if (!md.getName().equals("summary")) {
          String mdUuid = md.getChild(Edit.RootChild.INFO,
              Edit.NAMESPACE).getChildText(Edit.Info.Elem.UUID);
                    if(Log.isDebugEnabled(Geonet.MEF)) Log.debug(Geonet.MEF, "    Adding: " + mdUuid);
          uuids.add(mdUuid);
        }
      }
    }
    searcher.close();
    Log.info(Geonet.MEF, "  Found " + uuids.size() + " record(s).");

    return uuids;
  }
View Full Code Here

      params.addContent(new Element(Geonet.SearchResult.UUID).setText(uuids));

    }
   
    // perform the search and save search result into session
    MetaSearcher searcher;

    context.info("Creating searchers");

    searcher = searchMan.newSearcher(SearchManager.LUCENE, Geonet.File.SEARCH_LUCENE);

    searcher.search(context, params, _config);

    session.setProperty(Geonet.Session.SEARCH_RESULT, searcher);

    context.info("Getting summary");
   
    Element summary = searcher.getSummary();
    summary.addContent(new Element(Geonet.SearchResult.RESTORELASTSEARCH).setText(restoreLastSearch));

    return summary; 
   
  }
View Full Code Here

      oldSelection.close();
    }
   

    // perform the search and save search query into session
    MetaSearcher searcher;

    context.info("Creating searchers");

    if (remotesearcher = searchMan.newSearcher(SearchManager.Z3950,  Geonet.File.SEARCH_Z3950_CLIENT);
    else        searcher = searchMan.newSearcher(SearchManager.LUCENE, Geonet.File.SEARCH_LUCENE);

    searcher.search(context, elData, _config);
    if (remote && (searcher.getSize() == 0)) { // do it again for Z3950
      searcher.search(context, elData, _config);
    }
    session.setProperty(Geonet.Session.SEARCH_RESULT, searcher);
    session.removeProperty(Geonet.Session.SEARCH_REQUEST);
    context.info("Getting summary");

    return searcher.getSummary();
  }
View Full Code Here

    // possibly close old searcher
    UserSession  session     = context.getUserSession();
   
    // perform the search and save search result into session
    MetaSearcher searcher;
    context.info("Creating searchers");
   
        if(remote) {
      searcher = searchMan.newSearcher(SearchManager.Z3950,  Geonet.File.SEARCH_Z3950_CLIENT);
        } else {
      searcher = searchMan.newSearcher(SearchManager.LUCENE, Geonet.File.SEARCH_LUCENE);
        }
   
    try {
     
      // Check is user asked for summary only without building summary
      String summaryOnly = Util.getParam(params, Geonet.SearchResult.SUMMARY_ONLY, "0");
      String sBuildSummary = params.getChildText(Geonet.SearchResult.BUILD_SUMMARY);
            if(sBuildSummary != null && sBuildSummary.equals("false") && ! "0".equals(summaryOnly)) {
        elData.getChild(Geonet.SearchResult.BUILD_SUMMARY).setText("true");
            }
     
      session.setProperty(Geonet.Session.SEARCH_REQUEST, elData.clone());
      searcher.search(context, elData, _config);
 
      if (!"0".equals(summaryOnly)) {
        return searcher.getSummary();
      } else {
       
        elData.addContent(new Element(Geonet.SearchResult.FAST).setText("true"));
        elData.addContent(new Element("from").setText("1"));
        // FIXME ? from and to parameter could be used but if not
        // set, the service return the whole range of results
        // which could be huge in non fast mode ?
        elData.addContent(new Element("to").setText(searcher.getSize() +""));
   
        Element result = searcher.present(context, elData, _config);
       
        // Update result elements to present
        SelectionManager.updateMDResult(context.getUserSession(), result);
   
        return result;
      }
    } finally {
      searcher.close();
    }
  }
View Full Code Here

TOP

Related Classes of org.fao.geonet.kernel.search.MetaSearcher

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.