Java中使用ES引擎

一:在springboot中引入ES,需要安装配置(后面再补)

二:使用说明

场景:用户端输入信息,使用ES引擎获取相关数据。

(1)存储用户输入的查询信息,params

Map<String,Object> params = new HashMap<>();
params.put("location",location);
params.put("searchParam",inputStr);

(2)向ES传入用户输入的参数params

ClientInterface clientUtil = bossESStarter.getConfigRestClient("mapper/es/questionAns.xml"); //xml文件中配置了查询语句
MapRestResponse response = null;
response = clientUtil.search("questionans/_search","getSelected",params);//第一个参数是索引和使用查询,第二个参数是调用的查询方法。

(3)对返回结果进行处理,得到resultList链表。
 MapSearchHits searchHits =response.getSearchHits();
        //命中的数量
        Integer total = (Integer) searchHits.getTotal();
        //获取的数据如下
        List<MapSearchHit> hitlist = searchHits.getHits();
        //创建结果
        List<Object> resultList = new ArrayList<>();
        //需要获取哪些数据
        hitlist.forEach(
                value->
                {
                    resultList.add(value.getSource());
                }
        );

 

 


 最后,附上xml文件。这里值得解释的是,ES的语法。

bool 过滤

bool 过滤可以用来合并多个过滤条件查询结果的布尔逻辑,它包含一下操作符:

  • must :: 多个查询条件的完全匹配,相当于 and。
  • must_not :: 多个查询条件的相反匹配,相当于 not。
  • should :: 至少有一个查询条件匹配, 相当于 or。

下图中的语句,表示输入的location要完全匹配。输入的searchParam匹配问题和答案之中的一个就可以了。

 在上图的语句中加入一个location是否为*的判定,如果location是* 的话,要求通配所有的location。

 

 


 




posted on 2020-08-13 09:43  毛无语666  阅读(3211)  评论(4编辑  收藏  举报

导航