官方文档地址
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html
JavaClient 查询ES
QueryBuilder queryBuilder = QueryBuilders.termQuery("字段","term值"); SearchResponse response = client.prepareSearch("索引名称").setTypes("type名称").setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setQuery(queryBuilder).execute().actionGet(); //获取命中数 System.out.println(response.getHits().totalHits()); //获取响应字符串 System.out.println(response.toString()); //遍历查询结果输出相关度分值和文档内容 SearchHits searchHits = response.getHits(); for(SearchHit searchHit : searchHits){ System.out.println(searchHit.getScore()); System.out.println(searchHit.getSourceAsString()); }
1、term query 分词精确查询
查询hotelName 分词后包含 hotel的term的文档
QueryBuilders.termQuery("hotelName","hotel")
2、terms Query 多term查询
查询hotelName 包含 hotel 或test 中的任何一个或多个的文档
QueryBuilders.termsQuery("hotelName","hotel","test")
3、range query范围查询
QueryBuilders.rangeQuery("hotelNo").gt("10143262306"); //大于 10143262306 QueryBuilders.rangeQuery("hotelNo").lt("101432623062055348221"); //小于 101432623062055348221 QueryBuilders.rangeQuery("hotelNo").includeLower(true); //包括下界 QueryBuilders.rangeQuery("hotelNo").includeUpper(false); //包括上界
4、 exist query 查询字段不为null的文档
查询字段address 不为null的数据
QueryBuilders.existsQuery("address")
5、missing query返回没有字段或值为null或没有值的文档
java client 该方法已经标记为过时,推荐用exist代替 如下 existsQuery BoolQueryBuilder#mustNot(QueryBuilder) QueryBuilders.missingQuery("accountGuid") 等同 QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("accountGuid"));
6.prefix query 匹配分词前缀
如果字段没分词,就匹配整个字段前缀
QueryBuilders.prefixQuery("hotelName","花园")
7、wildcard query 通配符查询
支持* 任意字符串;?任意一个字符
QueryBuilders.wildcardQuery("channelCode","ctr*") QueryBuilders.wildcardQuery("channelCode","ctr?")
8、regexp query 正则表达式匹配分词
正则表达式自己写吧 略
9、 fuzzy query 分词模糊查询
通过增加fuzziness 模糊属性,来查询term 如下 能够匹配 hotelName 为 te el tel前或后加一个字母的term的 文档 fuzziness 的含义是检索的term 前后增加或减少n个单词的匹配查询
QueryBuilders.fuzzyQuery("hotelName", "tel").fuzziness(Fuzziness.ONE)
10、type Query
英文原文:Filters documents matching the provided document / mapping type.
QueryBuilders.typeQuery("channelCode")
11、idx Query 根据ID查询
QueryBuilders.idsQuery().addIds("exchange_operate_monitor_db$32293","exchange_operate_monitor_db$32294")