Beispiel #1
0
 /**
  * Sets the URL to be used by the currently selected search client when running requests.
  *
  * @param url
  */
 public void setServiceUrl(String url) {
   if (url != null && searchClient != null && !url.equals(searchClient.getServiceUrl())) {
     pzreq.getRecord().removeParametersInState();
     pzreq.getSearch().removeParametersInState();
     pzresp.getSp().resetAuthAndBeyond(true);
     searchClient.setServiceUrl(url);
   }
 }
Beispiel #2
0
  public Map<String, Long> countByField(IndexField indexField, FilterBuilder filter) {
    Map<String, Long> counts = new HashMap<>();

    SearchRequestBuilder request =
        client
            .prepareSearch(this.getIndexName())
            .setTypes(this.getIndexType())
            .setQuery(QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(), filter))
            .setSize(0)
            .addAggregation(
                AggregationBuilders.terms(indexField.field())
                    .field(indexField.field())
                    .order(Terms.Order.count(false))
                    .size(Integer.MAX_VALUE)
                    .minDocCount(0));

    SearchResponse response = request.get();

    Terms values = response.getAggregations().get(indexField.field());

    for (Terms.Bucket value : values.getBuckets()) {
      counts.put(value.getKey(), value.getDocCount());
    }
    return counts;
  }
Beispiel #3
0
  @Override
  @CheckForNull
  public Date getLastSynchronization(Map<String, String> params) {
    SearchRequestBuilder request =
        client
            .prepareSearch(this.getIndexName())
            .setTypes(this.getIndexType())
            .setQuery(
                QueryBuilders.filteredQuery(
                    QueryBuilders.matchAllQuery(), getLastSynchronizationBuilder(params)))
            .setSize(0)
            .addAggregation(
                AggregationBuilders.max("latest").field(BaseNormalizer.UPDATED_AT_FIELD));

    SearchResponse response = request.get();

    Max max = response.getAggregations().get("latest");
    if (max.getValue() > 0) {
      Date date = new DateTime((long) max.getValue()).toDate();
      LOG.debug(
          "Index {}:{} has last update of {}", this.getIndexName(), this.getIndexType(), date);
      return date;
    } else {
      LOG.debug("Index {}:{} has no last update date", this.getIndexName(), this.getIndexType());
      return null;
    }
  }
Beispiel #4
0
 @Override
 public IndexStat getIndexStat() {
   CountRequestBuilder countRequest =
       client
           .prepareCount(this.getIndexName())
           .setTypes(this.getIndexType())
           .setQuery(QueryBuilders.matchAllQuery());
   CountResponse response = countRequest.get();
   return new IndexStat(getLastSynchronization(), response.getCount());
 }
Beispiel #5
0
  @Test
  @Ignore
  public void testSearch() {
    System.out.println("test search");

    // Valid service connection
    System.out.println("Valid Service Connection - Case Management");
    String url = "http://localhost:8080/sola/webservices/search-service?wsdl";
    SearchClient result =
        WSManager.getInstance()
            .getWSClient(SearchClientImpl.class, url, "test", "test".toCharArray());
    System.out.print("Checking connection:");
    System.out.println(result.checkConnection());

    //               GenericResult res = result.test();
    //        System.out.println("test");
    //        System.out.println("result:" + res);

  }
Beispiel #6
0
  public static String[] getAddresses(String name, int numResults, String withinURL) {
    String query = "";
    if (withinURL != null && withinURL.length() > 0) query += "inurl:" + withinURL + " ";

    query += name;

    ArrayList<String> arrayList = new ArrayList<String>();

    SearchClient client =
        new SearchClient("OzomwBrV34GSUD6G3vWHVV43nlVF.srrxIT88JqkrDhrTPAqLMhNBwZa6TT.8WuJmCU-");

    // Create the web search request. In this case we're searching for
    // java-related hits.
    WebSearchRequest request = new WebSearchRequest(query);
    request.setResults(numResults);

    try {
      // Execute the search.
      WebSearchResults results = client.webSearch(request);

      // Iterate over the results.
      for (int i = 0; i < results.listResults().length; i++) {
        WebSearchResult result = results.listResults()[i];

        // Print out the document title and URL.
        arrayList.add(result.getUrl());
      }
    } catch (IOException e) {
      // Most likely a network exception of some sort.
      System.err.println("Error calling Yahoo! Search Service: " + e.toString());
      e.printStackTrace(System.err);
    } catch (SearchException e) {
      // An issue with the XML or with the service.
      System.err.println("Error calling Yahoo! Search Service: " + e.toString());
      e.printStackTrace(System.err);
    }
    String[] dummy = new String[0];
    return arrayList.toArray(dummy);
  }
