Java Code Examples for org.elasticsearch.search.SearchHit#getSourceAsString()
The following examples show how to use
org.elasticsearch.search.SearchHit#getSourceAsString() .
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: ESUtil.java From ns4_gear_watchdog with Apache License 2.0 | 6 votes |
/** * 通用的装换返回结果 * */ public <T> List<T> convertResponse(SearchResponse response,Class<T> clazz) { List<T> list = Lists.newArrayList(); if(response != null && response.getHits() != null) { String result = ""; T e = null; Field idField = ReflectionUtils.findField(clazz, "id"); if (idField != null) { ReflectionUtils.makeAccessible(idField); } for(SearchHit hit : response.getHits()) { result = hit.getSourceAsString(); if (StringUtils.hasText(result)) { e = JSONObject.parseObject(result, clazz); } if (e != null) { if (idField != null) { ReflectionUtils.setField(idField, e, hit.getId()); } list.add(e); } } } return list; }
Example 2
Source File: ElasticsearchTemplateRecordConsumerTest.java From baleen with Apache License 2.0 | 6 votes |
@Test public void testRecords() throws JsonParseException, JsonMappingException, IOException, AnalysisEngineProcessException { assertEquals(new Long(0), getCount()); process(); SearchHits hits = elasticsearch .client() .search(new SearchRequest().indices(BALEEN_INDEX)) .actionGet() .getHits(); assertEquals(3, hits.getTotalHits()); ObjectMapper mapper = new ObjectMapper(); Collection<ExtractedRecord> records = new ArrayList<>(); for (SearchHit hit : hits.getHits()) { String json = hit.getSourceAsString(); ElasticsearchExtractedRecord er = mapper.readValue(json, ElasticsearchExtractedRecord.class); records.add(er); } assertEquals(3, records.size()); Map<String, Collection<ExtractedRecord>> recordsMap = new HashMap<>(); recordsMap.put(annotatorClass.getSimpleName(), records); checkRecords(recordsMap); }
Example 3
Source File: ElasticsearchSupportHandler.java From super-cloudops with Apache License 2.0 | 6 votes |
@Override public List<T> findAll(SearchRequest searchRequest) throws Exception { SearchResponse searchResp = this.restHighLevelClient.search(searchRequest); for (ShardSearchFailure failure : searchResp.getShardFailures()) { listener.onFailure(failure); } SearchHits hits = searchResp.getHits(); SearchHit[] searchHits = hits.getHits(); List<T> list = new ArrayList<>(); for (SearchHit hit : searchHits) { String sourceAsString = hit.getSourceAsString(); T t = JacksonUtils.parseJSON(sourceAsString, clazzP); list.add(t); } Collections.reverse(list); return list; }
Example 4
Source File: ElasticSearchServiceMapper.java From vertx-elasticsearch-service with Apache License 2.0 | 6 votes |
private static Hit mapToHit(SearchHit searchHit) { final Hit hit = new Hit() .setId(searchHit.getId()) .setIndex(searchHit.getIndex()) .setType(searchHit.getType()) .setScore(searchHit.getScore()) .setVersion(searchHit.getVersion()) .setFields( searchHit.getFields() .entrySet() .stream() .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue().getValues())) ); if (searchHit.getSourceAsString() != null) { hit.setSource(new JsonObject(searchHit.getSourceAsString())); } return hit; }
Example 5
Source File: SpanRepository.java From cicada with MIT License | 5 votes |
private List<SpanModel> parseResponse(final SearchResponse response) { // 处理返回结果 final List<SpanModel> spans = new LinkedList<SpanModel>(); for (final SearchHit hit : response.getHits().hits()) { final String doc = hit.getSourceAsString(); try { spans.add(JSON.parseObject(doc, SpanModel.class)); } catch (JSONException ex) { log.error("failed load data {}, error {}", doc, ex); continue; } } return spans; }
Example 6
Source File: EntityScroll.java From elastic-crud with Apache License 2.0 | 5 votes |
@Override public void accept(final SearchHit hit) { final String source = hit.getSourceAsString(); @SuppressWarnings("unchecked") final T entity = (T) deserializer.apply(source).withId(hit.getId()); consumer.accept(entity); }
Example 7
Source File: ElasticSearchDAOV6.java From conductor with Apache License 2.0 | 5 votes |
private List<EventExecution> mapEventExecutionsResponse(SearchResponse response) throws IOException { SearchHit[] hits = response.getHits().getHits(); List<EventExecution> executions = new ArrayList<>(hits.length); for (SearchHit hit : hits) { String source = hit.getSourceAsString(); EventExecution tel = objectMapper.readValue(source, EventExecution.class); executions.add(tel); } return executions; }
Example 8
Source File: ElasticSearchDAOV6.java From conductor with Apache License 2.0 | 5 votes |
private List<Message> mapGetMessagesResponse(SearchResponse response) throws IOException { SearchHit[] hits = response.getHits().getHits(); TypeFactory factory = TypeFactory.defaultInstance(); MapType type = factory.constructMapType(HashMap.class, String.class, String.class); List<Message> messages = new ArrayList<>(hits.length); for (SearchHit hit : hits) { String source = hit.getSourceAsString(); Map<String, String> mapSource = objectMapper.readValue(source, type); Message msg = new Message(mapSource.get("messageId"), mapSource.get("payload"), null); messages.add(msg); } return messages; }
Example 9
Source File: ElasticSearchDAOV6.java From conductor with Apache License 2.0 | 5 votes |
private List<TaskExecLog> mapTaskExecLogsResponse(SearchResponse response) throws IOException { SearchHit[] hits = response.getHits().getHits(); List<TaskExecLog> logs = new ArrayList<>(hits.length); for (SearchHit hit : hits) { String source = hit.getSourceAsString(); TaskExecLog tel = objectMapper.readValue(source, TaskExecLog.class); logs.add(tel); } return logs; }
Example 10
Source File: ElasticSearchRestDAOV6.java From conductor with Apache License 2.0 | 5 votes |
private List<EventExecution> mapEventExecutionsResponse(SearchResponse response) throws IOException { SearchHit[] hits = response.getHits().getHits(); List<EventExecution> executions = new ArrayList<>(hits.length); for (SearchHit hit : hits) { String source = hit.getSourceAsString(); EventExecution tel = objectMapper.readValue(source, EventExecution.class); executions.add(tel); } return executions; }
Example 11
Source File: ElasticSearchRestDAOV6.java From conductor with Apache License 2.0 | 5 votes |
private List<Message> mapGetMessagesResponse(SearchResponse response) throws IOException { SearchHit[] hits = response.getHits().getHits(); TypeFactory factory = TypeFactory.defaultInstance(); MapType type = factory.constructMapType(HashMap.class, String.class, String.class); List<Message> messages = new ArrayList<>(hits.length); for (SearchHit hit : hits) { String source = hit.getSourceAsString(); Map<String, String> mapSource = objectMapper.readValue(source, type); Message msg = new Message(mapSource.get("messageId"), mapSource.get("payload"), null); messages.add(msg); } return messages; }
Example 12
Source File: ElasticSearchRestDAOV6.java From conductor with Apache License 2.0 | 5 votes |
private List<TaskExecLog> mapTaskExecLogsResponse(SearchResponse response) throws IOException { SearchHit[] hits = response.getHits().getHits(); List<TaskExecLog> logs = new ArrayList<>(hits.length); for (SearchHit hit : hits) { String source = hit.getSourceAsString(); TaskExecLog tel = objectMapper.readValue(source, TaskExecLog.class); logs.add(tel); } return logs; }
Example 13
Source File: AnnotationRepository.java From cicada with MIT License | 5 votes |
/** * 根据traceId和spanId获取annotationModel列表. */ public List<AnnotationModel> getSpanAnnotations(final String traceId, final String spanId) { // 声明SearchRequestBuilder实例 final String indice = getDefaultIndice(); final SearchRequestBuilder builder = client.prepareSearch(indice); builder.setTypes(props.getEsTypeName()); // 设置查询条件 final BoolQueryBuilder query = new BoolQueryBuilder(); query.must(QueryBuilders.termQuery("traceId", traceId)) // .must(QueryBuilders.termQuery("spanId", spanId)); // 执行查询 final SearchResponse response = builder.setQuery(query).execute().actionGet(); // 处理返回结果 final List<AnnotationModel> annos = new LinkedList<AnnotationModel>(); for (final SearchHit hit : response.getHits().hits()) { final String docStr = hit.getSourceAsString(); try { final AnnotationModel model = JSON.parseObject(docStr, AnnotationModel.class); annos.add(model); } catch (JSONException ex) { log.error("failed load data {} to AnnotationModel, error {}", docStr, ex); continue; } } return annos; }
Example 14
Source File: PersistentServiceDef.java From sfs with Apache License 2.0 | 5 votes |
public static PersistentServiceDef fromSearchHit(SearchHit searchHit) { long persistentVersion = searchHit.getVersion(); JsonObject document = new JsonObject(searchHit.getSourceAsString()); return new PersistentServiceDef(searchHit.getId(), persistentVersion) .merge(document); }
Example 15
Source File: ElasticSearchDAOV5.java From conductor with Apache License 2.0 | 5 votes |
private List<Message> mapGetMessagesResponse(SearchResponse response) throws IOException { SearchHit[] hits = response.getHits().getHits(); TypeFactory factory = TypeFactory.defaultInstance(); MapType type = factory.constructMapType(HashMap.class, String.class, String.class); List<Message> messages = new ArrayList<>(hits.length); for (SearchHit hit : hits) { String source = hit.getSourceAsString(); Map<String, String> mapSource = objectMapper.readValue(source, type); Message msg = new Message(mapSource.get("messageId"), mapSource.get("payload"), null); messages.add(msg); } return messages; }
Example 16
Source File: ConfService.java From SkaETL with Apache License 2.0 | 5 votes |
private void treatResponse(SearchResponse searchResponse) { for (SearchHit searchHit : searchResponse.getHits()) { String res = searchHit.getSourceAsString(); ObjectMapper objectMapper = new ObjectMapper(); try { ConfEsSkalogs conf = objectMapper.readValue(res, ConfEsSkalogs.class); map.put(conf.getConfigurationLogstash().getIdConfiguration(),conf.getConfigurationLogstash()); log.info("Add configuration {}",conf.getConfigurationLogstash()); }catch(Exception e){ log.error("Pwoblem during parsing {}",e); } } }
Example 17
Source File: PersistentContainer.java From sfs with Apache License 2.0 | 5 votes |
public static PersistentContainer fromSearchHit(PersistentAccount persistentAccount, SearchHit searchHit) { JsonObject document = new JsonObject(searchHit.getSourceAsString()); return new PersistentContainer(persistentAccount, searchHit.getId(), searchHit.getVersion()) .merge(document); }
Example 18
Source File: AlertsSearcher.java From opensoc-streaming with Apache License 2.0 | 5 votes |
private void doSenderWork( SearchHit hit ) { String kafkaBrokerHostName = configProps.getProperty("kafkaBrokerHostName", "localhost" ); String kafkaBrokerHostPort = configProps.getProperty("kafkaBrokerHostPort", "9092" ); String kafkaTopicName = configProps.getProperty("kafkaTopicName", "test" ); logger.debug( "kafkaBrokerHostName: " + kafkaBrokerHostName ); logger.debug( "kafkaBrokerHostPort: " + kafkaBrokerHostPort ); logger.debug( "kafkaTopicName: " + kafkaTopicName ); String sourceData = hit.getSourceAsString(); logger.debug( "Source Data: " + sourceData ); Properties props = new Properties(); props.put("metadata.broker.list", kafkaBrokerHostName + ":" + kafkaBrokerHostPort ); props.put("serializer.class", "kafka.serializer.StringEncoder"); // props.put("partitioner.class", "example.producer.SimplePartitioner"); props.put("request.required.acks", "1"); ProducerConfig config = new ProducerConfig(props); Producer<String, String> producer = new Producer<String, String>(config); KeyedMessage<String, String> data = new KeyedMessage<String, String>(kafkaTopicName, "", sourceData ); producer.send(data); }
Example 19
Source File: CamelSinkElasticSearchITCase.java From camel-kafka-connector with Apache License 2.0 | 5 votes |
private void verifyHit(SearchHit searchHit) { String source = searchHit.getSourceAsString(); assertTrue(source != null); assertFalse(source.isEmpty()); // TODO: this is not enough, we need to parse the json and check the key itself assertTrue(source.contains(transformKey)); LOG.debug("Search hit: {} ", searchHit.getSourceAsString()); received++; }
Example 20
Source File: SearchApiMain.java From elasticsearch-pool with Apache License 2.0 | 4 votes |
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(); } }