package com.tofly.loges.controller;

import com.tofly.loges.entity.TestEntity;
import com.tofly.loges.service.TestEntityRepository;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author tuonbed
 * @date 2020/4/22  10:12
 */
@RestController
public class MyTestController {

    @Autowired
    private TestEntityRepository testEntityRepository;
    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    @RequestMapping("/csadd")
    public String csadd(){
        TestEntity testEntity=new TestEntity();
        testEntity.setId(2);
        testEntity.setName("adasdasd");
        testEntity.setNotes("bbbbbbbbbbbbbb");
        testEntityRepository.save(testEntity);
        return "success";
    }

    @GetMapping("/get")
    public List get(){
        TestEntity testEntity = testEntityRepository.findById(1).get();
        testEntityRepository.findAll();
        SearchQuery searchQuery=new NativeSearchQueryBuilder()
                .withQuery(QueryBuilders.matchQuery("name","adasdasd")).build();

        Page<TestEntity> search = testEntityRepository.search(searchQuery);
        List<TestEntity> list = search.getContent();
        return list;
    }
}
