예제 #1
0
  private SearchRequestBuilder standardSearchRequest(
      String query, Set<String> indices, int limit, int offset, TimeRange range, Sorting sort)
      throws IndexHelper.InvalidRangeFormatException {
    if (query == null || query.trim().isEmpty()) {
      query = "*";
    }

    SearchRequestBuilder srb = c.prepareSearch();
    srb.setIndices(indices.toArray(new String[] {}));

    if (query.trim().equals("*")) {
      srb.setQuery(matchAllQuery());
    } else {
      QueryStringQueryBuilder qs = queryString(query);
      qs.allowLeadingWildcard(server.getConfiguration().isAllowLeadingWildcardSearches());
      srb.setQuery(qs);
    }

    srb.setFrom(offset);

    if (limit > 0) {
      srb.setSize(limit);
    }

    if (range != null) {
      srb.setFilter(IndexHelper.getTimestampRangeFilter(range));
    }

    if (sort != null) {
      srb.addSort(sort.getField(), sort.asElastic());
    }

    return srb;
  }
예제 #2
0
  public HistogramResult histogram(
      String query, Indexer.DateHistogramInterval interval, String filter, TimeRange range)
      throws IndexHelper.InvalidRangeFormatException {
    DateHistogramFacetBuilder fb =
        FacetBuilders.dateHistogramFacet("histogram")
            .field("timestamp")
            .interval(interval.toString().toLowerCase());

    fb.facetFilter(standardFilters(range, filter));

    QueryStringQueryBuilder qs = queryString(query);
    qs.allowLeadingWildcard(server.getConfiguration().isAllowLeadingWildcardSearches());

    SearchRequestBuilder srb = c.prepareSearch();
    srb.setIndices(IndexHelper.determineAffectedIndices(server, range).toArray(new String[] {}));
    srb.setQuery(qs);
    srb.addFacet(fb);

    final SearchRequest request = srb.request();
    SearchResponse r = c.search(request).actionGet();
    return new DateHistogramResult(
        (DateHistogramFacet) r.getFacets().facet("histogram"),
        query,
        request.source(),
        interval,
        r.getTook());
  }
  public static SearchRequest randomSearch(Client client, String[] indices) {

    final SearchRequestBuilder builder = new SearchRequestBuilder(client);
    builder.setIndices(indices);
    builder.setTypes(INDEX_TYPE);
    builder.setQuery(randomFrom(TestQueryType.values()).getQuery());
    return builder.request();
  }
  /** Set indices and types to the search request. */
  private void setIndicesAndTypes() {
    request.setIndices(query.getIndexArr());

    String[] typeArr = query.getTypeArr();
    if (typeArr != null) {
      request.setTypes(typeArr);
    }
  }
예제 #5
0
  private SearchHit oneOfIndex(String index, QueryBuilder q, SortOrder sort) {
    SearchRequestBuilder srb = c.prepareSearch();
    srb.setIndices(index);
    srb.setQuery(q);
    srb.setSize(1);
    srb.addSort("timestamp", sort);

    SearchResponse r = c.search(srb.request()).actionGet();
    if (r.getHits() != null && r.getHits().totalHits() > 0) {
      return r.getHits().getAt(0);
    } else {
      return null;
    }
  }
예제 #6
0
 public List<String> autocomplete(String prefix, int limit) {
   SearchRequestBuilder builder = new SearchRequestBuilder(factory.client());
   builder.setIndices(Constants.INDEX).setTypes(Constants.TYPE);
   builder.setSearchType(SearchType.DEFAULT);
   builder.setFrom(0).setSize(limit).setExplain(IS_EXPLAIN);
   QueryBuilder queryBuilder = QueryBuilders.matchPhraseQuery("title", prefix);
   builder.setQuery(queryBuilder.toString());
   logger.info("Search qeury");
   logger.info(builder.toString());
   SearchResponse sr = builder.execute().actionGet();
   logger.info("Search response");
   logger.info(sr.toString());
   Iterator<SearchHit> it = sr.getHits().iterator();
   while (it.hasNext()) {
     SearchHit hit = it.next();
     hit.getSource();
   }
   return Arrays.asList("a", "b", "c");
 }
예제 #7
0
 public Iterator<Blog> search(String keyword, int start, int size) {
   SearchRequestBuilder builder = new SearchRequestBuilder(factory.client());
   builder.setIndices(Constants.INDEX).setTypes(Constants.TYPE);
   builder.setSearchType(SearchType.DEFAULT);
   builder.setFrom(start).setSize(size).setExplain(IS_EXPLAIN);
   BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery().minimumNumberShouldMatch(1);
   queryBuilder.should(QueryBuilders.matchPhraseQuery("title", keyword));
   queryBuilder.should(QueryBuilders.matchPhraseQuery("content", keyword));
   queryBuilder.should(QueryBuilders.matchPhraseQuery("createPlace", keyword));
   builder.setQuery(queryBuilder.toString());
   logger.info("Search qeury");
   logger.info(builder.toString());
   SearchResponse sr = builder.execute().actionGet();
   logger.info("Search response");
   logger.info(sr.toString());
   try {
     return new IteratorImpl<Blog>(
         sr.getHits().iterator(), Blog.class.getConstructor(SearchHit.class));
   } catch (NoSuchMethodException | SecurityException e) {
     throw new RuntimeException(e);
   }
 }
  /**
   * Get all active rivers
   *
   * @return
   */
  public List<T> get() {
    if (logger.isDebugEnabled()) logger.debug("get()");
    List<T> rivers = new ArrayList<T>();

    SearchRequestBuilder srb = new SearchRequestBuilder(client);

    try {
      srb.setIndices(SMDSearchProperties.ES_META_INDEX);
      srb.setTypes(SMDSearchProperties.ES_META_RIVERS);

      SearchResponse response = srb.execute().actionGet();

      if (response.getHits().totalHits() > 0) {

        for (int i = 0; i < response.getHits().hits().length; i++) {
          T river = buildInstance();

          SearchHit hit = response.getHits().hits()[i];

          // We only manage rivers for type getHelper().type()
          river = getHelper().toRiver(river, hit.sourceAsMap());

          if (river.getType().equals(getHelper().type())) {
            // For each river, we check if the river is started or not
            river.setStart(riverService.checkState(river));
            rivers.add(river);
          }
        }
      }

    } catch (IndexMissingException e) {
      // That's a common use case. We started with an empty index
    }

    if (logger.isDebugEnabled()) logger.debug("/get()={}", rivers);
    return rivers;
  }