Elasticsearch 查询

Elasticsearch 查询

1.新建一个数据库,将 在Redis模糊查询案例中的分词,导入到新的数据库

image-20200915095045365

image-20200915095103445

2. 新建springboot工程,逆向工程连接数据库生成controller,service,dao层

​ spring版本为1.5

3. 下载Elasticsearch 并运行

​ Elasticsearch版本号为2.4.6

​ 解压后,在bin目录下,点击elasticsearch.bat 运行

image-20200915095823411

在浏览器输入http://localhost:9200/

image-20200915100016357

4. 把数据从数据库导入到Elasticsearch

  1. extends ElasticsearchRepository(本文使用)
  2. 注入elasticsearchTemplate

新建一个package

image-20200915102135032

public interface PoetryRepository extends ElasticsearchRepository<EsPoetry, Integer{
    //按content做模糊查询
    public List<EsPoetry> findByContentIn(String word);
}

@Document(indexName = "test", type = "poetry")
public class EsPoetry implements Serializable {
    @Id
    private Integer id;

    private String title;

    private String author;

    private String kind;

    private String intro;

    @Field(type = FieldType.String, analyzer = "ik_smart")//后续加入了ik分词器在serch里
    private String content;

    private static final long serialVersionUID = 1L;

    /**
     * @return id
     */
    public Integer getId() {
        return id;
    }

    /**
     * @param id
     */
    public void setId(Integer id) {
        this.id = id;
    }

    /**
     * @return title
     */
    public String getTitle() {
        return title;
    }

    /**
     * @param title
     */
    public void setTitle(String title) {
        this.title = title;
    }

    /**
     * @return author
     */
    public String getAuthor() {
        return author;
    }

    /**
     * @param author
     */
    public void setAuthor(String author) {
        this.author = author;
    }

    /**
     * @return kind
     */
    public String getKind() {
        return kind;
    }

    /**
     * @param kind
     */
    public void setKind(String kind) {
        this.kind = kind;
    }

    /**
     * @return intro
     */
    public String getIntro() {
        return intro;
    }

    /**
     * @param intro
     */
    public void setIntro(String intro) {
        this.intro = intro;
    }

    /**
     * @return content
     */
    public String getContent() {
        return content;
    }

    /**
     * @param content
     */
    public void setContent(String content) {
        this.content = content;
    }
}

@Document(indexName = “knowledge”,type=“poetry”)

​ 这个注解是ElasticsearchRepository要求管理的实体类需要打上这个注解,

  • indexName是索引名,对应关系型数据库的db名

  • type在非关系型数据库叫数据类型,对应关系型数据库的table名

在controller层写逻辑,将数据库的数据导入Elastic

@Controller
@RequestMapping("/moon/Poetry")
public class PoetryController extends BaseController {
    @Autowired
    private PoetryService poetryService;
    @Autowired
    private PoetryRepository poetryRepository;

    @RequestMapping("/initdata")
    public String initdata() {
        List<Poetry> list = poetryService.getPoetryMapper().selectAll();
        list.forEach(o -> {
            EsPoetry esPoetry = new EsPoetry();
            BeanUtils.copyProperties(o, esPoetry);
            poetryRepository.save(esPoetry);
            System.out.println("导入到es:" + esPoetry.getTitle());
        });
        return "demo/success";
    }
}

运行成功后

打印

导入到es:感遇·其一
导入到es:塞下曲·其一
导入到es:古从军行
导入到es:感遇·其二
    ...

5. 写一个模糊查询

一个搜索的页面代码

<div class="form-inline mt-2">
        <input id="word" type="text" class="form-control form-control-sm" placeholder="_输入关键词"/>
        <button class="btn btn-sm btn-outline-success ml-1" @click="showdata();">查询</button>
</div>
 <div>
        <div class="card border-light m-1" v-for="p in poetrydata">
            <div class="card-header font-weight-bold">{{p.title}}</div>
            <div class="card-body">{{p.content}}</div>
        </div>
</div>
<script th:inline="javascript">
    //app
    var app = new Vue({
        el: "#app",
        data: {
            poetrydata: []
        },
        methods: {
            showdata: function () {
                $.post("/moon/Poetry/selectByWord", {word: $("#word").val()}, function (data) {
                    app.poetrydata = data;
                });
            }
        }
    });
</script>

controller

  //模糊查询
    @RequestMapping("/selectByWord")
    @ResponseBody
    public List<EsPoetry> selectByWord(String word) {
        if (word == null) {
            return null;
        }
        return poetryRepository.findByContentIn(word);
        //return null;
    }

运行后

image-20200915110458543

6.加入ik分词器

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值