Java Code Examples for org.elasticsearch.search.SearchHits#getTotalHits()

The following examples show how to use org.elasticsearch.search.SearchHits#getTotalHits() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EsQuery.java    From AsuraFramework with Apache License 2.0 6 votes vote down vote up
public EsResponse queryByEsQueryDo(EsQueryDo esQueryObj) throws EsException {
    validationEsQuery(esQueryObj.getIndexName(),esQueryObj.getTypeName());
    //创建ES查询Request对象
    SearchRequestBuilder esSearch=buildSearchRequest (esQueryObj);
    //执行查询
    SearchResponse response =esSearch.execute().actionGet();
    JSONObject resObj = new JSONObject();
    //获取facet结果
    if(!Check.NuNObject(esQueryObj.aggregationFields())){
        parseAggregationResult(response, esQueryObj.aggregationFields(), resObj);
    }
    //1、获取搜索的文档结果
    SearchHits searchHits = response.getHits();
    if (searchHits == null || searchHits.getTotalHits() == 0) {
        return EsResponse.responseOK(null);
    }
    SearchHit[] hits = searchHits.getHits();
    resObj.put("total", searchHits.getTotalHits());
    //1.1、获取搜索结果
    parseSearchResult(hits, esQueryObj.isHighLigth(), esQueryObj, resObj);
    return EsResponse.responseOK(resObj);
}
 
Example 2
Source File: RwCrawlerThread.java    From elasticsearch-river-web with Apache License 2.0 6 votes vote down vote up
@Override
protected boolean isContentUpdated(final CrawlerClient client, final UrlQueue<?> urlQueue) {
    final RiverConfigManager riverConfigManager = SingletonLaContainer.getComponent(RiverConfigManager.class);
    final RiverConfig riverConfig = riverConfigManager.get(crawlerContext.getSessionId());
    if (riverConfig.isIncremental()) {
        final EsClient esClient = SingletonLaContainer.getComponent(EsClient.class);
        try {
            final SearchResponse response = esClient.prepareSearch(riverConfig.getIndex()).setTypes(riverConfig.getType())
                    .setQuery(QueryBuilders.termQuery("url", urlQueue.getUrl())).addField("lastModified")
                    .addSort("lastModified", SortOrder.DESC).execute().actionGet();
            final SearchHits hits = response.getHits();
            if (hits.getTotalHits() > 0) {
                final SearchHitField lastModifiedField = hits.getAt(0).getFields().get("lastModified");
                if (lastModifiedField != null) {
                    final Date lastModified = ConversionUtil.convert(lastModifiedField.getValue(), Date.class);
                    if (lastModified != null) {
                        urlQueue.setLastModified(lastModified.getTime());
                    }
                }
            }
        } catch (final Exception e) {
            logger.debug("Failed to retrieve lastModified.", e);
        }
    }
    return super.isContentUpdated(client, urlQueue);
}
 
Example 3
Source File: ElasticsearchSearchQueryBase.java    From vertexium with Apache License 2.0 6 votes vote down vote up
protected <T> ElasticsearchGraphQueryIterable<T> createIterable(
    SearchResponse response,
    QueryParameters filterParameters,
    Iterable<T> vertexiumObjects,
    long searchTimeInMillis,
    SearchHits hits
) {
    return new ElasticsearchGraphQueryIterable<>(
        this,
        response,
        filterParameters,
        vertexiumObjects,
        hits.getTotalHits(),
        searchTimeInMillis * 1000000,
        hits
    );
}
 
Example 4
Source File: ElasticsearchSearchQueryBase.java    From vertexium with Apache License 2.0 6 votes vote down vote up
protected <T> ElasticsearchGraphQueryIterable<T> createIterable(
    SearchResponse response,
    QueryParameters filterParameters,
    Iterable<T> vertexiumObjects,
    long searchTimeInMillis,
    SearchHits hits
) {
    return new ElasticsearchGraphQueryIterable<>(
        this,
        response,
        filterParameters,
        vertexiumObjects,
        hits.getTotalHits().value,
        searchTimeInMillis * 1000000,
        hits
    );
}
 
