public static void addDoc(String docnum, String payloads) {
    // 创建doc文档
    SolrInputDocument doc = new SolrInputDocument();
    doc.addField("id", docnum);
    doc.addField("payloads", payloads);
    System.out.println();
    System.out.println("docnum: " + docnum);
    System.out.println("payloads: " + payloads);
    log.info("docnum: " + docnum + "\tpayloads:" + payloads + "\twordcount:" + (j + existWords));
    try {
      // 添加一个doc文档
      UpdateResponse response = server.add(doc);
      // commit后才保存到索引库
      server.commit();

      /* System.out.println("Query Time:" + response.getQTime());
      System.out.println("Elapsed Time:" + response.getElapsedTime());
      System.out.println("Status:" + response.getStatus());*/

    } catch (SolrServerException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  @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"));
      }
    }
  }
      public void run() {
        // create new collections rapid fire
        Map<String, List<Integer>> collectionInfos = new HashMap<String, List<Integer>>();
        int cnt = random().nextInt(TEST_NIGHTLY ? 13 : 3) + 1;

        for (int i = 0; i < cnt; i++) {
          String collectionName = "awholynewstresscollection_" + name + "_" + i;
          int numShards = _TestUtil.nextInt(random(), 0, shardCount * 2) + 1;
          int replicationFactor = _TestUtil.nextInt(random(), 0, 3) + 1;
          int maxShardsPerNode =
              (((numShards * 2 * replicationFactor)
                      / getCommonCloudSolrServer()
                          .getZkStateReader()
                          .getClusterState()
                          .getLiveNodes()
                          .size()))
                  + 1;

          CloudSolrServer client = null;
          try {
            if (i == 0) {
              client = createCloudClient(null);
            } else if (i == 1) {
              client = createCloudClient(collectionName);
            }

            createCollection(
                collectionInfos,
                collectionName,
                numShards,
                replicationFactor,
                maxShardsPerNode,
                client,
                null,
                "conf1");

            // remove collection
            ModifiableSolrParams params = new ModifiableSolrParams();
            params.set("action", CollectionAction.DELETE.toString());
            params.set("name", collectionName);
            QueryRequest request = new QueryRequest(params);
            request.setPath("/admin/collections");

            if (client == null) {
              client = createCloudClient(null);
            }

            client.request(request);

          } catch (SolrServerException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
          } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
          } finally {
            if (client != null) client.shutdown();
          }
        }
      }
  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;
  }
  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;
  }
  public SearchResponse search(SearchRequest request) {
    SolrQuery query = new SolrQuery();
    query.setRows(request.getLimit());
    query.setStart(request.getOffset() * request.getLimit());
    BooleanQuery aggregate = new BooleanQuery();
    if (!StringUtils.isBlank(request.getText())) {
      TermQuery termQuery = new TermQuery(new Term("", request.getText()));
      aggregate.add(termQuery, BooleanClause.Occur.SHOULD);
    }
    if (!StringUtils.isBlank(request.getEventId())) {
      aggregate.add(
          new TermQuery(new Term("eventid", request.getEventId())), BooleanClause.Occur.MUST);
    }
    query.setQuery(aggregate.toString());

    log.info("QUERY IS: " + query.toString());
    try {
      QueryResponse queryResponse = solrServer.query(query);
      log.info("RESPONSE FROM QUERY WAS: " + queryResponse);
      SolrDocumentList results = queryResponse.getResults();
      log.info("RESULTS WAS: " + results);
      ArrayList<SearchResponse.Hit> hits = new ArrayList<SearchResponse.Hit>();
      for (SolrDocument result : results) {
        hits.add(
            new SearchResponse.Hit(
                ObjectType.session, String.valueOf(result.getFieldValue("id")), null));
      }
      return new SearchResponse(results.getNumFound(), queryResponse.getElapsedTime(), hits);
    } catch (SolrServerException e) {
      e.printStackTrace();
    }
    return null;
  }
  @Override
  public RecommendResponse recommend(RecommendQuery query, Integer maxReuslts) {
    productsToFilter = new ArrayList<String>();

    String queryString = "*:*";
    String sortCriteria = "user_count_purchased desc";
    ModifiableSolrParams solrParams = new ModifiableSolrParams();
    QueryResponse response = null;
    RecommendResponse searchResponse = new RecommendResponse();

    try {
      String filterQueryString =
          RecommendationQueryUtils.buildFilterForContentBasedFiltering(contentFilter);

      if (query.getProductIds() != null && query.getProductIds().size() > 0) {
        productsToFilter.addAll(query.getProductIds());
      }
      if (query.getUser() != null) {
        if (alreadyBoughtProducts != null) {
          productsToFilter.addAll(alreadyBoughtProducts);
        }
      }

      if (productsToFilter != null && productsToFilter.size() > 0) {
        if (filterQueryString.length() > 0) {
          filterQueryString += " OR ";
        }
        filterQueryString +=
            RecommendationQueryUtils.buildFilterForAlreadyBoughtProducts(productsToFilter);
      }
      solrParams.set("q", queryString);
      solrParams.set("fq", filterQueryString);
      solrParams.set("sort", sortCriteria);
      solrParams.set("rows", maxReuslts);

      response =
          SolrServiceContainer.getInstance()
              .getRecommendService()
              .getSolrServer()
              .query(solrParams);
      // fill response object
      List<String> extractedRecommendations =
          RecommendationQueryUtils.extractRecommendationIds(
              response.getBeans(CustomerAction.class));
      searchResponse.setResultItems(extractedRecommendations);
      searchResponse.setElapsedTime(response.getElapsedTime());
      SolrDocumentList docResults = response.getResults();
      searchResponse.setNumFound(docResults.getNumFound());
    } catch (SolrServerException e) {
      e.printStackTrace();
      searchResponse.setNumFound(0);
      searchResponse.setResultItems(new ArrayList<String>());
      searchResponse.setElapsedTime(-1);
    }

    return searchResponse;
  }
