elasticsearch处理map请求代码(适用组合条件查询)

public class ElaticsearchBiz {

    @Autowired
    JestClient jestClient;


    public MsgDto findBrandCategory(Map<String,Object> map) {
        String id =null;
        String name =null;
        Map<String,String> mm ;
        Map<String,Object> mm1 ;
        ListString,Object>> list = new ArrayList<>();
        for(Map.Entry<String,Object> entry:map.entrySet())
        {
            mm = new HashMap<>();
            mm.put(entry.getKey().toString().toLowerCase(),entry.getValue().toString());
            mm1 = new HashMap<>();
            if(1==1) //这里根据实际需要 判断是不是分词
                mm1.put("match",mm);
            else mm1.put("term",mm);
            list.add(mm1);

        }

        Map<String,Object> mm2  = new HashMap<>();
        mm2.put("must",list);
        Map<String,Object> mm3  = new HashMap<>();
        mm3.put("bool",mm2);
        Map<String,Object> mm4  = new HashMap<>();
        mm4.put("query",mm3);
        JSONObject jsonObject = JSONObject.fromObject(mm4);
       String aa =jsonObject.toString();
        Search search = new Search.Builder(aa)
                .addIndex("threees").addType("orgbrand").build();

           return ElasticSearch(search);

    }

    public MsgDto ElasticSearch(Search search){
        try {
            JestResult result = jestClient.execute(search);
            Map<String, Object> map = new HashMap<>();
            return new MsgDto(MsgCode.SUCCESS_CODE, "查询成功", true, result.getSourceAsObjectList(map.getClass()));
        }catch (IOException e){
            e.printStackTrace();
            return null;
        }
    }
}

你可能感兴趣的:(elasticsearch处理map请求代码(适用组合条件查询))