Example 5
Source File: ElasticSearchHelper.java    From sunbird-lms-service with MIT License 6 votes vote down vote up
/**
 * Method returns map which contains all the request data from elasticsearch
 *
 * @param SearchResponse response from elastic search
 * @param searchDTO searchDTO which was used to search data
 * @param finalFacetList Facets provide aggregated data based on a search query
 * @return Map which will have all the requested data
 */
public static Map<String, Object> getSearchResponseMap(
    SearchResponse response, SearchDTO searchDTO, List finalFacetList) {
  Map<String, Object> responseMap = new HashMap<>();
  List<Map<String, Object>> esSource = new ArrayList<>();
  long count = 0;
  if (response != null) {
    SearchHits hits = response.getHits();
    count = hits.getTotalHits();

    for (SearchHit hit : hits) {
      esSource.add(hit.getSourceAsMap());
    }

    // fetch aggregations aggregations
    finalFacetList = getFinalFacetList(response, searchDTO, finalFacetList);
  }
  responseMap.put(JsonKey.CONTENT, esSource);
  if (!(finalFacetList.isEmpty())) {
    responseMap.put(JsonKey.FACETS, finalFacetList);
  }
  responseMap.put(JsonKey.COUNT, count);
  return responseMap;
}
 
Example 6
Source File: Generator.java    From elasticsearch-report-engine with GNU General Public License v3.0 6 votes vote down vote up
/**
 * @param response
 * @return
 */
@SuppressWarnings("unchecked")
public List<Map> extractData(SearchResponse response)
    throws NoDataFoundException, ReportGenerationException {
  List<Map> data = new LinkedList<>();
  SearchHits hits = response.getHits();
  if (response.getShardFailures().length > 0) {
    throw new ReportGenerationException("Report failed to get data. Kindly try again.");
  }
  if (hits.getTotalHits() == 0) {
    throw new NoDataFoundException("No data found");
  }
  try {
    for (SearchHit hit : hits) {
      Map<String, Object> sourceMap = hit.getSourceAsMap();
      data.add(sourceMap);
    }
  } catch (Exception e) {
    throw new NoDataFoundException("Error extracting data : " + e.getMessage());
  }
  return data;
}
 
Example 7
Source File: HighlightResultMapper.java    From code with Apache License 2.0 6 votes vote down vote up
@Override
public <T> AggregatedPage<T> mapResults(SearchResponse searchResponse, Class<T> clazz, Pageable pageable) {
    SearchHits hits = searchResponse.getHits();
    List<T> result = new ArrayList<>((int) hits.getTotalHits());
    for (SearchHit searchHit : hits) {
        // 高亮 K,V
        Map<String, HighlightField> highlightFields = searchHit.getHighlightFields();
        // 使用反射比较通用一些,也可以直接 new 对象,把值set进去
        try {
            T document = objectMapper.readValue(searchHit.getSourceAsString(), clazz);
            for (Map.Entry<String, HighlightField> entry : highlightFields.entrySet()) {
                Field field = clazz.getField(entry.getKey());
                field.setAccessible(true);
                field.set(document, entry.getValue().fragments()[0].toString());
            }
            result.add(document);
        } catch (IllegalAccessException | IOException | NoSuchFieldException e) {
            e.printStackTrace();
        }

    }
    return new AggregatedPageImpl<>(result, pageable, hits.getTotalHits());
}
 