Beispiel #7
0
  protected void initializeIndex() {
    String index = this.getIndexName();

    IndicesExistsResponse indexExistsResponse = client.prepareIndicesExist(index).get();
    try {

      if (!indexExistsResponse.isExists()) {
        LOG.debug("Setup of {} for type {}", this.getIndexName(), this.getIndexType());
        client.prepareCreate(index).setSettings(getIndexSettings()).get();
      }

      LOG.debug("Update of index {} for type {}", this.getIndexName(), this.getIndexType());
      client
          .preparePutMapping(index)
          .setType(getIndexType())
          .setIgnoreConflicts(true)
          .setSource(mapDomain())
          .get();

    } catch (Exception e) {
      throw new IllegalStateException("Invalid configuration for index " + this.getIndexName(), e);
    }
  }
Beispiel #8
0
 /**
  * Configures the selected search client using the selected configuration reader.
  *
  * <p>The configuration reader is select deploy-time - by configuration in the application's
  * beans.xml.
  *
  * @param client search client to use
  * @param configReader the selected configuration mechanism
  * @throws MissingConfigurationContextException if this object is injected before there is a Faces
  *     context for example in a Servlet filter.
  */
 public void configureClient(SearchClient client, ConfigurationReader configReader)
     throws MissingConfigurationContextException {
   logger.debug(Utils.objectId(this) + " will configure search client for the session");
   try {
     client.configure(configReader);
   } catch (MissingConfigurationContextException mcce) {
     logger.info("No Faces context is available to the configurator at this time of invocation");
     throw mcce;
   } catch (ConfigurationException e) {
     logger.debug("Pz2Service adding configuration error");
     errors.addConfigurationError(
         new ConfigurationError("Search Client", "Configuration", e.getMessage()));
   }
   logger.info(configReader.document());
   pzresp.getSp().resetAuthAndBeyond(true);
 }
Beispiel #9
0
  @CheckForNull
  @Override
  public DOMAIN getNullableByKey(KEY key) {
    GetRequestBuilder request =
        client
            .prepareGet()
            .setType(this.getIndexType())
            .setIndex(this.getIndexName())
            .setId(this.getKeyValue(key))
            .setFetchSource(true)
            .setRouting(this.getKeyValue(key));

    GetResponse response = request.get();

    if (response.isExists()) {
      return toDoc(response.getSource());
    }
    return null;
  }
Beispiel #10
0
  public List<DOMAIN> getByKeys(Collection<KEY> keys) {
    if (keys.isEmpty()) {
      return Collections.emptyList();
    }
    List<DOMAIN> results = new ArrayList<>();
    MultiGetRequestBuilder request = client.prepareMultiGet().setPreference("_local");
    for (KEY key : keys) {
      request.add(
          new MultiGetRequest.Item(getIndexName(), getIndexType(), getKeyValue(key))
              .routing(getKeyValue(key))
              .fetchSourceContext(FetchSourceContext.FETCH_SOURCE));
    }

    MultiGetResponse response = request.get();
    if (response.getResponses() != null) {
      for (MultiGetItemResponse item : response.getResponses()) {
        Map<String, Object> source = item.getResponse().getSource();
        if (source != null) {
          results.add(toDoc(source));
        }
      }
    }
    return results;
  }
Beispiel #11
0
 public Long countAll() {
   return client.prepareCount(this.getIndexName()).setTypes(this.getIndexType()).get().getCount();
 }
Beispiel #12
0
 public boolean getServiceUrlIsDefined() {
   return (searchClient != null && searchClient.hasServiceUrl());
 }
Beispiel #13
0
 /**
  * Gets the currently selected URL used for executing requests.
  *
  * @return
  */
 public String getServiceUrl() {
   return (searchClient != null ? searchClient.getServiceUrl() : "");
 }