public List<DOSearchResult> getSearchResults(
      DORestSearchRequest request, SearchErrors errors, Map<String, ArrayList<String>> nerMap) {
    List<DOSearchResult> result = new ArrayList<DOSearchResult>();
    QueryParam tcqp = null;
    QueryResponse qres = null;
    try {
      SolrServer server = solrConnectionUtils.getRestSolrServer();

      tcqp = restQueryCreator.getSearchQuery(request, nerMap);
      qres = server.query(tcqp);
      if (qres != null) {
        DOSearchResult serachRes = null;
        if (request.isGrouprequest()) {
          serachRes =
              DOResponseUtils.processGroupQueryResponse(
                  qres, Constants.RESPONSE_TYPE_REST, request.isSpellcheckApplied());
        } else {
          serachRes =
              DOResponseUtils.processQueryResponse(
                  qres, Constants.RESPONSE_TYPE_REST, request.isSpellcheckApplied());
        }
        result.add(serachRes);
      }
    } catch (SolrServerException e) {
      logger.error(e.getMessage(), e);
      SearchError error = new SearchError(ErrorCode.SOLR_ERROR_CODE, e.getMessage());
      errors.add(error);
    } catch (Exception e) {
      logger.error(e.getMessage(), e);
      SearchError error = new SearchError(ErrorCode.SOLR_ERROR_CODE, e.getMessage());
      errors.add(error);
    }

    return result;
  }
  @Test
  public void testRedirect() throws Exception {
    try (HttpSolrClient client =
        new HttpSolrClient(jetty.getBaseUrl().toString() + "/redirect/foo")) {
      SolrQuery q = new SolrQuery("*:*");
      // default = false
      try {
        client.query(q);
        fail("Should have thrown an exception.");
      } catch (SolrServerException e) {
        assertTrue(e.getMessage().contains("redirect"));
      }

      client.setFollowRedirects(true);
      client.query(q);

      // And back again:
      client.setFollowRedirects(false);
      try {
        client.query(q);
        fail("Should have thrown an exception.");
      } catch (SolrServerException e) {
        assertTrue(e.getMessage().contains("redirect"));
      }
    }
  }
  private SearchEngineResponse<MediaItem> search(SolrQuery query) {

    SearchEngineResponse<MediaItem> response = new SearchEngineResponse<MediaItem>();
    QueryResponse rsp;

    try {
      rsp = server.query(query);
    } catch (SolrServerException e) {
      e.printStackTrace();
      Logger.getRootLogger().info(e.getMessage());
      return null;
    }

    List<SolrMediaItem> solrItems = rsp.getBeans(SolrMediaItem.class);

    List<MediaItem> mediaItems = new ArrayList<MediaItem>();
    for (SolrMediaItem solrMediaItem : solrItems) {
      try {
        MediaItem mediaItem = solrMediaItem.toMediaItem();
        String id = mediaItem.getId();

        mediaItem.setId(id);

        mediaItems.add(mediaItem);
      } catch (MalformedURLException ex) {
        Logger.getRootLogger().error(ex.getMessage());
      }
    }

    response.setResults(mediaItems);

    return response;
  }
Esempio n. 4
0
 public static SolrDocumentList query(SolrServer server, SolrQuery solrParams)
     throws DSpaceSolrException {
   try {
     solrParams.addSortField("item.id", ORDER.asc);
     QueryResponse response = server.query(solrParams);
     return response.getResults();
   } catch (SolrServerException ex) {
     throw new DSpaceSolrException(ex.getMessage(), ex);
   }
 }
Esempio n. 5
0
 public static SolrDocument querySingle(SolrServer server, SolrQuery solrParams)
     throws SolrSearchEmptyException {
   try {
     solrParams.addSortField("item.id", ORDER.asc);
     QueryResponse response = server.query(solrParams);
     if (response.getResults().getNumFound() > 0) return response.getResults().get(0);
     else throw new SolrSearchEmptyException();
   } catch (SolrServerException ex) {
     throw new SolrSearchEmptyException(ex.getMessage(), ex);
   }
 }
 protected void rollback(SolrServer solrServer) {
   try {
     solrServer.rollback();
   } catch (SolrServerException e) {
     LOG.info("Exception :", e);
     throw new DocstoreIndexException(e.getMessage());
   } catch (IOException e) {
     LOG.info("Exception :", e);
     throw new DocstoreIndexException(e.getMessage());
   }
 }
  @Test
  public void testTimeout() throws Exception {

    SolrQuery q = new SolrQuery("*:*");
    try (HttpSolrClient client = new HttpSolrClient(jetty.getBaseUrl().toString() + "/slow/foo")) {
      client.setSoTimeout(2000);
      client.query(q, METHOD.GET);
      fail("No exception thrown.");
    } catch (SolrServerException e) {
      assertTrue(e.getMessage().contains("Timeout"));
    }
  }
  public void forceCommitPending() {

    try {

      server.commit();
    } catch (SolrServerException ex) {
      ex.printStackTrace();
      Logger.getRootLogger().error(ex.getMessage());
    } catch (IOException ex) {
      ex.printStackTrace();
      Logger.getRootLogger().error(ex.getMessage());
    }
  }
