Java Code Examples for org.springframework.data.elasticsearch.core.ElasticsearchTemplate

The following examples show how to use org.springframework.data.elasticsearch.core.ElasticsearchTemplate. These examples are extracted from open source projects. 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
public ElasticsearchTemplate elasticsearchTemplate(Client client) {
    try {
        SimpleElasticsearchMappingContext simpleElasticsearchMappingContext = new SimpleElasticsearchMappingContext();
        ElasticsearchConverter elasticsearchConverter = new MappingElasticsearchConverter(simpleElasticsearchMappingContext);
        return new ElasticsearchTemplate(client, elasticsearchConverter);
    } catch (Exception var4) {
        throw new IllegalStateException(var4);
    }
}
 
Example 2
/**
 * 描述: Elasticsearch服务初始化
 * 时间: 2017年11月14日 上午10:55:02
 * @author yi.zhang
 */
public void init(String clusterName,String servers,String username,String password){
	try {
		TransportClientFactoryBean client = new TransportClientFactoryBean();
		client.setClusterName(clusterName);
		String clusterNodes = "";
		for(String server : servers.split(",")){
			String[] address = server.split(":");
			String ip = address[0];
			int port=9300;
			if(address.length>1){
				port = Integer.valueOf(address[1]);
			}
			if(StringUtil.isEmpty(clusterNodes)){
				clusterNodes = ip+":"+port;
			}else{
				clusterNodes +=","+ ip+":"+port;
			}
		}
		client.setClusterNodes(clusterNodes);
		if(!StringUtil.isEmpty(username)&&!StringUtil.isEmpty(password)){
			Properties properties = new Properties();
			properties.put("xpack.security.user",username+":"+password);
			client.setProperties(properties);
		}
		client.afterPropertiesSet();
		template = new ElasticsearchTemplate(client.getObject());
	} catch (Exception e) {
		logger.error("-----Elasticsearch Config init Error-----", e);
	}
}
 
Example 3
public ElasticsearchTransactionRepository(
    ElasticsearchTemplate template, MetricsService metricsService, int batchSize,
    int refreshTime) {
  this.template = template;
  this.metricsService = metricsService;
  this.batchSize = batchSize;
  this.refreshTime = refreshTime;
  if (this.refreshTime > 0) {
    new Thread(new RefreshTimer(), "elasticsearch-repository-refresh").start();
  }
  if (!this.template.indexExists(INDEX_NAME)) {
    this.template.createIndex(INDEX_NAME);
  }
}
 
Example 4
Source Project: servicecomb-pack   Source File: FsmAutoConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnProperty(value = "alpha.feature.akka.transaction.repository.type", havingValue = "elasticsearch")
public TransactionRepository transactionRepository(MetricsService metricsService,
    ElasticsearchTemplate template) {
  return new ElasticsearchTransactionRepository(template, metricsService,
      repositoryElasticsearchBatchSize, repositoryElasticsearchRefreshTime);
}
 
Example 5
Source Project: klask-io   Source File: FileSystemCrawler.java    License: GNU General Public License v3.0 5 votes vote down vote up
public FileSystemCrawler(String root, KlaskProperties klaskProperties, FileSearchRepository fileSearchRepository, ElasticsearchTemplate elasticsearchTemplate){
    super();
    this.root = root;
    this.rootPath = new java.io.File(this.root).toPath();
    this.result = null;
    this.klaskProperties = klaskProperties;
    this.fileSearchRepository = fileSearchRepository;
    this.elasticsearchTemplate = elasticsearchTemplate;
}
 
Example 6
@Bean
public CommandLineRunner example(CarRepository repository,
		ElasticsearchTemplate template) {
	return (args) -> {
		System.err.println("From the repository...");
		repository.findByMakeIgnoringCase("fOrD").forEach(System.err::println);

		System.err.println("\nFrom the template...");
		SearchQuery query = new NativeSearchQueryBuilder()
				.withQuery(fuzzyQuery("make", "Ronda")).build();
		template.queryForList(query, Car.class).forEach(System.err::println);
	};
}
 