示例#8
0
  public JSONArray Search(String query, String sort) {
    SolrServer server;
    SolrDocumentList docs = null;
    String Date_format = null; // for Date格式轉換
    JSONArray jsonArray = new JSONArray();
    try {
      server = new HttpSolrServer(url);
      // add

      SolrQuery solrQuery = new SolrQuery();
      solrQuery.setQuery(query);
      solrQuery.setStart(_start);
      solrQuery.setRows(_nbDocuments);
      solrQuery.setRequestHandler("query");
      solrQuery.set("fl", "*,score"); // 設定fl參數,指定要傳回哪個field的資料,這裡設定所有field與score
      if (_fq1 != "") {
        solrQuery.addFilterQuery("ProductName:" + _fq1);
      }
      if (_fq2 != "") {
        solrQuery.addFilterQuery("publishedDate:" + _fq2);
      }
      if (sort != null) {
        solrQuery.addSortField(sort, ORDER.asc);
      }
      solrQuery.setRequestHandler("/browse");
      response = server.query(solrQuery);
      docs = response.getResults();
      if (docs != null) {
        System.out.println(
            docs.getNumFound() + " documents found, " + docs.size() + " returned : ");
        setResultNumber(docs.getNumFound(), docs.size()); // 設定目前回傳幾筆資料給前端
        for (int i = 0; i < docs.size(); i++) {
          SolrDocument doc = docs.get(i);
          JSONObject jsonObject = new JSONObject();
          for (Iterator<Entry<String, Object>> it2 = doc.iterator(); it2.hasNext(); ) {
            Entry<String, Object> entry = it2.next();
            if (entry.getKey().equals("publishedDate")) { // 將傳回的date格式轉為純字串,方便前端呈現
              Date_format = entry.getValue().toString();
              jsonObject.put(entry.getKey(), Date_format);
            } else {
              // 一般情況
              jsonObject.put(entry.getKey(), entry.getValue());
            }
          }
          System.out.print("\n");
          // 將總共找到幾筆資料存在jsonarray的最後面傳給前端
          jsonObject.put("TotalResultFound", docs.getNumFound());
          jsonArray.add(jsonObject);
        }
      }

    } catch (SolrServerException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return jsonArray;
  }
示例#9
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);
   }
 }
 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());
   }
 }
示例#11
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);
   }
 }
  @Override
  protected OntologyTerm createRootTerm(OntologySearcher ontologySearcher) {

    try {
      return new LazyLoadedOntologyTerm(ontologySearcher, "MI:0190", "Interaction Type");
    } catch (SolrServerException e) {
      e.printStackTrace();
      addErrorMessage("Could not load the tree", "");
    }
    return null;
  }
  @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());
    }
  }
示例#15
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();
   }
 }
  /**
   * *
   *
   * @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);
    }
  }
示例#17
0
  @Override
  public void run() {
    Random random = LuceneTestCase.random();
    int numSearches = 0;

    while (true && !stop) {
      numSearches++;
      try {
        // to come to the aid of their country.
        cloudClient.query(new SolrQuery(QUERIES[random.nextInt(QUERIES.length)]));
      } catch (Exception e) {
        System.err.println("QUERY REQUEST FAILED:");
        e.printStackTrace();
        if (e instanceof SolrServerException) {
          System.err.println("ROOT CAUSE:");
          ((SolrServerException) e).getRootCause().printStackTrace();
        }
        queryFails.incrementAndGet();
      }
      try {
        Thread.sleep(random.nextInt(4000) + 300);
      } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
      }
    }

    log.info("num searches done:" + numSearches + " with " + queryFails + " fails");
  }
 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;
 }
示例#19
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());
   }
 }
示例#20
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();
 }
示例#21
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();
     }
   }
 }
示例#22
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 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;
    }
  }
  @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;
    }
  }
示例#25
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;
 }
示例#26
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;
 }
  @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;
    }
  }
 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());
   }
 }
示例#29
0
 public boolean execute() {
   SolrQuery query = queryGenerator.generate();
   try {
     logger.debug("executing query: " + query);
     long init = System.nanoTime();
     QueryResponse response = this.executeQuery(query);
     long clientTime = (System.nanoTime() - init) / 1000000;
     logger.debug(
         response.getResults().getNumFound() + " results found in " + response.getQTime() + " ms");
     if (response.getQTime() < 0) {
       throw new RuntimeException(
           "The query returned less than 0 as q time: "
               + response.getResponseHeader().get("q")
               + response.getQTime());
     }
     executor.notifyQueryExecuted(response, clientTime);
   } catch (SolrServerException e) {
     logger.error("Error on Query " + query);
     e.printStackTrace();
     executor.notifyError(new QueryException(e, query));
     return false;
   }
   return true;
 }
示例#30
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();
  }