Esempio n. 9
0
 private void index(TruckEvent truckEvent) {
   try {
     LOG.info("Starting process to solr index document[" + truckEvent + "]");
     UpdateResponse response = server.addBean(truckEvent);
     LOG.info(
         "Indexed document with id: " + truckEvent.getId() + " status: " + response.getStatus());
   } catch (IOException e) {
     LOG.error("Could not index document: " + truckEvent.getId() + " " + e.getMessage());
     e.printStackTrace();
   } catch (SolrServerException e) {
     LOG.error("Could not index document: " + truckEvent.getId() + " " + e.getMessage());
     e.printStackTrace();
   }
 }
 public SolrSearchResultSet getSearchResultSet(
     SlingHttpServletRequest request, Query query, boolean asAnon) throws SolrSearchException {
   try {
     SolrSearchResultSet rs = null;
     if (query.getType() == Type.SOLR) {
       rs = processSolrQuery(request, query, asAnon);
     } else if (query.getType() == Type.SPARSE) {
       rs = processSparseQuery(request, query, asAnon);
     }
     return rs;
   } catch (SolrServerException e) {
     LOGGER.warn(e.getMessage(), e);
     throw new SolrSearchException(500, e.getMessage());
   } catch (ParseException e) {
     LOGGER.warn(e.getMessage(), e);
     throw new SolrSearchException(500, e.getMessage());
   } catch (StorageClientException e) {
     LOGGER.warn(e.getMessage(), e);
     throw new SolrSearchException(500, e.getMessage());
   } catch (AccessDeniedException e) {
     LOGGER.warn(e.getMessage(), e);
     throw new SolrSearchException(403, e.getMessage());
   }
 }
  /**
   * *
   *
   * @param query
   * @param pageCount
   * @param rows
   * @returns the list of discussion question
   */
  public Object[] fetchDiscussionData(String query, int pageCount, int rows) {
    try {

      SolrServer server = Adder.getSolrServer(serverurlConstants.ADD_DISSCUSSION_QUESTION_URL);
      ModifiableSolrParams params = new ModifiableSolrParams();
      params.set("q", query);
      params.set("sort", "AnswerCount desc,LastRepliedDate desc,Rating desc");
      params.set("start", "" + pageCount);
      params.set("version", "2.2");
      params.set("wt", "json");
      params.set("indent", "on");
      params.set("rows", rows);

      QueryResponse response = server.query(params, SolrRequest.METHOD.POST);

      List<DiscussionQuestion> questions = new ArrayList<DiscussionQuestion>();
      for (SolrDocument updatesDoc : response.getResults()) {
        DiscussionQuestion discussionQuestion = new DiscussionQuestion();
        discussionQuestion.setID(updatesDoc.getFieldValue("ID").toString());
        discussionQuestion.setCreatedUserID(updatesDoc.getFieldValue("CreatedUserID").toString());
        discussionQuestion.setQuestionText(updatesDoc.getFieldValue("QuestionText").toString());
        discussionQuestion.setCreatedDate(updatesDoc.getFieldValue("CreatedDate").toString());
        discussionQuestion.setTags(updatesDoc.getFieldValue("Tags").toString());
        discussionQuestion.setRating(updatesDoc.getFieldValue("Rating").toString());
        discussionQuestion.setCreatedUserDisplayName(
            updatesDoc.getFieldValue("CreatedUserDisplayName").toString());
        discussionQuestion.setCreatedUserScreenName(
            updatesDoc.getFieldValue("CreatedUserScreenName").toString());
        discussionQuestion.setCategoryName(updatesDoc.getFieldValue("CategoryName").toString());
        discussionQuestion.setAnswerCount(updatesDoc.getFieldValue("AnswerCount").toString());
        discussionQuestion.setCreatedUserType(
            updatesDoc.getFieldValue("CreatedUserType").toString());
        // Date dateString = (Date) updatesDoc.getFieldValue("LastRepliedDate");
        // SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        discussionQuestion.setLastRepliedDate(
            updatesDoc.getFieldValue("LastRepliedDate").toString());
        questions.add(discussionQuestion);
      }
      Object[] resultArr = new Object[2];
      String numFound = response.getResults().getNumFound() + "";
      resultArr[0] = questions;
      resultArr[1] = numFound;

      return resultArr;
    } catch (SolrServerException e) {
      throw new DataAccessResourceFailureException(e.getMessage(), e);
    }
  }
 protected SolrDocument getSolrDocumentByUUID(String identifier) {
   SolrQuery query = new SolrQuery();
   SolrDocument solrDocument = null;
   SolrServer server = null;
   try {
     server = SolrServerManager.getInstance().getSolrServer();
     QueryResponse response = null;
     query.setQuery("id:" + identifier);
     response = server.query(query);
     solrDocument = response.getResults().get(0);
   } catch (SolrServerException e) {
     LOG.info("Exception :", e);
     throw new DocstoreIndexException(e.getMessage());
   }
   return solrDocument;
 }
Esempio n. 13
0
 @Override
 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
     throws ServletException, IOException {
   traceRequest(req);
   SolrQuery query = buildSolrQuery(req);
   if (query == null) {
     handleException(resp, "Invalid search request", null, HttpServletResponse.SC_BAD_REQUEST);
     return;
   }
   try {
     QueryResponse solrResponse = SearchActivator.getInstance().getSolrServer().query(query);
     writeResponse(query, req, resp, solrResponse);
   } catch (SolrServerException e) {
     LogHelper.log(e);
     resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
   }
 }
Esempio n. 14
0
 @Override
 public List<Item> search(final String searchQuery) {
   final SolrQuery query = new SolrQuery(searchQuery);
   query.add("wt", "json");
   QueryResponse queryResponse;
   try {
     queryResponse = solrServer.query(query);
     if (queryResponse != null) {
       final SolrDocumentList results = queryResponse.getResults();
       if (results != null) {
         return parseSearchResults(results);
       }
     }
   } catch (final SolrServerException exc) {
     LOG.error("Exception executing solr search on query {}. Message {}", query, exc.getMessage());
   }
   return Collections.<Item>emptyList();
 }
Esempio n. 15
0
 public void run() {
   while (true) {
     try {
       Thread.sleep(15000);
       server.commit();
       LOG.info("Committing Index");
     } catch (InterruptedException e) {
       LOG.error("Interrupted: " + e.getMessage());
       e.printStackTrace();
     } catch (SolrServerException e) {
       LOG.error("Error committing: " + e.getMessage());
       e.printStackTrace();
     } catch (IOException e) {
       LOG.error("Error committing: " + e.getMessage());
       e.printStackTrace();
     }
   }
 }
  @SuppressWarnings("finally")
  public boolean deleteItems(Query query) {
    boolean status = false;
    try {
      server.deleteByQuery(query.getQueryString());
      UpdateResponse response = server.commit();
      int statusId = response.getStatus();
      if (statusId == 0) {
        status = true;
      }

    } catch (SolrServerException ex) {
      Logger.getRootLogger().error(ex.getMessage());
    } catch (IOException ex) {
      Logger.getRootLogger().error(ex.getMessage());
    } finally {
      return status;
    }
  }