Example 8
Source File: HighLevelRestController.java    From ProjectStudy with MIT License 5 votes vote down vote up
/**
   * 列表查询
   *
   * @param page
* @param rows
* @param keyword
   * @return com.example.common.ResponseBean
   * @throws
   * @author wliduo[[email protected]]
   * @date 2019/8/15 16:01
   */
  @GetMapping("/book")
  public ResponseBean list(@RequestParam(defaultValue = "1") Integer page,
                           @RequestParam(defaultValue = "10") Integer rows,
                           String keyword) throws IOException {
      SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
      // 分页采用简单的from + size分页,适用数据量小的,了解更多分页方式可自行查阅资料
      searchSourceBuilder.from((page - 1) * rows);
      searchSourceBuilder.size(rows);
      // 查询条件,只有查询关键字不为空才带查询条件
      if (StringUtils.isNoneBlank(keyword)) {
          QueryBuilder queryBuilder = QueryBuilders.multiMatchQuery(keyword, "name", "desc");
          searchSourceBuilder.query(queryBuilder);
      }
      // 排序,根据ID倒叙
      searchSourceBuilder.sort("id", SortOrder.DESC);
      // SearchRequest
      SearchRequest searchRequest = new SearchRequest();
      searchRequest.source(searchSourceBuilder);
      // 查询ES
      SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
      SearchHits hits = searchResponse.getHits();
      // 获取总数
      Long total = hits.getTotalHits().value;
      // 遍历封装列表对象
      List<BookDto> bookDtoList = new ArrayList<>();
      SearchHit[] searchHits = hits.getHits();
      for (SearchHit searchHit : searchHits) {
          bookDtoList.add(JSON.parseObject(searchHit.getSourceAsString(), BookDto.class));
      }
      // 封装Map参数返回
      Map<String, Object> result = new HashMap<String, Object>(16);
      result.put("count", total);
      result.put("data", bookDtoList);
      return new ResponseBean(HttpStatus.OK.value(), "查询成功", result);
  }
 
Example 9
Source File: SearchBuilder.java    From microservices-platform with Apache License 2.0 5 votes vote down vote up
/**
 * 返回分页结果 PageResult<JSONObject>
 * @param page 当前页数
 * @param limit 每页显示
 */
public PageResult<JSONObject> getPage(Integer page, Integer limit) throws IOException {
    this.setPage(page, limit);
    SearchResponse response = this.get();
    SearchHits searchHits = response.getHits();
    long totalCnt = searchHits.getTotalHits();
    List<JSONObject> list = getList(searchHits);
    return PageResult.<JSONObject>builder().data(list).code(0).count(totalCnt).build();
}
 
Example 10
Source File: SearchResult.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
public SearchResult(SearchResponse resp) {
	SearchHits hits = resp.getHits();
	this.total = hits.getTotalHits().value;
	results = new ArrayList<>(hits.getHits().length);
	for (SearchHit searchHit : hits.getHits()) {
		if (searchHit.getSourceAsMap() != null) {
			results.add(searchHit.getSourceAsMap());
		} else if (searchHit.getFields() != null) {
			Map<String, DocumentField> fields = searchHit.getFields();
			results.add(toFieldsMap(fields));
		}

	}
}
 
