<!--ElasticSearch-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
spring:
elasticsearch:
uris:
- localhost:9200
username: elastic
password: password
@EnableElasticsearchRepositories(basePackages = "com.meta.es.repositories")//repository所在的包路径
@SpringBootApplication
public class SpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootApplication.class, args);
}
}
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import java.util.Date;
@Data
@Document(indexName = "elastic")//indexName索引名称等价于MySQL的表
public class ElasticSearchEntity {
@Id
@Field(type = FieldType.Keyword)//关键字在查询的时候不会被拆分
private String id;
@Field(type = FieldType.Text)//字符串对应文本类型
private String name;
@Field(type = FieldType.Date, format = DateFormat.date_hour_minute_second_millis)//日期类型
@JsonSerialize(using = LocalDateTimeSerializer.class)
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
private Date createTime;
@Field(type = FieldType.Boolean)//布尔类型
private boolean isDelete;
}
import org.springframework.data.elasticsearch.annotations.Query;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import java.util.List;
//ElasticsearchRepository<实体类,主键类型>
public interface ElasticSearchRepository extends ElasticsearchRepository<ElasticSearchEntity,String> {
List<ElasticSearchEntity> findByName(String name);
//自定义查询语句
@Query("{\"match\":{\"name\":\"?0\"}}")
List<ElasticSearchEntity> findAllByNameUsingAnnotations(String name);
}
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class ElasticSearchService {
@Resource
private ElasticSearchRepository elasticSearchRepository;
/**
* 新增/修改
*/
public void save(final ElasticSearchEntity elasticSearchEntity) {
this.elasticSearchRepository.save(elasticSearchEntity);
}
/**
* 通过ID查询
*/
public ElasticSearchEntity findById(final String id){
return this.elasticSearchRepository.findById(id).orElse(null);
}
/**
* 通过ID删除
*/
public void deleteById(String id){
this.elasticSearchRepository.deleteById(id);
}
/**
* 通过名称模糊查询
*/
public List<ElasticSearchEntity> findByName(final String name){
return this.elasticSearchRepository.findByName(name);
}
/**
* 通过使用注解名称模糊查询
*/
public List<ElasticSearchEntity> findAllByNameUsingAnnotations(String name){
return this.elasticSearchRepository.findAllByNameUsingAnnotations(name);
}
/**
* 分页
* @param size 数量
*/
public Page<ElasticSearchEntity> findAllByPage(int page,int size){
//Sort sort = Sort.by(Sort.Order.desc("create_date"));//排序
//Pageable pageable =PageRequest.of(Integer.parseInt(page), Integer.parseInt(size), sort);
Sort sort = Sort.by(Sort.Order.desc("createTime"));//根据
Pageable pageable =PageRequest.of(page, size, sort);
return this.elasticSearchRepository.findAll(pageable);
}
}
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
@RestController
@RequestMapping("elasticsearch")
public class ElasticSearchController {
@Resource
private ElasticSearchService elasticSearchService;
/**
* 新增/修改,如果id一致就修改,否则新增
* @param elasticSearchEntity 实体类
*/
@PostMapping
public void save(@RequestBody final ElasticSearchEntity elasticSearchEntity) {
elasticSearchService.save(elasticSearchEntity);
}
/**
* 通过ID查询
* @param id ID
* @return
*/
@GetMapping("/{id}")
public ElasticSearchEntity findById(@PathVariable final String id) {
return elasticSearchService.findById(id);
}
/**
* 通过ID删除
* @param id ID
* @return
*/
@DeleteMapping("/{id}")
public boolean deleteById(@PathVariable String id) {
elasticSearchService.deleteById(id);
return true;
}
/**
* 通过名称模糊查询
* @param name 名称
* @return
*/
@GetMapping("/name/{name}")
public List<ElasticSearchEntity> findAllByName(@PathVariable String name) {
return elasticSearchService.findByName(name);
}
/**
* 使用注释的方式名称模糊查询,即:@Query()注解
* @param name 名称
* @return
*/
@GetMapping("/name/{name}/annotations")
public List<ElasticSearchEntity> findAllByNameAnnotations(@PathVariable String name) {
return elasticSearchService.findAllByNameUsingAnnotations(name);
}
/**
* 简单分页查询
* @param page 起始页,默认从0开始
* @param size 每页数量
* @return
*/
@GetMapping("/page")
public Page<ElasticSearchEntity> findAllByPage(@RequestParam(name = "page",defaultValue = "0") int page, @RequestParam(name = "size",defaultValue = "10") int size){
return elasticSearchService.findAllByPage(page,size);
}
}
查看创建的索引(相当于MySQL的表)
method:GET
localhost:9200/_cat/indices
删除索引
method:DELETE
localhost:9200/{索引名称}
查看索引里的全部数据,elastic是实体类@Document定义的indexName
method:GET
localhost:9200/elastic/_search
新增索引里没有ID就是新增,有ID就是修改