Esempio n. 17
0
  private static long getTotalDocCount(SolrServer server) {
    long totalDocCount = 0;

    SolrQuery query = new SolrQuery();
    query.setQuery("+text:(*:*)");
    query.addFilterQuery("+pub_date_year:[1993 TO 2013]");
    query.setParam("fl", "pmid");

    try {
      QueryResponse rsp = server.query(query);
      totalDocCount = rsp.getResults().getNumFound();
    } catch (SolrServerException e) {
      // exit out if there is an error
      log.warning(e.getMessage());
      System.exit(1);
    }

    return totalDocCount;
  }
  @SuppressWarnings("finally")
  public boolean deleteMediaItem(String mediaItemId) {
    mediaItemId.replaceFirst("::", "%%");
    boolean status = false;
    try {
      server.deleteByQuery("id:" + mediaItemId);
      UpdateResponse response = server.commit();
      int statusId = response.getStatus();
      if (statusId == 0) {
        status = true;
      }

    } catch (SolrServerException ex) {
      Logger.getRootLogger().error(ex.getMessage());
    } catch (IOException ex) {
      Logger.getRootLogger().error(ex.getMessage());
    } finally {
      return status;
    }
  }
Esempio n. 19
0
 /**
  * Returns a MODS record for a given recordIdentifier.
  *
  * @param id a recordIdentifier for a solr document
  * @return the ModsType for this recordidentifier
  * @see ModsType
  */
 public ModsType getMods(String id) throws JAXBException {
   SolrDocumentList docs;
   SolrDocument doc;
   ModsType modsType = new ModsType();
   HttpSolrServer server = null;
   try {
     server = SolrServer.getSolrConnection();
     SolrQuery query = new SolrQuery("recordIdentifier:" + id);
     QueryResponse response = server.query(query);
     docs = response.getResults();
     if (docs.size() == 0) throw new ResourceNotFoundException("Item " + id + " not found");
     else {
       doc = docs.get(0);
       modsType = getModsType(doc);
     }
   } catch (SolrServerException se) {
     se.printStackTrace();
     log.error(se.getMessage());
   }
   return modsType;
 }
Esempio n. 20
0
 // deprecated - use getModsType instead, rewrite and use this if we decide to wrap
 // the mods document in an <item> tag
 public Item getItem(String id) {
   SolrDocumentList docs;
   SolrDocument doc;
   Item item = new Item();
   HttpSolrServer server = null;
   try {
     server = SolrServer.getSolrConnection();
     SolrQuery query = new SolrQuery("recordIdentifier:" + id);
     QueryResponse response = server.query(query);
     docs = response.getResults();
     if (docs.size() == 0) item = null;
     else {
       doc = docs.get(0);
       item = setItem(doc);
     }
   } catch (SolrServerException se) {
     log.error(se.getMessage());
     se.printStackTrace();
   }
   return item;
 }
  @SuppressWarnings("finally")
  public boolean insertMediaItems(List<MediaItem> mediaItems) {

    boolean status = true;
    try {
      List<SolrMediaItem> solrMediaItems = new ArrayList<SolrMediaItem>();
      for (MediaItem mediaItem : mediaItems) {
        SolrMediaItem solrMediaItem = new SolrMediaItem(mediaItem);
        solrMediaItems.add(solrMediaItem);
      }

      server.addBeans(solrMediaItems, commitPeriod);

    } catch (SolrServerException ex) {
      Logger.getRootLogger().error(ex.getMessage());
      status = false;
    } catch (IOException ex) {
      Logger.getRootLogger().error(ex.getMessage());
      status = false;
    } finally {
      return status;
    }
  }
Esempio n. 22
0
  /**
   * Returns a SolrDocumentList for a given set of search parameters. Search parameters are parsed
   * and mapped into solr (solrj) query syntax, and solr query is performed.
   *
   * @param queryParams query parameters to map to a solr query
   * @return the SolrDocumentList for this query
   * @see SolrDocumentList
   */
  private SolrDocumentList doQuery(MultivaluedMap<String, String> queryParams) {
    SolrDocumentList docs = null;
    HttpSolrServer server = null;
    ArrayList<String> queryList = new ArrayList<String>();
    server = SolrServer.getSolrConnection();
    SolrQuery query = new SolrQuery();
    for (String key : queryParams.keySet()) {
      String value = queryParams.getFirst(key);
      System.out.println(key + " : " + queryParams.getFirst(key) + "\n");
      if (key.equals("start")) {
        int startNo = Integer.parseInt(value);
        if (startNo < 0) startNo = 0;
        query.setStart(startNo);
      } else if (key.equals("limit")) {
        limit = Integer.parseInt(value);
        query.setRows(limit);
      } else if (key.equals("sort.asc") || key.equals("sort")) query.setSort(value, ORDER.asc);
      else if (key.equals("sort.desc")) query.setSort(value, ORDER.desc);
      else if (key.startsWith("facet")) {
        query.setFacet(true);
        String[] facetArray = value.split(",");
        for (String f : facetArray) {
          query.addFacetField(f);
        }
      } else {
        if (key.endsWith("_exact")) queryList.add(key.replace("_exact", "") + ":\"" + value + "\"");
        else {
          if (value.contains(" ")) value = "( " + value.replaceAll(" ", " AND ") + ")";
          if (key.equals("q")) queryList.add("keyword:" + value);
          else queryList.add(key + "_keyword:" + value);
        }
      }
    }

    Iterator<String> it = queryList.iterator();
    String queryStr = "";
    while (it.hasNext()) {
      String qTerm = (String) it.next();
      System.out.print("QT: " + qTerm + "\n");
      queryStr += qTerm;
      System.out.print("QS: " + queryStr + "\n");
      if (it.hasNext()) queryStr += " AND ";
    }
    System.out.print("queryStr: " + queryStr);
    query.setQuery(queryStr);
    QueryResponse response = null;
    try {
      response = server.query(query);
    } catch (SolrServerException se) {
      log.error(se.getMessage());
      throw new BadParameterException(se.getMessage());
    } catch (RemoteSolrException rse) {
      if (rse.getMessage().contains("SyntaxError")) {
        log.error("solr syntax error");
        throw new BadParameterException("Incorrect query syntax");
      } else {
        String msg = rse.getMessage().replace("_keyword", "");
        log.error(msg);
        throw new BadParameterException("Incorrect query syntax:" + msg);
      }
    }
    List<FacetField> facets = response.getFacetFields();
    facet = null;
    if (facets != null) {
      facet = new Facet();
      List<FacetType> facetTypes = new ArrayList<FacetType>();
      for (FacetField facetField : facets) {
        List<FacetTerm> facetTerms = new ArrayList<FacetTerm>();
        FacetType facetType = new FacetType();
        facetType.setFacetName(facetField.getName());
        List<FacetField.Count> facetEntries = facetField.getValues();
        for (FacetField.Count fcount : facetEntries) {
          if (fcount.getCount() > 0) {
            FacetTerm facetTerm = new FacetTerm();
            facetTerm.setTermName(fcount.getName());
            facetTerm.setTermCount(fcount.getCount());
            // System.out.println(fcount.getName() + ": " + fcount.getCount());
            facetTerms.add(facetTerm);
          }
        }
        facetType.setFacetTerms(facetTerms);
        facetTypes.add(facetType);
      }
      facet.setFacetTypes(facetTypes);
    }
    docs = response.getResults();
    return docs;
  }