Example 7
Source Project: onetwo   Source File: SimpleSuggestionBuilder.java    License: Apache License 2.0 5 votes vote down vote up
public <R> R get(ElasticsearchTemplate elasticsearchTemplate, Function<SuggestResponse, R> mapper){
	Assert.notNull(this.suggestion);
	Assert.notEmpty(indices);
	SuggestResponse response = elasticsearchTemplate.suggest(suggestion, this.indices.toArray(new String[0]));
	
	return mapper.apply(response);
}
 
Example 8
Source Project: onetwo   Source File: SimpleSuggestionBuilder.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public List<? extends Option> getOptions(ElasticsearchTemplate elasticsearchTemplate){
	if(StringUtils.isBlank(text)){
		return Collections.emptyList();
	}
	return get(elasticsearchTemplate, response->{
		Suggestion<?> suggestion = response.getSuggest().getSuggestion(name);
		if(suggestion==null || suggestion.getEntries().isEmpty()){
			return Collections.emptyList();
		}
		Suggestion.Entry<? extends Option> entry = suggestion.getEntries().get(0);
		List<? extends Option> options = entry.getOptions();
		return options;
	});
}
 
Example 9
Source Project: onetwo   Source File: SimpleSearchQueryBuilder.java    License: Apache License 2.0 5 votes vote down vote up
public Aggregations queryAggs(ElasticsearchTemplate elasticsearchTemplate){
	return doQuery(elasticsearchTemplate, response->{
		/*Terms agg = response.getAggregations().get("hots");
		List<Bucket> buckets = agg.getBuckets();
		Object key = buckets.get(0).getKey();
		Object doc = buckets.get(0).getDocCount();*/
		return response.getAggregations();
	});
}
 
Example 10
Source Project: onetwo   Source File: SimpleSearchQueryBuilder.java    License: Apache License 2.0 5 votes vote down vote up
public QueryResult(ElasticsearchTemplate elasticsearchTemplate, SimpleSearchQueryBuilder queryBuilder, SearchResponse response) {
	super();
	this.queryBuilder = queryBuilder;
	this.searchResponse = response;
	this.aggregationsResult = new AggregationsResult(this.searchResponse.getAggregations());
	this.mapper = new DefaultResultMapper(elasticsearchTemplate.getElasticsearchConverter().getMappingContext());
}
 
Example 11
Source Project: youkefu   Source File: QcTask.java    License: Apache License 2.0 4 votes vote down vote up
@Autowired
public void setElasticsearchTemplate(ElasticsearchTemplate elasticsearchTemplate) {
	this.elasticsearchTemplate = elasticsearchTemplate;
   }
 
Example 12
Source Project: youkefu   Source File: EkmKnowledgeTimesRepositoryImpl.java    License: Apache License 2.0 4 votes vote down vote up
@Autowired
public void setElasticsearchTemplate(ElasticsearchTemplate elasticsearchTemplate) {
	this.elasticsearchTemplate = elasticsearchTemplate;
   }
 
Example 13
Source Project: youkefu   Source File: EkmKnowledgeMasterRepositoryImpl.java    License: Apache License 2.0 4 votes vote down vote up
@Autowired
public void setElasticsearchTemplate(ElasticsearchTemplate elasticsearchTemplate) {
	this.elasticsearchTemplate = elasticsearchTemplate;
   }
 
Example 14
Source Project: youkefu   Source File: TopicRepositoryImpl.java    License: Apache License 2.0 4 votes vote down vote up
@Autowired
public void setElasticsearchTemplate(ElasticsearchTemplate elasticsearchTemplate) {
       this.elasticsearchTemplate = elasticsearchTemplate;
   }
 
Example 15
Source Project: youkefu   Source File: KbsTopicCommentRepositoryImpl.java    License: Apache License 2.0 4 votes vote down vote up
@Autowired
public void setElasticsearchTemplate(ElasticsearchTemplate elasticsearchTemplate) {
	this.elasticsearchTemplate = elasticsearchTemplate;
   }
 
Example 16
Source Project: youkefu   Source File: QuickReplyRepositoryImpl.java    License: Apache License 2.0 4 votes vote down vote up
@Autowired
public void setElasticsearchTemplate(ElasticsearchTemplate elasticsearchTemplate) {
       this.elasticsearchTemplate = elasticsearchTemplate ;
   }
 