Example 11
Source File: HighLevelRestController.java    From ProjectStudy with MIT License 5 votes vote down vote up
/**
   * 列表查询
   *
   * @param page
* @param rows
* @param keyword
   * @return com.example.common.ResponseBean
   * @throws
   * @author wliduo[[email protected]]
   * @date 2019/8/15 16:01
   */
  @GetMapping("/book")
  public ResponseBean list(@RequestParam(defaultValue = "1") Integer page,
                           @RequestParam(defaultValue = "10") Integer rows,
                           String keyword) throws IOException {
      SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
      // 分页采用简单的from + size分页,适用数据量小的,了解更多分页方式可自行查阅资料
      searchSourceBuilder.from((page - 1) * rows);
      searchSourceBuilder.size(rows);
      // 查询条件,只有查询关键字不为空才带查询条件
      if (StringUtils.isNoneBlank(keyword)) {
          QueryBuilder queryBuilder = QueryBuilders.multiMatchQuery(keyword, "name", "content", "describe");
          searchSourceBuilder.query(queryBuilder);
      }
      // 排序,根据ID倒叙
      // searchSourceBuilder.sort("id", SortOrder.DESC);
      // 使用unmappedType,解决排序引起的all shards failed问题,详细请自行查阅资料
      FieldSortBuilder fieldSortBuilder = SortBuilders.fieldSort("id").order(SortOrder.DESC).unmappedType("text");
      searchSourceBuilder.sort(fieldSortBuilder);
      // SearchRequest
      SearchRequest searchRequest = new SearchRequest();
      searchRequest.source(searchSourceBuilder);
      // 查询ES
      SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
      SearchHits hits = searchResponse.getHits();
      // 获取总数
      Long total = hits.getTotalHits().value;
      // 遍历封装列表对象
      List<BookDto> bookDtoList = new ArrayList<>();
      SearchHit[] searchHits = hits.getHits();
      for (SearchHit searchHit : searchHits) {
          bookDtoList.add(JSON.parseObject(searchHit.getSourceAsString(), BookDto.class));
      }
      // 封装Map参数返回
      Map<String, Object> result = new HashMap<String, Object>(16);
      result.put("count", total);
      result.put("data", bookDtoList);
      return new ResponseBean(HttpStatus.OK.value(), "查询成功", result);
  }
 
Example 12
Source File: ItemByEsServiceImpl.java    From springBoot with MIT License 4 votes vote down vote up
@Override
public Result<PageInfo<ItemByEsVo>> searchByNative(ItemByEsVo itemByEsVo) {
    // 统计查询时间,这里开始
    Instant start = Instant.now();

    // 构造查询条件
    QueryBuilder matchQuery = QueryBuilders.matchQuery("title", itemByEsVo.getTitle())
            .analyzer("standard") //分词器
            .operator(Operator.OR);//or查询 Operator.OR、and查询Operator.AND

    // 设置高亮,使用默认的highlighter高亮器
    HighlightBuilder highlightBuilder = new HighlightBuilder()
            .field("title") //需要高亮的域(字段)
            .preTags("<span style=\"color:red;\">") //前缀
            .postTags("</span>"); //后缀

    // 设置查询字段
    SearchResponse response = client.prepareSearch("book")
            .setQuery(matchQuery)
            .highlighter(highlightBuilder)
            // 设置一次返回的文档数量
            .setSize(10)
            .get();

    // 返回搜索结果
    SearchHits hits = response.getHits();

    // 统计搜索结束时间
    Instant end = Instant.now();

    ArrayList novel = new ArrayList();
    for (int i = 0; i < hits.getTotalHits(); i++) {
        // 得到SearchHit对象
        SearchHit hit = hits.getAt(i);
        // 遍历结果,使用HashMap存放
        LinkedHashMap map = new LinkedHashMap();
        map.put("Source As String", hit.getSourceAsString());
        // 返回String格式的文档结果
        System.out.println("Source As String:" + hit.getSourceAsString());
        map.put("Source As Map", hit.getSourceAsMap());
        // 返回Map格式的文档结果
        System.out.println("Source As Map:" + hit.getSourceAsMap());
        // 返回文档所在的索引
        map.put("Index", hit.getIndex());
        System.out.println("Index:" + hit.getIndex());
        // 返回文档所在的类型
        map.put("Type", hit.getType());
        System.out.println("Type:" + hit.getType());
        // 返回文档所在的ID编号
        map.put("Id", hit.getId());
        System.out.println("Id:" + hit.getId());
        // 返回指定字段的内容,例如这里返回完整的title的内容
        map.put("Title", hit.getSourceAsMap().get("title"));
        System.out.println("title: " + hit.getSourceAsMap().get("title"));
        // 返回文档的评分
        map.put("Scope", hit.getScore());
        System.out.println("Scope:" + hit.getScore());
        // 返回文档的高亮字段
        Text[] text = hit.getHighlightFields().get("title").getFragments();
        StringBuilder hight = new StringBuilder();
        if (text != null) {
            for (Text str : text) {
                hight.append(str);
                System.out.println(str.toString());
            }
        }
        map.put("Highlight", hight.toString());
        novel.add(map);
    }

    System.out.println(novel);
    System.out.println("共查出"+hits.getTotalHits()+"条记录!");
    System.out.println("共耗时"+ Duration.between(start, end).toMillis()+"ms");
    return null;
}
 