Esempio n. 23
0
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType(XML_RESPONSE_HEADER); // Talkback happens in XML form.
    response.setCharacterEncoding("UTF-8"); // Unicode++
    request.setCharacterEncoding("UTF-8");

    PrintWriter out = null; // The talkback buffer.

    // handle startrecord
    Integer startRecord = 0;

    if (!(request.getParameter("startRecord") == null)) {
      try {
        startRecord = Integer.parseInt(request.getParameter("startRecord")) - 1;
      } catch (NumberFormatException e) {
        startRecord = 0;
      }
    }

    // maximumrecords
    Integer maximumRecords = Integer.parseInt(this.config.getProperty("default_maximumRecords"));
    if (!(request.getParameter("maximumRecords") == null)) {
      maximumRecords = Integer.parseInt(request.getParameter("maximumRecords"));
    }

    // operation
    String operation = request.getParameter("operation");

    // x_collection
    String x_collection = request.getParameter("x-collection");
    if (x_collection == null) x_collection = this.config.getProperty("default_x_collection");
    if (x_collection == null) operation = null;

    // sortkeys
    String sortKeys = request.getParameter("sortKeys");

    // sortorder
    String sortOrder = request.getParameter("sortOrder");

    // recordschema
    String recordSchema = request.getParameter("recordSchema");
    if (recordSchema == null) recordSchema = "dc";

    if (recordSchema.equalsIgnoreCase("dcx")) {
      recordSchema = "dcx";
    }

    if (recordSchema.equalsIgnoreCase("solr")) {
      recordSchema = "solr";
    }

    // query request
    String query = request.getParameter("query");
    String q = request.getParameter("q");

    // who is requestor ?
    String remote_ip = request.getHeader("X-FORWARDED-FOR");

    if (remote_ip == null) {
      remote_ip = request.getRemoteAddr().trim();
    } else {
      remote_ip = request.getHeader("X-FORWARDED-FOR");
    }

    // handle debug
    Boolean debug = Boolean.parseBoolean(request.getParameter("debug"));
    if (!debug) {
      out = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), "UTF8"), true);
    }

    // handle query
    if ((query == null) && (q != null)) {
      query = q;
    } else {
      if ((query != null) && (q == null)) {
        q = query;
      } else {
        operation = null;
      }
    }

    // handle operation
    if (operation == null) {
      if (query != null) {
        operation = "searchRetrieve";
      } else {
        operation = "explain";
      }
    }

    //  searchRetrieve
    if (operation.equalsIgnoreCase("searchRetrieve")) {
      if (query == null) {
        operation = "explain";
        log.debug(operation + ":" + query);
      }
    }

    // start talking back.
    String[] sq = {""};
    String solrquery = "";

    // facet

    String facet = null;
    List<FacetField> fct = null;

    if (request.getParameter("facet") != null) {
      facet = request.getParameter("facet");
      log.debug("facet : " + facet);
    }

    if (operation == null) {
      operation = "searchretrieve";
    } else { // explain response
      if (operation.equalsIgnoreCase("explain")) {
        log.debug("operation = explain");
        out.write("<srw:explainResponse xmlns:srw=\"http://www.loc.gov/zing/srw/\">");
        out.write("</srw:explainResponse>");
      } else { // DEBUG routine
        operation = "searchretrieve";

        String triplequery = null;

        if (query.matches(".*?\\[.+?\\].*?")) { // New symantic syntax
          triplequery = symantic_query(query);
          query = query.split("\\[")[0] + " " + triplequery;
          log.fatal(triplequery);

          solrquery = CQLtoLucene.translate(query, log, config);
        } else {
          solrquery = CQLtoLucene.translate(query, log, config);
        }
        log.debug(solrquery);

        if (debug == true) {
          response.setContentType(HTML_RESPONSE_HEADER);
          out = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), "UTF8"), true);
          out.write("<html><body>\n\n");
          out.write("'" + remote_ip + "'<br>\n");
          out.write("<form action='http://www.kbresearch.nl/kbSRU'>");
          out.write("<input type=text name=q value='" + query + "' size=120>");
          out.write("<input type=hidden name=debug value=True>");
          out.write("<input type=submit>");
          out.write("<table border=1><tr><td>");
          out.write("q</td><td>" + query + "</td></tr><tr>");
          out.write("<td>query out</td><td>" + URLDecoder.decode(solrquery) + "</td></tr>");
          out.write(
              "<tr><td>SOLR_URL</td><td> <a href='"
                  + this.config.getProperty(
                      "collection." + x_collection.toLowerCase() + ".solr_baseurl")
                  + "/?q="
                  + solrquery
                  + "'>"
                  + this.config.getProperty(
                      "collection." + x_collection.toLowerCase() + ".solr_baseurl")
                  + "/select/?q="
                  + solrquery
                  + "</a><br>"
                  + this.config.getProperty("solr_url")
                  + solrquery
                  + "</td></tr>");
          out.write(
              "<b>SOLR_QUERY</b> : <BR> <iframe width=900 height=400 src='"
                  + this.config.getProperty(
                      "collection." + x_collection.toLowerCase() + ".solr_baseurl")
                  + "/../?q="
                  + solrquery
                  + "'></iframe><BR>");
          out.write(
              "<b>SRU_QUERY</b> : <BR> <a href="
                  + this.config.getProperty("baseurl")
                  + "?q="
                  + query
                  + "'>"
                  + this.config.getProperty("baseurl")
                  + "?q="
                  + query
                  + "</a><br><iframe width=901 height=400 src='http://www.kbresearch.nl/kbSRU/?q="
                  + query
                  + "'></iframe><BR>");
          out.write(
              "<br><b>JSRU_QUERY</b> : <BR><a href='http://jsru.kb.nl/sru/?query="
                  + query
                  + "&x-collection="
                  + x_collection
                  + "'>http://jsru.kb.nl/sru/?query="
                  + query
                  + "&x-collection=GGC</a><br><iframe width=900 height=400 src='http://jsru.kb.nl/sru/?query="
                  + query
                  + "&x-collection=GGC'></iframe>");

        } else { // XML SearchRetrieve response
          String url =
              this.config.getProperty("collection." + x_collection.toLowerCase() + ".solr_baseurl");
          String buffer = "";
          CommonsHttpSolrServer server = null;
          server = new CommonsHttpSolrServer(url);
          log.fatal("URSING " + url);
          server.setParser(new XMLResponseParser());
          int numfound = 0;
          try {
            SolrQuery do_query = new SolrQuery();
            do_query.setQuery(solrquery);
            do_query.setRows(maximumRecords);
            do_query.setStart(startRecord);

            if ((sortKeys != null) && (sortKeys.length() > 1)) {
              if (sortOrder != null) {
                if (sortOrder.equals("asc")) {
                  do_query.setSortField(sortKeys, SolrQuery.ORDER.asc);
                }
                if (sortOrder.equals("desc")) {
                  do_query.setSortField(sortKeys, SolrQuery.ORDER.desc);
                }
              } else {
                for (String str : sortKeys.trim().split(",")) {
                  str = str.trim();
                  if (str.length() > 1) {
                    if (str.equals("date")) {
                      do_query.setSortField("date_date", SolrQuery.ORDER.desc);
                      log.debug("SORTORDERDEBUG | DATE! " + str + " | ");
                      break;
                    } else {
                      do_query.setSortField(str + "_str", SolrQuery.ORDER.asc);
                      log.debug("SORTORDERDEBUG | " + str + " | ");
                      break;
                    }
                  }
                }
              }
            }

            if (facet != null) {
              if (facet.indexOf(",") > 1) {
                for (String str : facet.split(",")) {
                  if (str.indexOf("date") > 1) {
                    do_query.addFacetField(str);
                  } else {
                    do_query.addFacetField(str);
                  }
                  // do_query.setParam("facet.method", "enum");
                }
                // q.setFacetSort(false);
              } else {
                do_query.addFacetField(facet);
              }
              do_query.setFacet(true);
              do_query.setFacetMinCount(1);
              do_query.setFacetLimit(-1);
            }

            log.fatal(solrquery);

            QueryResponse rsp = null;
            boolean do_err = false;
            boolean do_sugg = false;
            SolrDocumentList sdl = null;
            String diag = "";
            StringBuffer suggest = new StringBuffer("");

            String content = "1";

            SolrQuery spellq = do_query;
            try {
              rsp = server.query(do_query);
            } catch (SolrServerException e) {
              String header = this.SRW_HEADER.replaceAll("\\$numberOfRecords", "0");
              out.write(header);
              diag = this.SRW_DIAG.replaceAll("\\$error", e.getMessage());
              do_err = true;
              rsp = null;
            }

            log.fatal("query done..");
            if (!(do_err)) { // XML dc response

              SolrDocumentList docs = rsp.getResults();
              numfound = (int) docs.getNumFound();
              int count = startRecord;
              String header =
                  this.SRW_HEADER.replaceAll("\\$numberOfRecords", Integer.toString(numfound));
              out.write(header);
              out.write("<srw:records>");

              Iterator<SolrDocument> iter = rsp.getResults().iterator();

              while (iter.hasNext()) {
                count += 1;
                if (recordSchema.equalsIgnoreCase("dc")) {
                  SolrDocument resultDoc = iter.next();
                  content = (String) resultDoc.getFieldValue("id");
                  out.write("<srw:record>");
                  out.write("<srw:recordPacking>xml</srw:recordPacking>");
                  out.write("<srw:recordSchema>info:srw/schema/1/dc-v1.1</srw:recordSchema>");
                  out.write(
                      "<srw:recordData xmlns:srw_dc=\"info:srw/schema/1/dc-v1.1\" xmlns:mods=\"http://www.loc.gov/mods\" xmlns:dcterms=\"http://purl.org/dc/terms/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:dcx=\"http://krait.kb.nl/coop/tel/handbook/telterms.html\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:marcrel=\"http://www.loc.gov/loc.terms/relators/OTH\" xmlns:facets=\"info:srw/extension/4/facets\" >");
                  StringBuffer result = new StringBuffer("");

                  construct_lucene_dc(result, resultDoc);

                  out.write(result.toString());
                  out.write("</srw:recordData>");
                  out.write(
                      "<srw:recordPosition>" + Integer.toString(count) + "</srw:recordPosition>");
                  out.write("</srw:record>");
                }

                if (recordSchema.equalsIgnoreCase("solr")) {
                  SolrDocument resultDoc = iter.next();
                  content = (String) resultDoc.getFieldValue("id");
                  out.write("<srw:record>");
                  out.write("<srw:recordPacking>xml</srw:recordPacking>");
                  out.write("<srw:recordSchema>info:srw/schema/1/solr</srw:recordSchema>");
                  out.write("<srw:recordData xmlns:expand=\"http://www.kbresearch.nl/expand\">");
                  StringBuffer result = new StringBuffer("");
                  construct_lucene_solr(result, resultDoc);
                  out.write(result.toString());

                  out.write("</srw:recordData>");
                  out.write(
                      "<srw:recordPosition>" + Integer.toString(count) + "</srw:recordPosition>");
                  out.write("</srw:record>");
                }

                if (recordSchema.equalsIgnoreCase("dcx")) { // XML dcx response
                  out.write("<srw:record>");
                  out.write("<srw:recordPacking>xml</srw:recordPacking>");
                  out.write("<srw:recordSchema>info:srw/schema/1/dc-v1.1</srw:recordSchema>");
                  out.write(
                      "<srw:recordData><srw_dc:dc xmlns:srw_dc=\"info:srw/schema/1/dc-v1.1\" xmlns:mods=\"http://www.loc.gov/mods\" xmlns:dcterms=\"http://purl.org/dc/terms/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:dcx=\"http://krait.kb.nl/coop/tel/handbook/telterms.html\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:marcrel=\"http://www.loc.gov/marc.relators/\" xmlns:expand=\"http://www.kbresearch.nl/expand\" xmlns:skos=\"http://www.w3.org/2004/02/skos/core#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" >");
                  SolrDocument resultDoc = iter.next();
                  content = (String) resultDoc.getFieldValue("id");

                  String dcx_data =
                      helpers.getOAIdcx(
                          "http://services.kb.nl/mdo/oai?verb=GetRecord&identifier=" + content,
                          log);
                  if (x_collection.equalsIgnoreCase("ggc-thes")) {
                    dcx_data =
                        helpers.getOAIdcx(
                            "http://serviceso.kb.nl/mdo/oai?verb=GetRecord&identifier=" + content,
                            log);
                  }

                  if (!(dcx_data.length() == 0)) {
                    out.write(dcx_data);
                  } else {
                    // Should not do this!!

                    out.write("<srw:record>");
                    out.write("<srw:recordPacking>xml</srw:recordPacking>");
                    out.write("<srw:recordSchema>info:srw/schema/1/dc-v1.1</srw:recordSchema>");
                    out.write(
                        "<srw:recordData xmlns:srw_dc=\"info:srw/schema/1/dc-v1.1\" xmlns:mods=\"http://www.loc.gov/mods\" xmlns:dcterms=\"http://purl.org/dc/terms/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:dcx=\"http://krait.kb.nl/coop/tel/handbook/telterms.html\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:marcrel=\"http://www.loc.gov/loc.terms/relators/OTH\" >");
                    StringBuffer result = new StringBuffer("");

                    construct_lucene_dc(result, resultDoc);

                    out.write(result.toString());
                    out.write("</srw:recordData>");
                    out.write(
                        "<srw:recordPosition>" + Integer.toString(count) + "</srw:recordPosition>");
                    out.write("</srw:record>");
                  }

                  out.write("</srw_dc:dc>");

                  StringBuffer expand_data;
                  boolean expand = false;

                  if (content.startsWith("GGC-THES:AC:")) {
                    String tmp_content = "";
                    tmp_content = content.replaceFirst("GGC-THES:AC:", "");
                    log.fatal("calling get");
                    expand_data =
                        new StringBuffer(
                            helpers.getExpand(
                                "http://www.kbresearch.nl/general/lod_new/get/"
                                    + tmp_content
                                    + "?format=rdf",
                                log));
                    log.fatal("get finini");

                    if (expand_data.toString().length() > 4) {

                      out.write(
                          "<srw_dc:expand xmlns:srw_dc=\"info:srw/schema/1/dc-v1.1\" xmlns:expand=\"http://www.kbresearch.nl/expand\" xmlns:skos=\"http://www.w3.org/2004/02/skos/core#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" >");
                      out.write(expand_data.toString());
                      expand = true;
                    }
                  } else {
                    expand_data =
                        new StringBuffer(
                            helpers.getExpand(
                                "http://www.kbresearch.nl/ANP.cgi?q=" + content, log));
                    if (expand_data.toString().length() > 0) {
                      if (!expand) {
                        out.write(
                            "<srw_dc:expand xmlns:srw_dc=\"info:srw/schema/1/dc-v1.1\" xmlns:expand=\"http://www.kbresearch.nl/expand\" xmlns:skos=\"http://www.w3.org/2004/02/skos/core#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" >");
                        expand = true;
                      }
                      out.write(expand_data.toString());
                    }
                  }
                  if (expand) {
                    out.write("</srw_dc:expand>");
                  }

                  out.write("</srw:recordData>");
                  out.write(
                      "<srw:recordPosition>" + Integer.toString(count) + "</srw:recordPosition>");
                  out.write("</srw:record>");
                }
              }
            }

            if ((do_err) || (numfound == 0)) {
              log.fatal("I haz suggestions");

              try {
                spellq.setParam("spellcheck", true);
                spellq.setQueryType("/spell");
                server = new CommonsHttpSolrServer(url);
                rsp = server.query(spellq);
                sdl = rsp.getResults();
                SpellCheckResponse spell;
                spell = rsp.getSpellCheckResponse();
                List<SpellCheckResponse.Suggestion> suggestions = spell.getSuggestions();
                if (suggestions.isEmpty() == false) {
                  suggest.append("<srw:extraResponseData>");
                  suggest.append("<suggestions>");

                  for (SpellCheckResponse.Suggestion sugg : suggestions) {
                    suggest.append("<suggestionfor>" + sugg.getToken() + "</suggestionfor>");
                    for (String item : sugg.getSuggestions()) {
                      suggest.append("<suggestion>" + item + "</suggestion>");
                    }
                    suggest.append("</suggestions>");
                    suggest.append("</srw:extraResponseData>");
                  }
                  do_sugg = true;
                }
              } catch (Exception e) {
                rsp = null;
                // log.fatal(e.toString());
              }
              ;
            }
            ;

            if (!do_err) {
              if (facet != null) {

                try {
                  fct = rsp.getFacetFields();
                  out.write("<srw:facets>");

                  for (String str : facet.split(",")) {
                    out.write("<srw:facet>");
                    out.write("<srw:facetType>");
                    out.write(str);
                    out.write("</srw:facetType>");

                    for (FacetField f : fct) {
                      log.debug(f.getName());
                      // if (f.getName().equals(str+"_str") || (f.getName().equals(str+"_date")) ) {
                      List<FacetField.Count> facetEnties = f.getValues();
                      for (FacetField.Count fcount : facetEnties) {
                        out.write("<srw:facetValue>");
                        out.write("<srw:valueString>");
                        out.write(helpers.xmlEncode(fcount.getName()));
                        out.write("</srw:valueString>");
                        out.write("<srw:count>");
                        out.write(Double.toString(fcount.getCount()));
                        out.write("</srw:count>");
                        out.write("</srw:facetValue>");
                        //   }
                      }
                    }
                    out.write("</srw:facet>");
                  }
                  out.write("</srw:facets>");
                  startRecord += 1;
                } catch (Exception e) {
                }

                // log.fatal(e.toString()); }
              }
            } else {
              out.write(diag);
            }
            out.write("</srw:records>"); // SearchRetrieve response footer
            String footer = this.SRW_FOOTER.replaceAll("\\$query", helpers.xmlEncode(query));
            footer = footer.replaceAll("\\$startRecord", (startRecord).toString());
            footer = footer.replaceAll("\\$maximumRecords", maximumRecords.toString());
            footer = footer.replaceAll("\\$recordSchema", recordSchema);
            if (do_sugg) {
              out.write(suggest.toString());
            }
            out.write(footer);
          } catch (MalformedURLException e) {
            out.write(e.getMessage());
          } catch (IOException e) {
            out.write("TO ERR is Human");
          }
        }
      }
    }
    out.close();
  }
