org.elasticsearch.action.search.SearchType Java Examples

The following examples show how to use org.elasticsearch.action.search.SearchType. 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: ElasticToMongoProvider.java    From mongolastic with MIT License 6 votes vote down vote up
@Override
public List buildJSONContent(int skip, int limit) {

    if (Objects.isNull(response)) {
        response = elastic.getClient().prepareSearch(config.getMisc().getDindex().getName())
                .setTypes(config.getMisc().getCtype().getName())
                .setSearchType(SearchType.QUERY_THEN_FETCH)
                .setScroll(new TimeValue(60000))
                .setSize(limit)
                .execute().actionGet();
    }
    else {
        response = elastic.getClient()
                .prepareSearchScroll(response.getScrollId())
                .setScroll(new TimeValue(60000))
                .execute().actionGet();
    }

    return Arrays.stream(response.getHits().getHits())
            .map(hit -> new Document(hit.getSourceAsMap()))
            .collect(Collectors.toList());
}
 
Example #2
Source File: WebmagicService.java    From javabase with Apache License 2.0 6 votes vote down vote up
public List<ContentBean> search(Model model, String keyWord, Integer from) {
    if (from == null) from = 0;
    model.addAttribute("from", from);
    List<ContentBean> listContentBean = new ArrayList<>();
    SearchResponse response = elasticSearch.getTransportClient().prepareSearch(ElasticSearch.INDEX_NAME)
            .setTypes(ElasticSearch.TIEABA_CONTENT_TYPE)
            .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
            .setQuery(QueryBuilders.matchQuery(ElasticSearch.TIEABA_CONTENT_FIELD, keyWord)
            ).setFrom(from * ConfigConsts.PAGE_SIZE).setSize(ConfigConsts.PAGE_SIZE).execute().actionGet();
    SearchHits hits = response.getHits();
    model.addAttribute("totalSize", hits.getTotalHits());
    for (SearchHit searchHitFields : hits.getHits()) {
        listContentBean.add(JSONObject.parseObject(searchHitFields.getSourceAsString(), ContentBean.class));
    }
    return listContentBean;
}
 