Example 13
Source File: SearchApiMain.java    From elasticsearch-pool with Apache License 2.0 4 votes vote down vote up
public static void searchApi() throws IOException {

        RestHighLevelClient client = HighLevelClient.getInstance();

        try {
            SearchRequest searchRequest = new SearchRequest("jingma2_test");//限定index
            searchRequest.types("testlog");//限定type

            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            /*查询所有记录*/

//        searchSourceBuilder.query(QueryBuilders.matchAllQuery());


            /*根据匹配查询*/
            QueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("name", "风雷");
            /*设置中文分词器*/
//            ((MatchQueryBuilder) matchQueryBuilder).analyzer("ik");
//            ((MatchQueryBuilder) matchQueryBuilder).analyzer("ik_max_word");
//            ((MatchQueryBuilder) matchQueryBuilder).analyzer("ik_smart");
//            ((MatchQueryBuilder) matchQueryBuilder).analyzer("standard");
            searchSourceBuilder.query(matchQueryBuilder);

            /*限定查询条件和查询条数*/
//            searchSourceBuilder.query(QueryBuilders.termQuery("name", "风雷"));
            searchSourceBuilder.from(0);
            searchSourceBuilder.size(5);
//            searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));

            /*限定查询结果排序*/
//            searchSourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC));
//            searchSourceBuilder.sort(new FieldSortBuilder("age").order(SortOrder.ASC));

            searchRequest.source(searchSourceBuilder);

            SearchResponse searchResponse = client.search(searchRequest);
            System.out.println(searchResponse);

            SearchHits hits = searchResponse.getHits();
            long totalHits = hits.getTotalHits();
            float maxScore = hits.getMaxScore();

            SearchHit[] searchHits = hits.getHits();
            for (SearchHit hit : searchHits) {
                String index = hit.getIndex();
                String type = hit.getType();
                String id = hit.getId();
                float score = hit.getScore();
                String sourceAsString = hit.getSourceAsString();
                System.out.println(sourceAsString);
//                Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            }

        }finally {
            HighLevelClient.close();
        }

    }
 
Example 14
Source File: Select.java    From code with Apache License 2.0 4 votes vote down vote up
public void after() throws IOException {
    //3.获取查询结果
    SearchResponse searchResponse = restHighLevelClient.search(searchRequest,
            RequestOptions.DEFAULT);

    // 查询花费时间,单位是毫秒
    TimeValue took = searchResponse.getTook();
    // 分片信息
    int total = searchResponse.getTotalShards();
    int success = searchResponse.getSuccessfulShards();
    int skipped = searchResponse.getSkippedShards();
    int failed = searchResponse.getFailedShards();
    // 搜索结果总览对象
    SearchHits searchHits = searchResponse.getHits();
    // 搜索到的总条数
    long totalHits = searchHits.getTotalHits();
    // 所有结果中文档得分的最高分
    float maxScore = searchHits.getMaxScore();

    System.out.println("took:" + took);
    System.out.println("_shards:");
    System.out.println("        total:" + total);
    System.out.println("        success:" + success);
    System.out.println("        skipped:" + skipped);
    System.out.println("        failed:" + failed);
    System.out.println("hits:");
    System.out.println("        total:" + totalHits);
    System.out.println("        max_score:" + maxScore);
    System.out.println("        hits:");
    // 搜索结果的文档对象数组,每个元素是一条搜索到的文档信息
    SearchHit[] hits = searchHits.getHits();
    for (SearchHit hit : hits) {
        // 索引库
        String index = hit.getIndex();
        // 文档类型
        String type = hit.getType();
        // 文档id
        String id = hit.getId();
        // 文档得分
        float score = hit.getScore();
        // 文档的源数据
        String source = hit.getSourceAsString();
        System.out.println("            _index:" + index);
        System.out.println("            _type:" + type);
        System.out.println("            _id:" + id);
        System.out.println("            _score:" + score);
        System.out.println("            _source:" + source);
    }
    restHighLevelClient.close();
}
 
Example 15
Source File: PostServiceImpl.java    From SENS with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Page<Post> findPostsByEs(Map<String, Object> criteria, Page<Post> page) {

    //search request
    SearchRequest searchRequest = new SearchRequest("blog");

    //search builder
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

    BoolQueryBuilder booleanQueryBuilder = QueryBuilders.boolQuery();
    for (String key : criteria.keySet()) {
        booleanQueryBuilder.must(QueryBuilders.matchQuery(key, criteria.get(key)));
    }

    sourceBuilder.query(booleanQueryBuilder);
    sourceBuilder.from(Integer.parseInt(String.valueOf((page.getCurrent() - 1) * page.getSize())));
    sourceBuilder.size(Integer.parseInt(String.valueOf(page.getSize())));
    sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));

    //sort
    sourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC));

    //highlight
    HighlightBuilder highlightBuilder = new HighlightBuilder();
    HighlightBuilder.Field highlightTitle = new HighlightBuilder.Field("postTitle");
    highlightTitle.preTags("<span class=\"highlight\">");
    highlightTitle.postTags("</span>");
    highlightBuilder.field(highlightTitle);
    sourceBuilder.highlighter(highlightBuilder);

    // add builder into request
    searchRequest.indices("blog");
    searchRequest.source(sourceBuilder);

    //response
    SearchResponse searchResponse = null;
    List<Post> postList = new ArrayList<>();
    try {
        searchResponse = highLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        //search hits
        SearchHits hits = searchResponse.getHits();
        long totalHits = hits.getTotalHits();

        SearchHit[] searchHits = hits.getHits();
        page.setTotal((int) totalHits);
        for (SearchHit hit : searchHits) {
            String str = hit.getSourceAsString();
            Post esPost = JSONObject.parseObject(str, Post.class);
            //高亮
            Map<String, HighlightField> highlightFields = hit.getHighlightFields();
            HighlightField highlight = highlightFields.get("postTitle");
            if (highlight != null) {
                Text[] fragments = highlight.fragments();
                String fragmentString = fragments[0].string();
                esPost.setPostTitle(fragmentString);
            }
            postList.add(esPost);
        }
    } catch (Exception e) {
        e.printStackTrace();
        log.error("ES未开启: {}", e);
    }
    return page.setRecords(postList);
}
 