Esempio n. 24
0
 @RequestMapping(
     value = "/postupdatelogin",
     method = RequestMethod.POST,
     produces = "text/plain;charset=UTF-8")
 @ResponseBody
 public static String solrqueryresult(HttpServletRequest request) {
   loginDao logindao = new loginImpl();
   String data = request.getParameter("info");
   String cid = "";
   String lid = "";
   String state = "";
   String account = "";
   String bug_one = "";
   List<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
   List<SolrLogin> results = new ArrayList<SolrLogin>();
   ModifiableSolrParams params = new ModifiableSolrParams();
   data = data.replaceAll("\\{|\"|\\}", "").trim();
   String[] data1 = data.split(",");
   for (String s : data1) {
     if (s.contains("cid")) {
       String[] s1 = s.split(":");
       cid = s1[1];
     }
     if (s.contains("account")) {
       String[] s1 = s.split(":");
       account = s1[1];
     }
   }
   //		params.add("q",  "cid" + ":" + cid + " AND " + "account" + ":" + account);
   params.add("q", "account" + ":" + account);
   params.add("start", "0"); // 记录开始位置
   params.add("rows", "3001"); // 查询的行数
   try {
     QueryResponse rsp = server.query(params);
     long p = rsp.getResults().getNumFound();
     if (p > 0) {
       results = rsp.getBeans(SolrLogin.class);
       for (SolrLogin bean : results) {
         SolrInputDocument doc = new SolrInputDocument();
         String beanjson = JSON.toJSONString(bean, false);
         beanjson = beanjson.replaceAll("\\{|\\}|\"", "").trim();
         String[] bjson = beanjson.split(",");
         cid = bean.cid;
         for (String s : bjson) {
           String[] w = s.split(":");
           if (s.contains("lid")) {
             String[] s1 = s.split(":");
             if (s1.length > 1) {
               doc.addField(s1[0], s1[1]);
               lid = s1[1];
             } else {
               doc.setField(s1[0], "");
             }
             continue;
           }
           if (s.contains("account")) {
             String[] s1 = s.split(":");
             if (s1.length > 1) {
               doc.addField(s1[0], s1[1]);
             } else {
               doc.addField(s1[0], "");
             }
             continue;
           }
           if (s.contains("password")) {
             String[] s1 = s.split(":");
             if (s1.length > 1) {
               doc.addField(s1[0], s1[1]);
             } else {
               doc.addField(s1[0], "");
             }
             continue;
           }
           if (s.contains("cid")) {
             String[] s1 = s.split(":");
             if (s1.length > 1) {
               doc.addField(s1[0], s1[1]);
             } else {
               doc.addField(s1[0], "");
             }
             continue;
           }
           if (s.contains("state")) {
             String[] s1 = s.split(":");
             if (s1.length > 1) {
               state = "0";
               doc.setField(s1[0], "0");
             } else {
               doc.setField(s1[0], "0");
             }
             continue;
           }
           if (s.contains("type")) {
             String[] s1 = s.split(":");
             if (s1.length > 1) {
               doc.addField(s1[0], s1[1]);
             } else {
               doc.addField(s1[0], "");
             }
             continue;
           }
           if (s.contains("integral")) {
             String[] s1 = s.split(":");
             if (s1.length > 1) {
               doc.addField(s1[0], s1[1]);
             } else {
               doc.addField(s1[0], "");
             }
             continue;
           }
           if (s.contains("createTime")) {
             String[] s1 = s.split(":");
             if (s1.length > 1) {
               doc.addField(s1[0], s1[1]);
             } else {
               doc.addField(s1[0], "");
             }
             continue;
           }
           if (s.contains("modifyTime")) {
             String[] s1 = s.split(":");
             if (s1.length > 1) {
               doc.addField(s1[0], s1[1]);
             } else {
               doc.addField(s1[0], "");
             }
             continue;
           }
           if ("headspace".equals(w[0])) {
             String[] s1 = s.split(":");
             if (s1.length > 1) {
               doc.addField(s1[0], s1[1]);
             } else {
               doc.addField(s1[0], "");
             }
             continue;
           }
           if (s.contains("headspace1")) {
             String[] s1 = s.split(":");
             if (s1.length > 1) {
               doc.addField(s1[0], s1[1]);
             } else {
               doc.addField(s1[0], "");
             }
             continue;
           }
           if (s.contains("headspace2")) {
             String[] s1 = s.split(":");
             if (s1.length > 1) {
               doc.addField(s1[0], s1[1]);
             } else {
               doc.addField(s1[0], "");
             }
             continue;
           }
           if (s.contains("headspace3")) {
             String[] s1 = s.split(":");
             if (s1.length > 1) {
               doc.addField(s1[0], s1[1]);
             } else {
               doc.addField(s1[0], "");
             }
             continue;
           }
           if (s.contains("headspace4")) {
             String[] s1 = s.split(":");
             if (s1.length > 1) {
               doc.addField(s1[0], s1[1]);
             } else {
               doc.addField(s1[0], "");
             }
             continue;
           }
         }
         docs.add(doc);
       }
     }
     try {
       server.add(docs);
       server.commit();
       logindao.loginUpdateImpl(lid, state);
     } catch (Exception e) {
       LogRecord.error(updatelogin.class, e);
       bug_one = e.getMessage();
     }
   } catch (SolrServerException e) {
     LogRecord.error(updatelogin.class, e);
     bug_one = e.getMessage();
   }
   return bug_one.length() > 1
       ? "{\"errorCode\":\"060002\",\"msg\":\"操作失败\"}"
       : "{\"errorCode\":\"000000\",\"cid\":\"" + cid + "\",\"msg\":\"操作成功\"}";
 }