Example 17
Source Project: youkefu   Source File: EkmKnowledgeCollectRepositoryImpl.java    License: Apache License 2.0 4 votes vote down vote up
@Autowired
public void setElasticsearchTemplate(ElasticsearchTemplate elasticsearchTemplate) {
	this.elasticsearchTemplate = elasticsearchTemplate;
   }
 
Example 18
Source Project: youkefu   Source File: KbsTopicRepositoryImpl.java    License: Apache License 2.0 4 votes vote down vote up
@Autowired
public void setElasticsearchTemplate(ElasticsearchTemplate elasticsearchTemplate) {
       this.elasticsearchTemplate = elasticsearchTemplate;
   }
 
Example 19
Source Project: youkefu   Source File: ContactsRepositoryImpl.java    License: Apache License 2.0 4 votes vote down vote up
@Autowired
public void setElasticsearchTemplate(ElasticsearchTemplate elasticsearchTemplate) {
	this.elasticsearchTemplate = elasticsearchTemplate;
   }
 
Example 20
Source Project: youkefu   Source File: EkmKnowledgeRepositoryImpl.java    License: Apache License 2.0 4 votes vote down vote up
@Autowired
public void setElasticsearchTemplate(ElasticsearchTemplate elasticsearchTemplate) {
	this.elasticsearchTemplate = elasticsearchTemplate;
   }
 
Example 21
Source Project: youkefu   Source File: EntCustomerRepositoryImpl.java    License: Apache License 2.0 4 votes vote down vote up
@Autowired
public void setElasticsearchTemplate(ElasticsearchTemplate elasticsearchTemplate) {
	this.elasticsearchTemplate = elasticsearchTemplate;
   }
 
Example 22
Source Project: youkefu   Source File: UKDataContext.java    License: Apache License 2.0 4 votes vote down vote up
public static ElasticsearchTemplate getTemplet() {
	return templet;
}
 
Example 23
Source Project: youkefu   Source File: UKDataContext.java    License: Apache License 2.0 4 votes vote down vote up
public static void setTemplet(ElasticsearchTemplate templet) {
	UKDataContext.templet = templet;
}
 
Example 24
@Bean
public ElasticsearchTemplate elasticsearchTemplate(Client client, ObjectMapper objectMapper) {
  return new ElasticsearchTemplate(client, new CustomEntityMapper(objectMapper));
}
 
Example 25
@Bean
public ElasticsearchTemplate elasticsearchTemplate(Client client, ObjectMapper objectMapper) {
  return new ElasticsearchTemplate(client, new CustomEntityMapper(objectMapper));
}
 
Example 26
Source Project: pivotal-bank-demo   Source File: EsConfig.java    License: Apache License 2.0 4 votes vote down vote up
@Bean
public ElasticsearchOperations elasticsearchTemplate() throws Exception {
    return new ElasticsearchTemplate(client());
}
 
Example 27
Source Project: pivotal-bank-demo   Source File: EsConfig.java    License: Apache License 2.0 4 votes vote down vote up
@Bean
public ElasticsearchOperations elasticsearchTemplate() throws Exception {
    return new ElasticsearchTemplate(client());
}
 
Example 28
Source Project: Spring-5.0-Projects   Source File: ElasticDataConfig.java    License: MIT License 4 votes vote down vote up
@Bean
public ElasticsearchTemplate elasticsearchTemplate() throws Exception {
    return new ElasticsearchTemplate(client());
}
 
Example 29
Source Project: poseidon   Source File: ItemServiceImpl.java    License: Apache License 2.0 4 votes vote down vote up
public ItemServiceImpl(ItemRepository itemRepository, ElasticsearchTemplate elasticsearchTemplate,
		ItemFeign itemFeign) {
	this.itemRepository = itemRepository;
	this.elasticsearchTemplate = elasticsearchTemplate;
	this.itemFeign = itemFeign;
}
 
Example 30
Source Project: albert   Source File: ESConfig.java    License: MIT License 4 votes vote down vote up
@Bean
public ElasticsearchOperations elasticsearchTemplate() throws Exception {
	return new ElasticsearchTemplate(client());
}