org.elasticsearch.action.search.SearchResponse Java Examples
The following examples show how to use
org.elasticsearch.action.search.SearchResponse.
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: MongoElasticsearchSyncIT.java From streams with Apache License 2.0 | 6 votes |
@Test public void testSync() throws Exception { MongoElasticsearchSync sync = new MongoElasticsearchSync(testConfiguration); sync.run(); IndicesExistsRequest indicesExistsRequest = Requests.indicesExistsRequest(testConfiguration.getDestination().getIndex()); IndicesExistsResponse indicesExistsResponse = testClient.admin().indices().exists(indicesExistsRequest).actionGet(); assertTrue(indicesExistsResponse.isExists()); // assert lines in file SearchRequestBuilder countRequest = testClient .prepareSearch(testConfiguration.getDestination().getIndex()) .setTypes(testConfiguration.getDestination().getType()); SearchResponse countResponse = countRequest.execute().actionGet(); assertEquals((int)countResponse.getHits().getTotalHits(), 89); }
Example #2
Source File: UITemplateManagementEsDAO.java From skywalking with Apache License 2.0 | 6 votes |
@Override public List<DashboardConfiguration> getAllTemplates(final Boolean includingDisabled) throws IOException { SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource(); BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); if (!includingDisabled) { boolQueryBuilder.must().add(QueryBuilders.termQuery(UITemplate.DISABLED, BooleanUtils.booleanToValue(includingDisabled))); } sourceBuilder.query(boolQueryBuilder); //It is impossible we have 10000+ templates. sourceBuilder.size(10000); SearchResponse response = getClient().search(UITemplate.INDEX_NAME, sourceBuilder); List<DashboardConfiguration> configs = new ArrayList<>(); final UITemplate.Builder builder = new UITemplate.Builder(); for (SearchHit searchHit : response.getHits()) { Map<String, Object> sourceAsMap = searchHit.getSourceAsMap(); final UITemplate uiTemplate = builder.map2Data(sourceAsMap); configs.add(new DashboardConfiguration().fromEntity(uiTemplate)); } return configs; }
Example #3
Source File: RunDao.java From usergrid with Apache License 2.0 | 6 votes |
/** * Returns a map of all Runs with queried commitId, runNumber and testName. * <p> * <ul> * <li>Key of the map is Run's id in elastic search</li> * <li>Value of the map is Run itself</li> * </ul> * * @param commitId commit id of the Run * @param runNumber Run number to filter queried Runs * @param testName Test class name that resulting Run is about * @return Map satisfying given parameters. The map is empty if there are no Runs. */ public Map<String, Run> getMap( String commitId, int runNumber, String testName ) { BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery() .must( termQuery( "commitId", commitId.toLowerCase() ) ) .must( termQuery( "runNumber", runNumber ) ) .must( termQuery( "testName", testName.toLowerCase() ) ); SearchResponse response = getRequest( DAO_INDEX_KEY, DAO_TYPE_KEY ) .setQuery( queryBuilder ) .setSize( MAX_RESULT_SIZE ) .execute() .actionGet(); HashMap<String, Run> runs = new HashMap<String, Run>(); for ( SearchHit hit : response.getHits().hits() ) { runs.put( hit.getId(), toRun( hit ) ); } return runs; }
Example #4
Source File: ResultUtils.java From vind with Apache License 2.0 | 6 votes |
public static HashMap<FieldDescriptor, TermFacetResult<?>> buildSuggestionResults(SearchResponse response, DocumentFactory factory, String context) { if(Objects.nonNull(response) && Objects.nonNull(response.getHits()) && Objects.nonNull(response.getHits().getHits())){ //TODO: if nested doc search is implemented final HashMap<FieldDescriptor, TermFacetResult<?>> suggestionValues = new HashMap<>(); response.getAggregations().asList().stream() .map(aggregation -> getTermFacetResults(aggregation, new Facet.TermFacet(factory.getField(aggregation.getName())), factory)) .filter(pair -> CollectionUtils.isNotEmpty(pair.getValue().getValues())) .forEach(pair -> suggestionValues.put(pair.getKey(), pair.getValue())); return suggestionValues; } else { throw new ElasticsearchException("Empty result from ElasticClient"); } }
Example #5
Source File: ESIndex.java From pyramid with Apache License 2.0 | 6 votes |
/** * phrase query * use whitespace analyzer in the query * @param field * @param phrase already stemmed * @param ids * @param slop * @return */ public SearchResponse matchPhrase(String field, String phrase, String[] ids, int slop) { IdsQueryBuilder idsFilterBuilder = new IdsQueryBuilder(documentType); idsFilterBuilder.addIds(ids); SearchResponse response = client.prepareSearch(indexName).setSize(ids.length) .setTrackScores(false) .setFetchSource(false).setExplain(false).setFetchSource(false). setQuery(QueryBuilders.boolQuery().must(QueryBuilders.matchPhraseQuery(field, phrase) .slop(slop).analyzer("whitespace")).filter(idsFilterBuilder)) .execute().actionGet(); // debug // XContentBuilder builder = XContentFactory.jsonBuilder(); // builder.startObject(); // System.out.println(response.toXContent(builder, ToXContent.EMPTY_PARAMS)); // builder.endObject(); // System.out.println(builder.string()); return response; }
Example #6
Source File: InternalEsClient.java From io with Apache License 2.0 | 6 votes |
/** * 非同期でドキュメントを検索. * @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 #7
Source File: SearchES.java From Transwarp-Sample-Code with MIT License | 6 votes |
/** * 多字段查询 */ public static void multisearch() { try { Settings settings = Settings.settingsBuilder().put("cluster.name", "elasticsearch1").build(); TransportClient transportClient = TransportClient.builder(). settings(settings).build().addTransportAddress( new InetSocketTransportAddress(InetAddress.getByName("172.16.2.93"), 9300)); SearchRequestBuilder searchRequestBuilder = transportClient.prepareSearch("service2","clients"); SearchResponse searchResponse = searchRequestBuilder. setQuery(QueryBuilders.boolQuery() .should(QueryBuilders.termQuery("id","5")) .should(QueryBuilders.prefixQuery("content","oracle"))) .setFrom(0).setSize(100).setExplain(true).execute().actionGet(); SearchHits searchHits = searchResponse.getHits(); System.out.println(); System.out.println("Total Hits is " + searchHits.totalHits()); System.out.println(); } catch (Exception e) { e.printStackTrace(); } }
Example #8
Source File: MetadataBackendKV.java From heroic with Apache License 2.0 | 6 votes |
@Override public AsyncFuture<CountSeries> countSeries(final CountSeries.Request filter) { return doto(c -> { final OptionalLimit limit = filter.getLimit(); if (limit.isZero()) { return async.resolved(new CountSeries()); } final QueryBuilder f = filter(filter.getFilter()); SearchRequest request = c.getIndex().count(METADATA_TYPE); SearchSourceBuilder sourceBuilder = request.source(); limit.asInteger().ifPresent(sourceBuilder::terminateAfter); sourceBuilder.query(new BoolQueryBuilder().must(f)); final ResolvableFuture<SearchResponse> future = async.future(); c.execute(request, bind(future)); return future.directTransform( r -> new CountSeries(r.getHits().getTotalHits().value, false)); }); }
Example #9
Source File: MatchPhrasePrefixQueryDemo.java From elasticsearch-full with Apache License 2.0 | 6 votes |
@Test public void test() throws Exception { String key = "this is a"; MatchPhrasePrefixQueryBuilder matchPhrasePrefixQueryBuilder = QueryBuilders.matchPhrasePrefixQuery("title",key); matchPhrasePrefixQueryBuilder.boost(10); matchPhrasePrefixQueryBuilder.analyzer("standard"); matchPhrasePrefixQueryBuilder.slop(2); matchPhrasePrefixQueryBuilder.maxExpansions(100); SearchResponse searchResponse = client.prepareSearch() .setIndices("my_index") .setTypes("my_type") .setQuery(matchPhrasePrefixQueryBuilder) .execute() .actionGet(); }
Example #10
Source File: EsUtil.java From bookmark with MIT License | 6 votes |
/** * Description: 搜索 * * @param index index * @param builder 查询参数 * @param c 结果类对象 * @return java.util.ArrayList * @author fanxb * @date 2019/7/25 13:46 */ public <T> List<T> search(String index, SearchSourceBuilder builder, Class<T> c) { if (!status) { return null; } SearchRequest request = new SearchRequest(index); request.source(builder); try { SearchResponse response = client.search(request, RequestOptions.DEFAULT); SearchHit[] hits = response.getHits().getHits(); List<T> res = new ArrayList<>(hits.length); for (SearchHit hit : hits) { res.add(JSON.parseObject(hit.getSourceAsString(), c)); } return res; } catch (Exception e) { throw new EsException(e); } }
Example #11
Source File: SiteElasticSearchIndexBuilder.java From sakai with Educational Community License v2.0 | 6 votes |
/** * 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 #12
Source File: ElasticsearchIndex.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * Evaluates the given query and returns the results as a TopDocs instance. */ public SearchHits search(SearchRequestBuilder request, QueryBuilder query) { String[] types = getTypes(); int nDocs; if (maxDocs > 0) { nDocs = maxDocs; } else { long docCount = client.prepareSearch(indexName) .setTypes(types) .setSource(new SearchSourceBuilder().size(0).query(query)) .get() .getHits() .getTotalHits(); nDocs = Math.max((int) Math.min(docCount, Integer.MAX_VALUE), 1); } SearchResponse response = request.setIndices(indexName) .setTypes(types) .setVersion(true) .setQuery(query) .setSize(nDocs) .execute() .actionGet(); return response.getHits(); }
Example #13
Source File: ElasticsearchRequestSubmitterTest.java From metron with Apache License 2.0 | 6 votes |
@Test public void searchShouldFailWhenNotOK() throws IOException { // mocks SearchResponse response = mock(SearchResponse.class); SearchRequest request = new SearchRequest(); // response will have status of OK when(response.status()).thenReturn(RestStatus.PARTIAL_CONTENT); when(response.getFailedShards()).thenReturn(0); when(response.getTotalShards()).thenReturn(2); // search should succeed ElasticsearchRequestSubmitter submitter = setup(response); assertThrows(InvalidSearchException.class, () -> submitter.submitSearch(request)); }
Example #14
Source File: CommonWebpageDAO.java From Gather-Platform with GNU General Public License v3.0 | 6 votes |
/** * 获取query的关联信息 * * @param query 查询queryString * @param size 结果集数量 * @return 相关信息 */ public Pair<Map<String, List<Terms.Bucket>>, List<Webpage>> relatedInfo(String query, int size) { SearchRequestBuilder searchRequestBuilder = client.prepareSearch(INDEX_NAME) .setTypes(TYPE_NAME) .setQuery(QueryBuilders.queryStringQuery(query)) .addSort("gatherTime", SortOrder.DESC) .addAggregation(AggregationBuilders.terms("relatedPeople").field("namedEntity.nr")) .addAggregation(AggregationBuilders.terms("relatedLocation").field("namedEntity.ns")) .addAggregation(AggregationBuilders.terms("relatedInstitution").field("namedEntity.nt")) .addAggregation(AggregationBuilders.terms("relatedKeywords").field("keywords")) .setSize(size); SearchResponse response = searchRequestBuilder.execute().actionGet(); Map<String, List<Terms.Bucket>> info = Maps.newHashMap(); info.put("relatedPeople", ((Terms) response.getAggregations().get("relatedPeople")).getBuckets()); info.put("relatedLocation", ((Terms) response.getAggregations().get("relatedLocation")).getBuckets()); info.put("relatedInstitution", ((Terms) response.getAggregations().get("relatedInstitution")).getBuckets()); info.put("relatedKeywords", ((Terms) response.getAggregations().get("relatedKeywords")).getBuckets()); return Pair.of(info, warpHits2List(response.getHits())); }
Example #15
Source File: ElasticsearchUtil.java From SpringBootLearn with Apache License 2.0 | 5 votes |
/** * 深度排序 分页 从当前页为1001开始 * @Author lihaodong * @Description * @Date 20:18 2018/12/21 * @Param [indexName, esType, startPage, pageSize, highlightField] * @return com.li.elasticsearch.EsPage **/ public static EsPage deepPageing(String indexName, String esType, int startPage, int pageSize, String highlightField) { System.out.println("scroll 模式启动!"); long begin = System.currentTimeMillis(); //初始化查询,获取scrollId BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); boolQueryBuilder.must(QueryBuilders.matchQuery("name", "名")); // boolQueryBuilder.filter(QueryBuilders.rangeQuery("price").from("1").to("999821")); SearchResponse response = client.prepareSearch(indexName)//对应索引 .setTypes(esType)//对应索引type .setQuery(boolQueryBuilder) .addSort("price", SortOrder.ASC) .setScroll(TimeValue.timeValueMinutes(1)) .setSize(10000) //第一次不返回size条数据 .highlighter(new HighlightBuilder().preTags("<span style='color:red' >").postTags("</span>").field(highlightField)) .setExplain(true) .execute() .actionGet(); long totalHits = response.getHits().totalHits; List<Map<String, Object>> result = disposeScrollResult(response, highlightField); List<Map<String, Object>> sourceList = result.stream().parallel().skip((startPage - 1-(10000/pageSize)) * pageSize).limit(pageSize).collect(Collectors.toList()); long end = System.currentTimeMillis(); System.out.println("耗时: " + (end - begin) + "ms"); System.out.println("耗时: " + (end - begin) / 1000 + "s"); System.out.println("查询"+totalHits+"条数据"); return new EsPage(startPage, pageSize, (int) totalHits, sourceList); }
Example #16
Source File: InternalEsClient.java From io with Apache License 2.0 | 5 votes |
/** * スクロールIDを指定してスクロールサーチを継続する. * @param scrollId スクロールID * @return 非同期応答 */ public ActionFuture<SearchResponse> asyncScrollSearch(String scrollId) { ActionFuture<SearchResponse> ret = esTransportClient.prepareSearchScroll(scrollId) .setScroll(new TimeValue(SCROLL_SEARCH_KEEP_ALIVE_TIME)) .execute(); return ret; }
Example #17
Source File: DistinctAction.java From foxtrot with Apache License 2.0 | 5 votes |
@Override public ActionResponse getResponse(org.elasticsearch.action.ActionResponse response, DistinctRequest parameter) { Aggregations aggregations = ((SearchResponse) response).getAggregations(); // Check if any aggregation is present or not if (aggregations == null) { logger.error("Null response for Group. Request : {}", parameter); return new DistinctResponse(new ArrayList<>(), new ArrayList<>()); } return getDistinctResponse(parameter, aggregations); }
Example #18
Source File: ESIndexTest.java From pyramid with Apache License 2.0 | 5 votes |
static void test11() throws Exception{ ESIndex index = new ESIndex.Builder().setClientType("node").setIndexName("imdb") .build(); // SpanTermQuery quick = new SpanTermQuery(new Term("body", "skip")); // SpanTermQuery brown = new SpanTermQuery(new Term("body", "thi")); // SpanTermQuery fox = new SpanTermQuery(new Term("body", "movi")); // SpanQuery[] quick_brown_dog = // new SpanQuery[]{quick, brown, fox}; // SpanNearQuery snq = // new SpanNearQuery(quick_brown_dog, 0, true); QueryBuilder builder = QueryBuilders.spanNearQuery(new SpanTermQueryBuilder("body", "skip"), 0) .addClause(new SpanTermQueryBuilder("body", "thi")) .addClause(new SpanTermQueryBuilder("body","movi")) .inOrder(true); SearchResponse response = index.getClient().prepareSearch(index.getIndexName()).setSize(index.getNumDocs()). setTrackScores(false). setFetchSource(false).setExplain(false).setFetchSource(false). setQuery(builder). execute().actionGet(); System.out.println(response.getHits().getTotalHits()); index.close(); }
Example #19
Source File: ReindexingPluginTest.java From elasticsearch-reindexing with Apache License 2.0 | 5 votes |
private void test_index_to_newIndex_withSource(Node node, String index) throws IOException { String newIndex = "dataset2"; try (CurlResponse curlResponse = Curl .post(node, "/" + index + "/_reindex/" + newIndex) .param("wait_for_completion", "true") .body("{\"query\":{\"term\":{\"msg\":{\"value\":\"1\"}}}}") .execute()) { Map<String, Object> map = curlResponse.getContentAsMap(); assertTrue(map.containsKey("acknowledged")); assertNull(map.get("name")); } runner.flush(); assertTrue(runner.indexExists(index)); assertTrue(runner.indexExists(newIndex)); // search newly created index { final SearchResponse searchResponse = runner.search(newIndex, new BuilderCallback<SearchRequestBuilder>() { @Override public SearchRequestBuilder apply(SearchRequestBuilder builder) { return builder; } }); assertEquals(1, searchResponse.getHits().getTotalHits()); } runner.deleteIndex(newIndex); }
Example #20
Source File: ElasticUtils.java From elasticsearch-sql with Apache License 2.0 | 5 votes |
public static SearchResponse scrollOneTimeWithHits(Client client, SearchRequestBuilder requestBuilder, Select originalSelect, int resultSize) { SearchResponse responseWithHits;SearchRequestBuilder scrollRequest = requestBuilder .setScroll(new TimeValue(60000)) .setSize(resultSize); boolean ordered = originalSelect.isOrderdSelect(); if(!ordered) scrollRequest.addSort(FieldSortBuilder.DOC_FIELD_NAME, SortOrder.ASC); responseWithHits = scrollRequest.get(); //on ordered select - not using SCAN , elastic returns hits on first scroll //es5.0 elastic always return docs on scan // if(!ordered) { // responseWithHits = client.prepareSearchScroll(responseWithHits.getScrollId()).setScroll(new TimeValue(600000)).get(); // } return responseWithHits; }
Example #21
Source File: DateHistogramAggregationMain.java From elasticsearch-pool with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws IOException { RestHighLevelClient client = HighLevelClient.getInstance(); try{ DateHistogramAggregationBuilder dateHistogramAggregationBuilder = AggregationBuilders.dateHistogram("ctm_date_histogram"); dateHistogramAggregationBuilder.field("ctm");//设置直方图针对的字段 dateHistogramAggregationBuilder.dateHistogramInterval(DateHistogramInterval.hours(6));//直方图每个分组对应的范围 dateHistogramAggregationBuilder.timeZone(DateTimeZone.forOffsetHours(8));//时区偏移 dateHistogramAggregationBuilder.keyed(true);//是否需要key名 dateHistogramAggregationBuilder.format("yyyy-MM-dd HH:mm");//key名格式 // dateHistogramAggregationBuilder.order(BucketOrder.aggregation("_key",true));//分组key的排序 // dateHistogramAggregationBuilder.minDocCount(0);//对于每个分组最少具有多少条数据,少于这个设置,则该分组不显示 // dateHistogramAggregationBuilder.extendedBounds(0,8000);//设置分组区间的下线和上线,只有当min_doc_count为0时有效 TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("cmd","weather_info"); SearchRequest searchRequest = new SearchRequest("serverlog_20180710");//限定index searchRequest.types("log");//限定type SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(termQueryBuilder); searchSourceBuilder.aggregation(dateHistogramAggregationBuilder); searchRequest.source(searchSourceBuilder); SearchResponse searchResponse = client.search(searchRequest); System.out.println(searchResponse); }finally{ HighLevelClient.close(); } }
Example #22
Source File: BaseDemo.java From Elasticsearch-Tutorial-zh-CN with GNU General Public License v3.0 | 5 votes |
/** * 查询 prefix * * @param transportClient * @throws IOException */ private static void queryByPrefix(TransportClient transportClient) throws IOException { SearchResponse searchResponse = transportClient.prepareSearch("product_index").setTypes("product") .setQuery(QueryBuilders.prefixQuery("product_name", "飞利")) .get(); for (SearchHit searchHit : searchResponse.getHits().getHits()) { logger.info("--------------------------------:" + searchHit.getSourceAsString()); } }
Example #23
Source File: TransportClient.java From elasticsearch-jest-example with MIT License | 5 votes |
/** * 删除查询到的文档 * @param index * @param name * @param value */ private static void scrollSearchDelete(String index,String name,String value){ Client client = createTransportClient(); QueryBuilder qb = termQuery(name, value); SearchResponse scrollResp = client.prepareSearch(index) .setSearchType(SearchType.SCAN) .setScroll(new TimeValue(60000)) .setQuery(qb) .setSize(100).execute().actionGet(); //100 hits per shard will be returned for each scroll BulkRequestBuilder bulkRequest = client.prepareBulk(); while (true) { for (SearchHit hit : scrollResp.getHits().getHits()) { bulkRequest.add(client.prepareDelete(hit.getIndex(),hit.getType(),hit.getId())); } scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(600000)).execute().actionGet(); if (scrollResp.getHits().getHits().length == 0) { break; } } BulkResponse bulkResponse = bulkRequest.execute().actionGet(); if (bulkResponse.hasFailures()) { BulkItemResponse[] bulkItemResponse = bulkResponse.getItems(); for (int i = 0; i <bulkItemResponse.length ; i++) { System.out.println(bulkItemResponse[i].getItemId()+":"+bulkItemResponse[i].getIndex()+":"+bulkItemResponse[i].getFailureMessage()); } } }
Example #24
Source File: RunResultDao.java From usergrid with Apache License 2.0 | 5 votes |
/** * @return All RunResult objects, stored in elastic search */ public List<RunResult> getAll() { SearchResponse response = elasticSearchClient.getClient() .prepareSearch( DAO_INDEX_KEY ) .setTypes( DAO_TYPE_KEY ) .addSort( fieldSort( "createTime" ) ) .setSize( MAX_RESULT_SIZE ) .execute() .actionGet(); return toList( response ); }
Example #25
Source File: TrendAction.java From foxtrot with Apache License 2.0 | 5 votes |
@Override public ActionResponse execute(TrendRequest parameter) { SearchRequestBuilder searchRequestBuilder = getRequestBuilder(parameter); try { SearchResponse searchResponse = searchRequestBuilder.execute() .actionGet(getGetQueryTimeout()); return getResponse(searchResponse, parameter); } catch (ElasticsearchException e) { throw FoxtrotExceptions.createQueryExecutionException(parameter, e); } }
Example #26
Source File: FeatureLoader.java From pyramid with Apache License 2.0 | 5 votes |
private static void loadCodeDesFeature(ESIndex index, DataSet dataSet, Feature feature, IdTranslator idTranslator, String docFilter){ int featureIndex = feature.getIndex(); CodeDescription codeDescription = (CodeDescription)(feature); SearchResponse response = index.minimumShouldMatch(codeDescription.getDescription(), codeDescription.getField(), codeDescription.getPercentage(), idTranslator.numData(), docFilter); SearchHit[] hits = response.getHits().getHits(); for (SearchHit hit: hits){ String indexId = hit.getId(); float score = hit.getScore(); int algorithmId = idTranslator.toIntId(indexId); dataSet.setFeatureValue(algorithmId,featureIndex,score); } }
Example #27
Source File: FuzzyQueryDemo.java From elasticsearch-full with Apache License 2.0 | 5 votes |
@Test public void testForClient() throws Exception { /** * fuzzyQuery基于编辑距离(Levenshtein)来进行相似搜索,比如搜索kimzhy,可以搜索出kinzhy(编辑距离为1) * 为了进行测试说明,前创建一个索引,插入几条数据ka,kab,kib,ba,我们的搜索源为ki * 了解更多关于编辑距离(Levenshtein)的概念,请参考:<a href='http://www.cnblogs.com/biyeymyhjob/archive/2012/09/28/2707343.html'></a> * 了解更多编辑距离的算法,请参考:<a href='http://blog.csdn.net/ironrabbit/article/details/18736185'></a> * ki — ka 编辑距离为1 * ki — kab 编辑距离为2 * ki — kbia 编辑距离为3 * ki — kib 编辑距离为1 * 所以当我们设置编辑距离(ES中使用fuzziness参数来控制)为0的时候,没有结果 * 所以当我们设置编辑距离(ES中使用fuzziness参数来控制)为1的时候,会出现结果ka,kib * 所以当我们设置编辑距离(ES中使用fuzziness参数来控制)为2的时候,会出现结果ka,kib,kab * 所以当我们设置编辑距离(ES中使用fuzziness参数来控制)为3的时候,会出现结果ka,kib,kab,kbaa(很遗憾,ES本身最多只支持到2,因此不会出现此结果) */ QueryBuilder qb = QueryBuilders.fuzzyQuery("username","ki") // .fuzziness(Fuzziness.ZERO); 没有结果 // .fuzziness(Fuzziness.ONE); 会出现结果ka,kib // .fuzziness(Fuzziness.TWO);会出现结果ka,kib,kab .fuzziness(Fuzziness.AUTO) ////会出现结果ka,kib,kab,但是这里以java-api的方式好像不太好使,原因未定 .prefixLength(0) .boost(1) .maxExpansions(100); SearchResponse response = client.prepareSearch() .setIndices("index") .setTypes("type") .setQuery(qb) .execute() .actionGet(); println(response); }
Example #28
Source File: ElasticSearchTargetIT.java From datacollector with Apache License 2.0 | 5 votes |
@Test public void testWriteRecords() throws Exception { Target target = createTarget(); TargetRunner runner = new TargetRunner.Builder(ElasticSearchDTarget.class, target).build(); try { runner.runInit(); List<Record> records = new ArrayList<>(); Record record1 = RecordCreator.create(); record1.set(Field.create(ImmutableMap.of("a", Field.create("Hello"), "index", Field.create("i"), "type", Field.create("t")))); Record record2 = RecordCreator.create(); record2.set(Field.create(ImmutableMap.of("a", Field.create("Bye"), "index", Field.create("i"), "type", Field.create("t")))); records.add(record1); records.add(record2); runner.runWrite(records); Assert.assertTrue(runner.getErrorRecords().isEmpty()); Assert.assertTrue(runner.getErrors().isEmpty()); prepareElasticSearchServerForQueries(); Set<Map> expected = new HashSet<>(); expected.add(ImmutableMap.of("a", "Hello", "index", "i", "type", "t")); expected.add(ImmutableMap.of("a", "Bye", "index", "i", "type", "t")); SearchResponse response = esServer.client().prepareSearch("i").setTypes("t") .setSearchType(SearchType.DEFAULT).execute().actionGet(); SearchHit[] hits = response.getHits().getHits(); Assert.assertEquals(2, hits.length); Set<Map> got = new HashSet<>(); got.add(hits[0].getSource()); got.add(hits[1].getSource()); Assert.assertEquals(expected, got); } finally { runner.runDestroy(); } }
Example #29
Source File: SearchQuestionBean.java From sakai with Educational Community License v2.0 | 5 votes |
public boolean userOwns(String questionId){ // To know if the actual user owns a question... just search for it with the "own" parameter. // If results length is 0... then you don't own it :) // This is needed to avoid the view of not owned question by someone that // tries to preview a question (for instance calling the jsf directly). // To improve performance, we will store a "search-duration" cache of this, // so we don't call the search service once we know that result. if (questionsIOwn.containsKey(questionId)){ if (questionsIOwn.get(questionId)){ return true; }else{ return false; } }else { HashMap<String, String> additionalSearchInformation = new HashMap<String, String>(); additionalSearchInformation.put("scope", "own"); additionalSearchInformation.put("subtype", "item"); additionalSearchInformation.put("questionId", questionId); try { SearchResponse sr = searchService.searchResponse("", null, 0, 1, "questions", additionalSearchInformation); if (sr.getHits().totalHits() < 1) { questionsIOwn.put(questionId, Boolean.FALSE); return false; } else { questionsIOwn.put(questionId, Boolean.TRUE); return true; } } catch (Exception ex) { //Failure searching. It should not happen, Let's return false. log.warn("Failure calculating permissions to preview"); return false; } } }
Example #30
Source File: ESClientTest.java From emotional_analysis with Apache License 2.0 | 5 votes |
@Test public void test4(){ SearchRequestBuilder responsebuilder = client.prepareSearch("twitter").setTypes("tweet") ; SearchResponse myresponse=responsebuilder.setQuery(QueryBuilders.matchPhraseQuery("user", "kimchy")) .setFrom(0).setSize(10).setExplain(true).execute().actionGet(); SearchHits hits = myresponse.getHits(); for (int i = 0; i < hits.getHits().length; i++) { System.out.println(hits.getHits()[i].getSourceAsString());} }