Esempio n. 25
0
  private static long getTermCount(
      SolrServer server,
      Map<String, Integer> singleCountMap,
      SearchTermAndList searchTermAndList,
      Map<String, String> filterGrayList,
      Map<String, String> keepGrayList) {
    List<String[]> searchTerms = searchTermAndList.asListArray();
    Integer searchTermCountObject =
        searchTerms.size() != 1 ? null : singleCountMap.get(join(searchTerms.get(0), ","));
    long searchTermCount = 0;
    if (searchTermCountObject == null) {
      // didn't find it in map, so need to go get count
      SolrQuery query = new SolrQuery();
      query.setQuery("+text:(*:*)");
      query.addFilterQuery("+pub_date_year:[1993 TO 2013]");
      query.setParam("fl", "pmid");

      for (int i = 0; i < searchTerms.size(); i++) {
        String term1 = "";
        for (String aTermArray : searchTerms.get(i)) {
          if (filterGrayList.containsKey(aTermArray.toLowerCase())) {
            String filterTerms = filterGrayList.get(aTermArray.toLowerCase());
            String[] splitFilterTerms = filterTerms.split(",");
            term1 = term1 + "(+\"" + aTermArray + "\" -(";

            for (String splitFilterTerm : splitFilterTerms) {
              term1 = term1 + "\"" + splitFilterTerm + "\" ";
            }
            term1 = term1 + ")) ";
          } else if (keepGrayList.containsKey(aTermArray.toLowerCase())) {
            String keepTerms = keepGrayList.get(aTermArray.toLowerCase());
            String[] splitKeepTerms = keepTerms.split(",");
            term1 = term1 + "(+\"" + aTermArray + "\" +(";

            for (String splitKeepTerm : splitKeepTerms) {
              term1 = term1 + "\"" + splitKeepTerm + "\" ";
            }
            term1 = term1 + ")) ";
          } else {
            term1 = term1 + "\"" + aTermArray + "\" ";
          }
        }

        query.addFilterQuery("+text:(" + term1 + ")");
      }

      try {
        QueryResponse rsp = server.query(query);
        searchTermCount = rsp.getResults().getNumFound();
        singleCountMap.put(
            join(searchTerms.get(0), ","), Integer.parseInt(Long.toString(searchTermCount)));
      } catch (SolrServerException e) {
        // exit out if there is an error
        log.warning(e.getMessage());
        System.exit(1);
      }
    } else {
      searchTermCount = searchTermCountObject;
    }
    return searchTermCount;
  }