Example #3
Source File: ElasticsearchExtendHighRestFactory.java    From database-transform-tool with Apache License 2.0 6 votes vote down vote up
public String selectTermAll(String indexs,String types,String field,String value){
	try {
		if(StringUtil.isEmpty(indexs))indexs="_all";
		if(xclient==null){
			init();
		}
		SearchSourceBuilder search = new SearchSourceBuilder();
		if(!StringUtil.isEmpty(field)&&!StringUtil.isEmpty(value)&&!(field.matches(regex)||field.matches(value))){
			search.query(QueryBuilders.termQuery(field, value));
		}
		search.aggregation(AggregationBuilders.terms("data").field(field+".keyword"));
		search.explain(false);
		SearchRequest request = new SearchRequest();
		request.searchType(SearchType.DFS_QUERY_THEN_FETCH);
		request.source(search);
		request.indices(indexs.split(","));
		request.types(types.split(","));
		SearchResponse response = xclient.search(request);
		return response.toString();
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return null;
}
 
Example #4
Source File: DefaultMigrationClient.java    From elasticsearch-migration with Apache License 2.0 6 votes vote down vote up
private List<MigrationEntry> getAllMigrations() {
    try {
        final QueryBuilder queryBuilder = QueryBuilders.boolQuery().must(QueryBuilders.termQuery(MigrationEntryMeta.IDENTIFIER_FIELD, identifier));
        final SearchRequest searchRequest = new SearchRequest()
                .indices(MigrationEntryMeta.INDEX)
                .searchType(SearchType.DEFAULT)
                .source(SearchSourceBuilder.searchSource().query(queryBuilder).fetchSource(true).size(1000).sort(MigrationEntryMeta.VERSION_FIELD, SortOrder.ASC));

        final SearchResponse searchResponse = restHighLevelClient.search(searchRequest);
        if (searchResponse.status() == RestStatus.OK) {
            return transformHitsFromEs(searchResponse.getHits(), MigrationEntry.class);
        } else {
            throw new MigrationFailedException("Could not access '" + MigrationEntryMeta.INDEX + "' index. Failures: " + Arrays.asList(searchResponse.getShardFailures()));
        }
    } catch (IOException e) {
        throw new MigrationFailedException("IO Exception during migration", e);
    }
}
 
Example #5
Source File: TransportClient.java    From elasticsearch-jest-example with MIT License 6 votes vote down vote up
private static void matchAllQuery() {
	Client client = createTransportClient();
	SearchResponse searchResponse = client.prepareSearch("book")
			.setQuery(QueryBuilders.matchAllQuery())
			.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
	        // 设置查询数据的位置,分页用
			.setFrom(0)
			// 设置查询结果集的最大条数
			.setSize(60)
			// 设置是否按查询匹配度排序
			.setExplain(true)
			.execute()
			.actionGet();
	SearchHits searchHits = searchResponse.getHits();
	System.out.println("-----------------matchAllQuery---------------------");
	System.out.println("共匹配到:"+searchHits.getTotalHits()+"条记录!");
	SearchHit[] hits = searchHits.getHits();
	for (SearchHit searchHit : hits) {
		Map<String, Object> sourceAsMap = searchHit.sourceAsMap();
		Set<String> keySet = sourceAsMap.keySet();
		for (String string : keySet) {
			System.out.println(string+":"+sourceAsMap.get(string));
		}
		System.out.println();
	}
}
 
Example #6
Source File: ElasticsearchHighRestFactory.java    From database-transform-tool with Apache License 2.0 6 votes vote down vote up
public String selectAll(String indexs,String types,String condition){
	try {
		if(StringUtil.isEmpty(indexs))indexs="_all";
		if(xclient==null){
			init();
		}
		SearchSourceBuilder search = new SearchSourceBuilder();
		search.query(QueryBuilders.queryStringQuery(condition)); 
		search.explain(false);
		SearchRequest request = new SearchRequest();
		request.searchType(SearchType.DFS_QUERY_THEN_FETCH);
		request.source(search);
		request.indices(indexs.split(","));
		request.types(types.split(","));
		SearchResponse response = xclient.search(request);
		return response.toString();
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return null;
}
 
Example #7
Source File: SiteElasticSearchIndexBuilder.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
/**
 * Get all indexed resources for a site
 *
 * @param siteId Site containing indexed resources
 * @return a collection of resource references or an empty collection if no resource was found
 */
protected Collection<String> getResourceNames(String siteId) {
    getLog().debug("Obtaining indexed elements for site: '" + siteId + "'");

    SearchResponse response = client.prepareSearch(indexName)
            .setSearchType(SearchType.QUERY_THEN_FETCH)
            .setQuery(termQuery(SearchService.FIELD_SITEID, siteId))
            .setTypes(indexedDocumentType)
            .setSize(Integer.MAX_VALUE)
            .addFields(SearchService.FIELD_REFERENCE)
            .execute()
            .actionGet();

    Collection<String> resourceNames = new ArrayList<String>();
    for (SearchHit hit : response.getHits().hits()) {
        resourceNames.add(getFieldFromSearchHit(SearchService.FIELD_REFERENCE, hit));
    }
    return resourceNames;

}
 
Example #8
Source File: SearchDemo.java    From javabase with Apache License 2.0 6 votes vote down vote up
private static SearchResponse zkfc(String indexName, String zkType, TransportClient client) throws IOException {
    //返回一个可以执行管理性操作的客户端
    //1) cluster(),产生一个允许从集群中执行action或操作的client;
    //2) indices(),产生一个允许从索引中执行action或操作的client。
    //创建zk分词
    PutMappingRequest mapping = Requests.putMappingRequest(indexName).type(zkType).source(createIKMapping(zkType).string());
    client.admin().indices().putMapping(mapping).actionGet();
    Goods goodsOne= new Goods( 1,"iphone7 iphone7plus 钢化膜 玻璃膜 苹果 苹果7/7plus 贴膜 买就送清水","http://m.ule.com/item/detail/1771161");
    Goods goodsTwo=new Goods( 2,"苹果 (Apple) iPhone 7 移动联通电信4G手机 土豪金 32G 标配","http://m.ule.com/item/detail/1799356");
    Goods goodsThree=new Goods( 3,"苹果 Apple iPhone 7 (A1660) 128G 金色 移动联通电信 全网通 4G手机","http://m.ule.com/item/detail/1781429");
    client.prepareIndex(indexName,zkType).setId(1+"").setSource(JSONObject.toJSONString(goodsOne)).execute().actionGet();
    client.prepareIndex(indexName,zkType).setId(2+"").setSource(JSONObject.toJSONString(goodsTwo)).execute().actionGet();
    client.prepareIndex(indexName,zkType).setId(3+"").setSource(JSONObject.toJSONString(goodsThree)).execute().actionGet();

    SearchResponse response = client.prepareSearch(indexName)
            .setTypes(zkType)
            .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
            .setQuery( QueryBuilders.matchQuery("title", "苹果")
            ).execute().actionGet();
    return response;
}
 
Example #9
Source File: ESSearchTest.java    From summerframework with Apache License 2.0 6 votes vote down vote up
@Test
public void highLightResultSet() {
    HighLight highLight = new HighLight();
    HighlightBuilder hBuilder = new HighlightBuilder();
    hBuilder.preTags("<h2>");
    hBuilder.postTags("</h2>");
    hBuilder.field("productName");

    queryPair.setFieldNames(new String[] {"productName"});
    queryPair.setContent("*");
    queryCondition.setSearchType(SearchType.QUERY_THEN_FETCH);
    queryCondition
        .setQueryBuilder(QueryBuilders.wildcardQuery(queryPair.getFieldNames()[0], queryPair.getContent()));

    highLight.setBuilder(hBuilder);
    highLight.setField("productName");

    List<Map<String, Object>> sourceList =
        elasticsearchTemplate.highLightResultSet(esBasicInfo, queryCondition, highLight);

    assertThat(sourceList.size(), is(3));
    assertThat((String)sourceList.get(0).get("productName"), containsString("运"));
    assertThat((String)sourceList.get(1).get("productName"), containsString("android"));
    assertThat((String)sourceList.get(2).get("productName"), containsString("华"));
}
 
Example #10
Source File: ElasticsearchAnySearchDAO.java    From syncope with Apache License 2.0 6 votes vote down vote up
private SearchRequest searchRequest(
        final Set<String> adminRealms,
        final SearchCond cond,
        final AnyTypeKind kind,
        final int from,
        final int size,
        final List<SortBuilder<?>> sortBuilders) {

    Pair<DisMaxQueryBuilder, Set<String>> filter = adminRealmsFilter(adminRealms);
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().
            query(SyncopeConstants.FULL_ADMIN_REALMS.equals(adminRealms)
                    ? getQueryBuilder(cond, kind)
                    : QueryBuilders.boolQuery().
                            must(filter.getLeft()).
                            must(getQueryBuilder(buildEffectiveCond(cond, filter.getRight()), kind))).
            from(from).
            size(size);
    sortBuilders.forEach(sourceBuilder::sort);

    return new SearchRequest(ElasticsearchUtils.getContextDomainName(AuthContextUtils.getDomain(), kind)).
            searchType(SearchType.QUERY_THEN_FETCH).
            source(sourceBuilder);
}
 
Example #11
Source File: InternalEsClient.java    From io with Apache License 2.0 6 votes vote down vote up
/**
 * 非同期でドキュメントを検索. <br />
 * Queryの指定方法をMapで直接記述せずにQueryBuilderにするため、非推奨とする.
 * @param index インデックス名
 * @param routingId routingId
 * @param query クエリ情報
 * @return 非同期応答
 */
public ActionFuture<SearchResponse> asyncSearch(
        String index,
        String routingId,
        Map<String, Object> query) {
    SearchRequest req = new SearchRequest(index).searchType(SearchType.DEFAULT);
    if (query != null) {
        req.source(query);
    }
    if (routingFlag) {
        req = req.routing(routingId);
    }
    ActionFuture<SearchResponse> ret = esTransportClient.search(req);
    this.fireEvent(Event.afterRequest, index, null, null, JSONObject.toJSONString(query), "Search");
    return ret;
}
 
Example #12
Source File: ESConnector.java    From Siamese with GNU General Public License v3.0 6 votes vote down vote up
public long getMaxId(String index, boolean isDFS) throws Exception {
	SearchType searchType;

	if (isDFS)
		searchType = SearchType.DFS_QUERY_THEN_FETCH;
	else
		searchType = SearchType.QUERY_THEN_FETCH;

	SearchResponse response = client.prepareSearch(index).setSearchType(searchType)
			.addSort(SortBuilders.fieldSort("_score").order(SortOrder.DESC))
			.addSort(SortBuilders.fieldSort("file").order(SortOrder.DESC))
			.setQuery(QueryBuilders.matchAllQuery())
			.addAggregation(AggregationBuilders.max("max_id").field("id"))
			.execute()
			.actionGet();

	Double maxId = (Double) response.getAggregations().get("max_id").getProperty("value");

	return maxId.longValue();
}
 
Example #13
Source File: InternalEsClient.java    From io with Apache License 2.0 6 votes vote down vote up
/**
 * 非同期でドキュメントを検索.
 * @param index インデックス名
 * @param type タイプ名
 * @param routingId routingId
 * @param query クエリ情報
 * @return 非同期応答
 */
public ActionFuture<SearchResponse> asyncSearch(
        String index,
        String type,
        String routingId,
        Map<String, Object> query) {
    SearchRequest req = new SearchRequest(index).types(type).searchType(SearchType.DEFAULT);
    if (query != null) {
        req.source(query);
    }
    if (routingFlag) {
        req = req.routing(routingId);
    }
    ActionFuture<SearchResponse> ret = esTransportClient.search(req);
    this.fireEvent(Event.afterRequest, index, type, null, JSONObject.toJSONString(query), "Search");
    return ret;
}
 
Example #14
Source File: ElasticsearchClient.java    From yacy_grid_mcp with GNU Lesser General Public License v2.1 6 votes vote down vote up
private int deleteByQueryInternal(String indexName, final QueryBuilder q) {
    Map<String, String> ids = new TreeMap<>();
    SearchRequestBuilder request = elasticsearchClient.prepareSearch(indexName);
    request
        .setSearchType(SearchType.QUERY_THEN_FETCH)
        .setScroll(scrollKeepAlive)
        .setQuery(q)
        .setSize(100);
    SearchResponse response = request.execute().actionGet();
    while (true) {
        // accumulate the ids here, don't delete them right now to prevent an interference of the delete with the
        // scroll
        for (SearchHit hit : response.getHits().getHits()) {
            ids.put(hit.getId(), hit.getType());
        }
        // termination
        if (response.getHits().getHits().length == 0) break;
        // scroll
        response = elasticsearchClient.prepareSearchScroll(response.getScrollId()).setScroll(scrollKeepAlive).execute().actionGet();
    }
    return deleteBulk(indexName, ids);
}
 
Example #15
Source File: ElasticSearchUtilImpl.java    From metacat with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public List<TableDto> simpleSearch(final String searchString) {
    final List<TableDto> result = Lists.newArrayList();
    final SearchResponse response = client.prepareSearch(esIndex)
        .setTypes(ElasticSearchDoc.Type.table.name())
        .setSearchType(SearchType.QUERY_THEN_FETCH)
        .setQuery(QueryBuilders.termQuery("_all", searchString))
        .setSize(Integer.MAX_VALUE)
        .execute()
        .actionGet(esCallTimeout);
    if (response.getHits().getHits().length != 0) {
        result.addAll(parseResponse(response, TableDto.class));
    }
    return result;
}
 
Example #16
Source File: ElasticSearchUtilImpl.java    From metacat with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public List<String> getIdsByQualifiedName(final String type, final QualifiedName qualifiedName) {
    List<String> result = Lists.newArrayList();
    // Run the query and get the response.
    final QueryBuilder queryBuilder = QueryBuilders.boolQuery()
        .must(QueryBuilders.termQuery("name.qualifiedName.tree", qualifiedName))
        .must(QueryBuilders.termQuery("deleted_", false));
    final SearchRequestBuilder request = client.prepareSearch(esIndex)
        .setTypes(type)
        .setSearchType(SearchType.QUERY_THEN_FETCH)
        .setQuery(queryBuilder)
        .setSize(Integer.MAX_VALUE)
        .setFetchSource(false);
    final SearchResponse response = request.execute().actionGet(esCallTimeout);
    if (response.getHits().getHits().length != 0) {
        result = getIds(response);
    }
    return result;
}
 
Example #17
Source File: ElasticSearchUtilImpl.java    From metacat with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public List<String> getTableIdsByCatalogs(final String type, final List<QualifiedName> qualifiedNames,
                                          final List<QualifiedName> excludeQualifiedNames) {
    List<String> ids = Lists.newArrayList();
    final QueryBuilder queryBuilder = QueryBuilders.boolQuery()
        .must(QueryBuilders.termsQuery("name.qualifiedName.tree", qualifiedNames))
        .must(QueryBuilders.termQuery("deleted_", false))
        .mustNot(QueryBuilders.termsQuery("name.qualifiedName.tree", excludeQualifiedNames));

    // Run the query and get the response.
    final SearchRequestBuilder request = client.prepareSearch(esIndex)
        .setTypes(type)
        .setSearchType(SearchType.QUERY_THEN_FETCH)
        .setQuery(queryBuilder)
        .setSize(Integer.MAX_VALUE)  // TODO May break if too many tables returned back, change to Scroll
        .setFetchSource(false);
    final SearchResponse response = request.execute().actionGet(esCallTimeout);
    if (response.getHits().getHits().length != 0) {
        ids = getIds(response);
    }
    return ids;
}
 
Example #18
Source File: ConsoleHistoryManager.java    From foxtrot with Apache License 2.0 6 votes vote down vote up
private void deleteOldData(final String name) {
    String updatedAt = "updatedAt";
    try {
        SearchHits searchHits = connection.getClient()
                .prepareSearch(INDEX_HISTORY)
                .setTypes(TYPE)
                .setSearchType(SearchType.QUERY_THEN_FETCH)
                .setQuery(QueryBuilders.termQuery("name.keyword", name))
                .addSort(SortBuilders.fieldSort(updatedAt)
                                 .order(SortOrder.DESC))
                .setFrom(10)
                .setSize(9000)
                .execute()
                .actionGet()
                .getHits();
        for(SearchHit searchHit : CollectionUtils.nullAndEmptySafeValueList(searchHits.getHits())) {
            ConsoleV2 consoleV2 = mapper.readValue(searchHit.getSourceAsString(), ConsoleV2.class);
            elasticsearchConsolePersistence.deleteOldVersion(consoleV2.getId());
        }
    } catch (Exception e) {
        throw new ConsoleFetchException(e);
    }
}
 
Example #19
Source File: ElasticSearchUtilImpl.java    From metacat with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public List<String> getTableIdsByUri(final String type, final String dataUri) {
    List<String> ids = Lists.newArrayList();
    // Run the query and get the response.
    if (dataUri != null) {
        final SearchRequestBuilder request = client.prepareSearch(esIndex)
            .setTypes(type)
            .setSearchType(SearchType.QUERY_THEN_FETCH)
            .setQuery(QueryBuilders.termQuery("serde.uri", dataUri))
            .setSize(Integer.MAX_VALUE)
            .setFetchSource(false);
        final SearchResponse response = request.execute().actionGet(esCallTimeout);
        if (response.getHits().getHits().length != 0) {
            ids = getIds(response);
        }
    }
    return ids;
}
 
Example #20
Source File: InternalEsClient.java    From io with Apache License 2.0 6 votes vote down vote up
/**
 * 非同期でドキュメントを検索.
 * @param index インデックス名
 * @param routingId routingId
 * @param query クエリ情報
 * @return 非同期応答
 */
public ActionFuture<SearchResponse> asyncSearch(
        String index,
        String routingId,
        QueryBuilder query) {
    SearchRequest req = new SearchRequest(index).searchType(SearchType.DEFAULT);

    String queryString = "null";
    if (query != null) {
        req.source(new SearchSourceBuilder().query(query));
        queryString = query.buildAsBytes().toUtf8();
    }
    if (routingFlag) {
        req = req.routing(routingId);
    }
    ActionFuture<SearchResponse> ret = esTransportClient.search(req);
    this.fireEvent(Event.afterRequest, index, null, null, queryString, "Search");
    return ret;
}
 
Example #21
Source File: InternalEsClient.java    From io with Apache License 2.0 6 votes vote down vote up
/**
 * 非同期でドキュメントを検索.
 * @param index インデックス名
 * @param routingId routingId
 * @param query クエリ情報
 * @return 非同期応答
 */
public ActionFuture<SearchResponse> asyncSearch(
        String index,
        String routingId,
        QueryBuilder query) {
    SearchRequest req = new SearchRequest(index).searchType(SearchType.DEFAULT);

    String queryString = "null";
    if (query != null) {
        req.source(new SearchSourceBuilder().query(query));
        queryString = query.buildAsBytes().toUtf8();
    }
    if (routingFlag) {
        req = req.routing(routingId);
    }
    ActionFuture<SearchResponse> ret = esTransportClient.search(req);
    this.fireEvent(Event.afterRequest, index, null, null, queryString, "Search");
    return ret;
}
 
Example #22
Source File: ElasticsearchConsolePersistence.java    From foxtrot with Apache License 2.0 6 votes vote down vote up
@Override
public List<ConsoleV2> getAllOldVersions(final String name, final String sortBy) {
    try {
        SearchHits searchHits = connection.getClient()
                .prepareSearch(INDEX_HISTORY)
                .setSearchType(SearchType.QUERY_THEN_FETCH)
                .setQuery(QueryBuilders.termQuery("name.keyword", name))
                .addSort(SortBuilders.fieldSort(sortBy)
                        .order(SortOrder.DESC))
                .setFrom(0)
                .setSize(10)
                .execute()
                .actionGet()
                .getHits();
        List<ConsoleV2> results = new ArrayList<>();
        for (SearchHit searchHit : CollectionUtils.nullAndEmptySafeValueList(searchHits.getHits())) {
            results.add(mapper.readValue(searchHit.getSourceAsString(), ConsoleV2.class));
        }
        return results;
    } catch (Exception e) {
        throw new ConsoleFetchException(e);
    }
}
 
Example #23
Source File: FqlStoreServiceImpl.java    From foxtrot with Apache License 2.0 6 votes vote down vote up
@Override
public List<FqlStore> get(FqlGetRequest fqlGetRequest) {
    SearchHits searchHits;
    List<FqlStore> fqlStoreList = new ArrayList<>();
    try {
        searchHits = elasticsearchConnection.getClient()
                .prepareSearch(FQL_STORE_INDEX)
                .setTypes(DOCUMENT_TYPE_NAME)
                .setQuery(QueryBuilders.prefixQuery(TITLE_FIELD, fqlGetRequest.getTitle()
                        .toLowerCase()))
                .setSearchType(SearchType.QUERY_THEN_FETCH)
                .setFrom(fqlGetRequest.getFrom())
                .setSize(fqlGetRequest.getSize())
                .execute()
                .actionGet()
                .getHits();
        for(SearchHit searchHit : CollectionUtils.nullAndEmptySafeValueList(searchHits.getHits())) {
            fqlStoreList.add(objectMapper.readValue(searchHit.getSourceAsString(), FqlStore.class));
        }
    } catch (Exception e) {
        throw new FqlPersistenceException("Couldn't get FqlStore: " + e.getMessage(), e);
    }
    return fqlStoreList;
}
 
Example #24
Source File: InternalEsClient.java    From io with Apache License 2.0 6 votes vote down vote up
/**
 * 非同期でドキュメントを検索. <br />
 * Queryの指定方法をMapで直接記述せずにQueryBuilderにするため、非推奨とする.
 * @param index インデックス名
 * @param routingId routingId
 * @param query クエリ情報
 * @return 非同期応答
 */
public ActionFuture<SearchResponse> asyncSearch(
        String index,
        String routingId,
        Map<String, Object> query) {
    SearchRequest req = new SearchRequest(index).searchType(SearchType.DEFAULT);
    if (query != null) {
        req.source(query);
    }
    if (routingFlag) {
        req = req.routing(routingId);
    }
    ActionFuture<SearchResponse> ret = esTransportClient.search(req);
    this.fireEvent(Event.afterRequest, index, null, null, JSONObject.toJSONString(query), "Search");
    return ret;
}
 
Example #25
Source File: S3River.java    From es-amazon-s3-river with Apache License 2.0 6 votes vote down vote up
/** Retrieve the ids of files already present into index. */
private List<String> getAlreadyIndexFileIds(){
   List<String> fileIds = new ArrayList<String>();
   // TODO : Should be later optimized for only retrieving ids and getting
   // over the 5000 hits limitation.
   SearchResponse response = client
         .prepareSearch(indexName)
         .setSearchType(SearchType.QUERY_AND_FETCH)
         .setTypes(typeName)
         .setFrom(0)
         .setSize(5000)
         .execute().actionGet();
   if (response.getHits() != null && response.getHits().getHits() != null){
      for (SearchHit hit : response.getHits().getHits()){
         fileIds.add(hit.getId());
      }
   }
   return fileIds;
}
 
Example #26
Source File: ShardSearchLocalRequest.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
protected void innerReadFrom(StreamInput in) throws IOException {
    index = in.readString();
    shardId = in.readVInt();
    searchType = SearchType.fromId(in.readByte());
    numberOfShards = in.readVInt();
    if (in.readBoolean()) {
        scroll = readScroll(in);
    }

    source = in.readBytesReference();
    extraSource = in.readBytesReference();

    types = in.readStringArray();
    filteringAliases = in.readStringArray();
    nowInMillis = in.readVLong();

    templateSource = in.readBytesReference();
    if (in.readBoolean()) {
        template = Template.readTemplate(in);
    }
    requestCache = in.readOptionalBoolean();
}
 
Example #27
Source File: ElasticToMongoProvider.java    From mongolastic with MIT License 6 votes vote down vote up
@Override
public long getCount() {
    long count = 0;
    IndicesAdminClient admin = elastic.getClient().admin().indices();
    IndicesExistsRequestBuilder builder = admin.prepareExists(config.getMisc().getDindex().getName());
    if (builder.execute().actionGet().isExists()) {
        SearchResponse countResponse = elastic.getClient().prepareSearch(config.getMisc().getDindex().getName())
                .setTypes(config.getMisc().getCtype().getName())
                .setSearchType(SearchType.QUERY_THEN_FETCH)
                .setSize(0)
                .execute().actionGet();
        count = countResponse.getHits().getTotalHits();
    } else {
        logger.info("Index/Type does not exist or does not contain the record");
        System.exit(-1);
    }

    logger.info("Elastic Index/Type count: " + count);
    return count;
}
 
Example #28
Source File: ESConnector.java    From Siamese with GNU General Public License v3.0 6 votes vote down vote up
public ArrayList<Document> search(String index, String type, String query, boolean isPrint
			, boolean isDFS, int resultOffset, int resultSize) throws Exception {
        SearchType searchType;

        if (isDFS)
            searchType = SearchType.DFS_QUERY_THEN_FETCH;
        else
            searchType = SearchType.QUERY_THEN_FETCH;

		SearchResponse response = client.prepareSearch(index).setSearchType(searchType)
				.addSort(SortBuilders.fieldSort("_score").order(SortOrder.DESC))
				.addSort(SortBuilders.fieldSort("file").order(SortOrder.DESC))
				.setQuery(QueryBuilders.matchQuery("tokenizedsrc", query))
//				.setQuery(QueryBuilders.matchQuery("src", query))
				.setFrom(resultOffset).setSize(resultSize).execute()
				.actionGet();
		SearchHit[] hits = response.getHits().getHits();

        return prepareResults(hits, resultSize, isPrint);
    }
 
Example #29
Source File: TestMongoToElastic.java    From mongolastic with MIT License 5 votes vote down vote up
public long getCount(ElasticConfiguration elastic, YamlConfiguration config) {
    IndicesAdminClient admin = elastic.getClient().admin().indices();
    IndicesExistsRequestBuilder builder = admin.prepareExists(config.getMisc().getDindex().getAs());
    assertThat(builder.execute().actionGet().isExists(), is(true));

    elastic.getClient().admin().indices().flush(new FlushRequest(config.getMisc().getDindex().getAs())).actionGet();

    SearchResponse response = elastic.getClient().prepareSearch(config.getMisc().getDindex().getAs())
            .setTypes(config.getMisc().getCtype().getAs())
            .setSearchType(SearchType.QUERY_THEN_FETCH)
            .setSize(0)
            .execute().actionGet();
    long count = response.getHits().getTotalHits();
    return count;
}
 
Example #30
Source File: QuestionElasticSearchIndexBuilder.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Override
protected Pair<SearchRequestBuilder, QueryBuilder> addSearchCoreParams(Pair<SearchRequestBuilder, QueryBuilder> builders,
                                                                       String searchTerms, List<String> references,
                                                                       List<String> siteIds) {
    final SearchRequestBuilder searchRequestBuilder = builders.getLeft();
    return pairOf(searchRequestBuilder.setSearchType(SearchType.QUERY_THEN_FETCH).setTypes(indexedDocumentType),
            builders.getRight());
}