Example 16
Source File: ElasticsearchDataModel.java    From elasticsearch-taste with Apache License 2.0 4 votes vote down vote up
@Override
public Float getPreferenceValue(final long userID, final long itemID) {
    if (cache != null) {
        final DmValue dmValue = cache.getIfPresent(DmKey.key(
                DmKey.PREFERENCE_VALUE, userID, itemID));
        if (dmValue != null) {
            return dmValue.getValue();
        }
    }

    SearchResponse response;
    try {
        response = client.prepareSearch(preferenceIndex)
                .setTypes(preferenceType)
                .setQuery(QueryBuilders.boolQuery()
                        .must(QueryBuilders.termQuery(itemIdField, itemID))
                        .must(QueryBuilders.termQuery(userIdField, userID))
                        .filter(getLastAccessedFilterQuery()))
                .addFields(valueField)
                .addSort(timestampField, SortOrder.DESC).setSize(1)
                .execute().actionGet();
    } catch (final ElasticsearchException e) {
        throw new TasteException("Failed to get the preference by ("
                + userID + "," + itemID + ")", e);
    }

    final SearchHits hits = response.getHits();
    final long totalHits = hits.getTotalHits();
    if (totalHits == 0) {
        return null;
    } else if (totalHits > 1) {
        logger.warn(
                "ItemID {} of UserID {} has {} preferences. Use the latest value.",
                itemID, userID, totalHits);
    }

    final SearchHit[] searchHits = hits.getHits();
    if (searchHits.length > 0) {
        final SearchHitField result = searchHits[0].field(valueField);
        if (result != null) {
            final Number value = result.getValue();
            final float floatValue = value.floatValue();
            if (cache != null) {
                cache.put(DmKey.create(DmKey.PREFERENCE_VALUE, userID,
                        itemID), new DmValue(floatValue, 16));
            }
            return floatValue;
        }
    }

    return null;
}
 
