ElasticSearch基本操作

@SpringBootTest@RunWith(SpringRunner.class)public class ItemTest { @Autowired private ElasticsearchTemplate elasticsearchTemplate; @Autowired private ItemRepository itemRepositor...
摘要由CSDN通过智能技术生成

@SpringBootTest
@RunWith(SpringRunner.class)
public class ItemTest {

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    @Autowired
    private ItemRepository itemRepository;

    /**
     * Class对象创建索引
     */
    @Test
    public void testDemo() {
        this.elasticsearchTemplate.createIndex(Item.class); //创建索引,根据@Document注解创建
        this.elasticsearchTemplate.putMapping(Item.class);
        //映射索引,根据@id和@field来映射

    }

    /**
     * String形式创建索引库
     */
    @Test
    public void testDemo1() {
        Settings setting = Settings.builder().put("number_of_replicas", 0).put("number_of_shards", 5).build();
        this.elasticsearchTemplate.createIndex("love", setting);
    }

    /**
     * 测试删除索引库,
     * [Class<T>] /[String indexName]
     */
    @Test
    public void deleteDemo() {
//        this.elasticsearchTemplate.deleteIndex(Item.class);
        this.elasticsearchTemplate.deleteIndex("love");
    }


    /**
     * 使用Repository来新增文档
     */
    @Test
    public void repositoryAddIndexName() {
        Item item = new Item();
        item.setId(88888L);
        item.setBrand("小米");
        item.setCategory("手机");
        item.setTitle("小米手机");
        item.setImages("http://image.leyou.com/123456.jpg");
        item.setPrice(1999d);
        itemRepository.save(item);
    }

    @Test
    public void repositoryAddListIndexName() {
        ArrayList<Item> list = new ArrayList<>();
        list.add(new Item(2L, "坚果手机R1", " 手机", "锤子", 3699.00, "http://image.leyou.com/123.jpg"));
        list.add(new Item(3L, "华为META10", " 手机", "华为", 4499.00, "http://image.l
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Elasticsearch是一个开源的分布式搜索和分析引擎,它可以用于存储、搜索和分析大量的数据。以下是一些Elasticsearch基本操作: 1. 安装和启动Elasticsearch:首先,你需要从Elasticsearch官方网站下载和安装Elasticsearch。安装完成后,你可以使用命令行或者图形界面来启动Elasticsearch。 2. 创建索引:在Elasticsearch中,数据存储在索引中。你可以使用PUT请求来创建一个新的索引。例如,使用curl命令可以发送以下请求来创建一个名为"my_index"的索引: ``` curl -XPUT 'localhost:9200/my_index' ``` 3. 添加文档:一旦索引创建好了,你可以使用POST请求来向索引中添加文档。文档是以JSON格式表示的数据。以下是向名为"my_index"的索引添加一个文档的示例请求: ``` curl -XPOST 'localhost:9200/my_index/_doc' -d ' { "title": "Elasticsearch Basics", "content": "This is a basic introduction to Elasticsearch" }' ``` 4. 搜索文档:你可以使用GET请求来搜索索引中的文档。以下是一个搜索名为"my_index"的索引中包含关键字"elasticsearch"的文档的示例请求: ``` curl -XGET 'localhost:9200/my_index/_search?q=elasticsearch' ``` 5. 更新文档:使用POST请求可以更新索引中的文档。以下是更新名为"my_index"的索引中ID为1的文档的示例请求: ``` curl -XPOST 'localhost:9200/my_index/_doc/1/_update' -d ' { "doc": { "content": "This is an updated content" } }' ``` 6. 删除文档:使用DELETE请求可以删除索引中的文档。以下是删除名为"my_index"的索引中ID为1的文档的示例请求: ``` curl -XDELETE 'localhost:9200/my_index/_doc/1' ``` 这些是Elasticsearch的一些基本操作。你可以根据需要进一步探索和学习更多高级功能和API。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值