Esempio n. 26
0
    public NGDItem call() {
      NGDItem totalResults = null;
      SolrQuery query = new SolrQuery();

      query.setQuery("+text:(*:*)");
      query.addFilterQuery("+pub_date_year:[1993 TO 2013]");
      query.setParam("fl", "pmid");

      for (int i = 0; i < term1Array.size(); i++) {
        String term1Combined = createCombinedTerm(term1Array.get(i));
        query.addFilterQuery("+text:(" + term1Combined + ")");
      }
      for (int j = 0; j < term2Array.size(); j++) {
        String term2Combined = createCombinedTerm(term2Array.get(j));
        query.addFilterQuery("+text:(" + term2Combined + ")");
      }

      boolean retry = false;
      try {

        QueryResponse rsp = this.server.query(query);
        totalResults =
            new NGDItem(
                this.term1count,
                this.term2count,
                this.term1,
                this.term2,
                this.term1Array,
                this.term2Array,
                rsp.getResults().getNumFound(),
                useAlias,
                this.totalDocCount);
      } catch (SolrServerException e) {
        log.warning(e.getMessage());
        e.printStackTrace();
        retry = true;
      }

      if (retry) {
        log.warning("retrying query");
        try {
          // retry once.....
          QueryResponse rsp = this.server.query(query);
          totalResults =
              new NGDItem(
                  this.term1count,
                  this.term2count,
                  this.term1,
                  this.term2,
                  this.term1Array,
                  this.term2Array,
                  rsp.getResults().getNumFound(),
                  useAlias,
                  this.totalDocCount);
        } catch (SolrServerException e) {
          log.warning(e.getMessage());
          e.printStackTrace();
          System.exit(1);
        }
      }
      return totalResults;
    }