修改索引里没有ID就是新增,有ID就是修改

查询

删除

分页

通过name模糊查询

我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.
我有一个用户工厂。我希望默认情况下确认用户。但是鉴于unconfirmed特征,我不希望它们被确认。虽然我有一个基于实现细节而不是抽象的工作实现,但我想知道如何正确地做到这一点。factory:userdoafter(:create)do|user,evaluator|#unwantedimplementationdetailshereunlessFactoryGirl.factories[:user].defined_traits.map(&:name).include?(:unconfirmed)user.confirm!endendtrait:unconfirmeddoenden
我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr
我在app/helpers/sessions_helper.rb中有一个帮助程序文件,其中包含一个方法my_preference,它返回当前登录用户的首选项。我想在集成测试中访问该方法。例如,这样我就可以在测试中使用getuser_path(my_preference)。在其他帖子中,我读到这可以通过在测试文件中包含requiresessions_helper来实现,但我仍然收到错误NameError:undefinedlocalvariableormethod'my_preference'.我做错了什么?require'test_helper'require'sessions_hel
有没有办法在这个简单的get方法中添加超时选项?我正在使用法拉第3.3。Faraday.get(url)四处寻找,我只能先发起连接后应用超时选项,然后应用超时选项。或者有什么简单的方法?这就是我现在正在做的:conn=Faraday.newresponse=conn.getdo|req|req.urlurlreq.options.timeout=2#2secondsend 最佳答案 试试这个:conn=Faraday.newdo|conn|conn.options.timeout=20endresponse=conn.get(url
我一直很高兴地使用DelayedJob习惯用法:foo.send_later(:bar)这会调用DelayedJob进程中对象foo的方法bar。我一直在使用DaemonSpawn在我的服务器上启动DelayedJob进程。但是...如果foo抛出异常,Hoptoad不会捕获它。这是任何这些包中的错误...还是我需要更改某些配置...或者我是否需要在DS或DJ中插入一些异常处理来调用Hoptoad通知程序?回应下面的第一条评论。classDelayedJobWorker 最佳答案 尝试monkeypatchingDelayed::W
我想在Ruby中创建一个用于开发目的的极其简单的Web服务器(不,不想使用现成的解决方案)。代码如下:#!/usr/bin/rubyrequire'socket'server=TCPServer.new('127.0.0.1',8080)whileconnection=server.acceptheaders=[]length=0whileline=connection.getsheaders想法是从命令行运行这个脚本,提供另一个脚本,它将在其标准输入上获取请求,并在其标准输出上返回完整的响应。到目前为止一切顺利,但事实证明这真的很脆弱,因为它在第二个请求上中断并出现错误:/usr/b
我意识到这可能是一个非常基本的问题,但我现在已经花了几天时间回过头来解决这个问题,但出于某种原因,Google就是没有帮助我。(我认为部分问题在于我是一个初学者,我不知道该问什么......)我也看过O'Reilly的RubyCookbook和RailsAPI,但我仍然停留在这个问题上.我找到了一些关于多态关系的信息,但它似乎不是我需要的(尽管如果我错了请告诉我)。我正在尝试调整MichaelHartl'stutorial创建一个包含用户、文章和评论的博客应用程序(不使用脚手架)。我希望评论既属于用户又属于文章。我的主要问题是:我不知道如何将当前文章的ID放入评论Controller。
我的工作要求我为某些测试自动生成电子邮件。我一直在四处寻找,但未能找到可以快速实现的合理解决方案。它需要在outlook而不是其他邮件服务器中,因为我们有一些奇怪的身份验证规则,我们需要保存草稿而不是仅仅发送邮件的选项。显然win32ole可以做到这一点,但我找不到任何相当简单的例子。 最佳答案 假设存储了Outlook凭据并且您设置为自动登录到Outlook,WIN32OLE可以很好地完成此操作:require'win32ole'outlook=WIN32OLE.new('Outlook.Application')message=
华为OD机试题本篇题目:明明的随机数题目输入描述输出描述:示例1输入输出说明代码编写思路最近更新的博客华为od2023|什么是华为od,od薪资待遇,od机试题清单华为OD机试真题大全,用Python解华为机试题|机试宝典【华为OD机试】全流程解析+经验分享,题型分享,防作弊指南华为o