Example 17
Source File: ElasticsearchDataModel.java    From elasticsearch-taste with Apache License 2.0 4 votes vote down vote up
@Override
public Long getPreferenceTime(final long userID, final long itemID) {
    if (cache != null) {
        final DmValue dmValue = cache.getIfPresent(DmKey.key(
                DmKey.PREFERENCE_TIME, userID, itemID));
        if (dmValue != null) {
            return dmValue.getValue();
        }
    }

    SearchResponse response;
    try {
        response = client
                .prepareSearch(preferenceIndex)
                .setTypes(preferenceType)
                .setQuery(QueryBuilders.boolQuery()
                        .must(QueryBuilders.termQuery(itemIdField, itemID))
                        .must(QueryBuilders.termQuery(userIdField, userID))
                        .filter(getLastAccessedFilterQuery()))
                .addFields(timestampField)
                .addSort(timestampField, SortOrder.DESC).setSize(1)
                .execute().actionGet();
    } catch (final ElasticsearchException e) {
        throw new TasteException("Failed to get the timestamp by ("
                + userID + "," + itemID + ")", e);
    }

    final SearchHits hits = response.getHits();
    final long totalHits = hits.getTotalHits();
    if (totalHits == 0) {
        return null;
    } else if (totalHits > 1) {
        logger.warn(
                "ItemID {} of UserID {} has {} preferences. Use the latest value.",
                itemID, userID, totalHits);
    }

    final SearchHit[] searchHits = hits.getHits();
    if (searchHits.length > 0) {
        final SearchHitField result = searchHits[0].field(timestampField);
        if (result != null) {
            final Date date = result.getValue();
            final long time = date.getTime();
            if (cache != null) {
                cache.put(
                        DmKey.create(DmKey.PREFERENCE_TIME, userID, itemID),
                        new DmValue(time, 16));
            }
            return time;
        }
    }

    return null;
}
 
Example 18
Source File: ActionHandler.java    From elasticsearch-taste with Apache License 2.0 4 votes vote down vote up
protected long[] getTargetIDs(final String index, final String type,
        final String fieldName, final String targetName) {
    final Map<String, Object> userSettings = SettingsUtils.get(
            rootSettings, targetName);
    final String userQuery = SettingsUtils.get(userSettings, "query");
    if (StringUtils.isBlank(userQuery)) {
        return null;
    }
    final Number size = SettingsUtils.get(userSettings, "size", 1000);
    final Number keepAlive = SettingsUtils.get(userSettings, "keep_alive",
            60000); //1min

    int count = 0;
    long[] targetIDs = null;
    SearchResponse response = null;
    while (true) {
        if (response == null) {
            response = client.prepareSearch(index).setTypes(type)
                    .setScroll(new TimeValue(keepAlive.longValue()))
                    .setQuery(QueryBuilders.queryStringQuery(userQuery))
                    .addField(fieldName).setSize(size.intValue()).execute()
                    .actionGet();
        } else {
            response = client.prepareSearchScroll(response.getScrollId())
                    .setScroll(new TimeValue(keepAlive.longValue()))
                    .execute().actionGet();
        }
        final SearchHits hits = response.getHits();
        if (targetIDs == null) {
            targetIDs = new long[(int) hits.getTotalHits()];
            if (logger.isDebugEnabled()) {
                logger.debug("{} users are found by {}",
                        hits.getTotalHits(), userQuery);
            }
        }

        if (hits.getHits().length == 0) {
            break;
        }

        for (final SearchHit hit : hits) {
            final SearchHitField searchHitField = hit.getFields().get(
                    fieldName);
            final Number value = searchHitField.getValue();
            targetIDs[count] = value.longValue();
            count++;
        }
    }
    return targetIDs;
}
 
