package com.elab.es.data;

import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
import com.alibaba.druid.sql.parser.SQLStatementParser;
import com.alibaba.fastjson.JSON;
import com.elab.es.App;
import com.elab.es.model.Posts;
import org.elasticsearch.index.query.MatchPhraseQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.zxp.esclientrhl.enums.SqlFormat;
import org.zxp.esclientrhl.index.ElasticsearchIndex;
import org.zxp.esclientrhl.repository.*;

import java.util.List;

/**
 * 模版工具类测试
 *
 * @author Liukx
 * @create 2020-03-24 13:51
 * @email liukx@elab-plus.com
 **/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class)
public class ElasticsearchTemplateImplCase {
    @Autowired
    private ElasticsearchTemplateImpl<Posts, String> searchTemplate;

    @Autowired
    private ElasticsearchIndex<Posts> searchIndex;

    /**
     * 新增一条数据
     *
     * @throws Exception
     */
    @Test
    public void save() throws Exception {
        Posts posts = new Posts("测试", "test data!!");
        boolean save = searchTemplate.save(posts);
    }

    @Test
    public void delete() throws Exception {
        boolean b = searchTemplate.deleteById("b6f35bd3-3e63-402f-90ee-3668fb87272a", Posts.class);
        System.out.println(b);
    }

    @Test
    public void querySQL() throws Exception {
//        String sql = "select message from posts limit 5";
//        String sql = "select message from posts where user like '%某'";
//        String sql = "select message from posts where user in ('某某某','测试')";
        String sql = "SELECT postDate, id, message, user FROM posts WHERE user = '某某某'";
        SQLStatementParser parser = new MySqlStatementParser(sql);
        SQLStatement statement = parser.parseStatement();
        MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
        statement.accept(visitor);

        String s = searchTemplate.queryBySQL(sql, SqlFormat.JSON);
        String s1 = searchTemplate.queryBySQL(sql, SqlFormat.TXT);
        String s2 = searchTemplate.queryBySQL(sql, SqlFormat.CBOR);
        String s3 = searchTemplate.queryBySQL(sql, SqlFormat.YAML);
        String s4 = searchTemplate.queryBySQL(sql, SqlFormat.SMILE);
        String s5 = searchTemplate.queryBySQL(sql, SqlFormat.TSV);
        System.out.println(s);
    }

    @Test
    public void queryLike() throws Exception {
        MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("message", "我");

        List<Posts> search = searchTemplate.search(matchQueryBuilder, Posts.class);
        System.out.println(JSON.toJSONString(search));
    }

    @Test
    public void queryHight() throws Exception {
        MatchPhraseQueryBuilder matchPhraseQueryBuilder = QueryBuilders.matchPhraseQuery("message", "我");
        List<Posts> search = searchTemplate.search(matchPhraseQueryBuilder, Posts.class);
        System.out.println(JSON.toJSONString(search));
    }

    @Test
    public void existsIndex() throws Exception {
        boolean exists = searchIndex.exists(Posts.class);
        System.out.println(exists);
    }


    @Test
    public void pageSearch1() throws Exception {
        List<Posts> postses = searchTemplate.searchMore(QueryBuilders.matchAllQuery(), 2, Posts.class);
        System.out.println(JSON.toJSONString(postses));
    }

    @Test
    public void pageSearch2() throws Exception {
        //定制分页信息
        int currentPage = 1;
        int pageSize = 10;
        PageSortHighLight psh = new PageSortHighLight(currentPage, pageSize);
        //排序字段，注意如果proposal_no是text类型会默认带有keyword性质，需要拼接.keyword
        String sorter = "postDate";
        Sort.Order order = new Sort.Order(SortOrder.ASC, sorter);
        psh.setSort(new Sort(order));
        //定制高亮，如果定制了高亮，返回结果会自动替换字段值为高亮内容
        HighLight message = new HighLight().field("message");
        //可以单独定义高亮的格式
        message.setPreTag("<em>");
        message.setPostTag("</em>");
        psh.setHighLight(message);

        PageList<Posts> pageList = new PageList<>();
        MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("message", "我");
        pageList = searchTemplate.search(matchQueryBuilder, psh, Posts.class);
        System.out.println(JSON.toJSONString(pageList));
    }

    @Test
    public void queryBuilders() throws Exception {
        // 精准查询 如果不加.keyword就会变成模糊查询
//        QueryBuilder queryBuilder = QueryBuilders.termQuery("message.keyword", "我");
        //必须相邻的查询条件
//        QueryBuilder queryBuilder = QueryBuilders.matchPhraseQuery("message", "圈圈");

        QueryBuilder queryBuilder = QueryBuilders.matchPhraseQuery("message", "深深你").slop(3);


        List<Posts> search = searchTemplate.search(queryBuilder, Posts.class);
        System.out.println(JSON.toJSONString(search));
    }

}