Example 19
Source File: ItemRequestHandler.java    From elasticsearch-taste with Apache License 2.0 4 votes vote down vote up
private void doItemCreation(final Params params,
        final RequestHandler.OnErrorListener listener,
        final Map<String, Object> requestMap,
        final Map<String, Object> paramMap,
        final Map<String, Object> itemMap, final String index,
        final String type, final String itemIdField,
        final String timestampField, final RequestHandlerChain chain) {
    final OnResponseListener<SearchResponse> responseListener = response -> {
        validateRespose(response);

        Number currentId = null;
        final SearchHits hits = response.getHits();
        if (hits.getTotalHits() != 0) {
            final SearchHit[] searchHits = hits.getHits();
            final SearchHitField field = searchHits[0].getFields().get(
                    itemIdField);
            if (field != null) {
                currentId = field.getValue();
            }
        }
        final Long itemId;
        if (currentId == null) {
            itemId = Long.valueOf(1);
        } else {
            itemId = Long.valueOf(currentId.longValue() + 1);
        }
        doItemUpdate(params, listener, requestMap, paramMap, itemMap,
                index, type, itemIdField, timestampField, itemId,
                OpType.CREATE, chain);
    };
    final OnFailureListener failureListener = t -> {
        final List<Throwable> errorList = getErrorList(paramMap);
        if (errorList.size() >= maxRetryCount) {
            listener.onError(t);
        } else {
            sleep(t);
            errorList.add(t);
            doItemIndexExists(params, listener, requestMap, paramMap,
                    chain);
        }
    };
    client.prepareSearch(index).setTypes(type)
            .setQuery(QueryBuilders.matchAllQuery()).addField(itemIdField)
            .addSort(itemIdField, SortOrder.DESC).setSize(1)
            .execute(on(responseListener, failureListener));
}
 
Example 20
Source File: UserRequestHandler.java    From elasticsearch-taste with Apache License 2.0 4 votes vote down vote up
private void doUserCreation(final Params params,
        final RequestHandler.OnErrorListener listener,
        final Map<String, Object> requestMap,
        final Map<String, Object> paramMap,
        final Map<String, Object> userMap, final String index,
        final String type, final String userIdField,
        final String timestampField, final RequestHandlerChain chain) {
    final OnResponseListener<SearchResponse> responseListener = response -> {
        validateRespose(response);

        Number currentId = null;
        final SearchHits hits = response.getHits();
        if (hits.getTotalHits() != 0) {
            final SearchHit[] searchHits = hits.getHits();
            final SearchHitField field = searchHits[0].getFields().get(
                    userIdField);
            if (field != null) {
                currentId = field.getValue();
            }
        }
        final Long userId;
        if (currentId == null) {
            userId = Long.valueOf(1);
        } else {
            userId = Long.valueOf(currentId.longValue() + 1);
        }
        doUserUpdate(params, listener, requestMap, paramMap, userMap,
                index, type, userIdField, timestampField, userId,
                OpType.CREATE, chain);
    };
    final OnFailureListener failureListener = t -> {
        final List<Throwable> errorList = getErrorList(paramMap);
        if (errorList.size() >= maxRetryCount) {
            listener.onError(t);
        } else {
            sleep(t);
            errorList.add(t);
            doUserIndexExists(params, listener, requestMap, paramMap,
                    chain);
        }
    };
    client.prepareSearch(index).setTypes(type)
            .setQuery(QueryBuilders.matchAllQuery()).addField(userIdField)
            .addSort(userIdField, SortOrder.DESC).setSize(1)
            .execute(on(responseListener, failureListener));
}