一、概述
查询所使用的 QueryBuilders来源于以下
import static org.elasticsearch.index.query.QueryBuilders.*;
请注意,您可以使用QueryBuilder对象上的toString()方法轻松打印(也称为调试)JSON生成的查询。
1.1、匹配所有
最简单的查询,匹配所有文档,给它们一个_score 1.0。
QueryBuilder qb = matchAllQuery();
1.2、全文查询
高级全文查询通常用于在全文字段(如电子邮件正文)上运行全文查询。他们了解如何分析被查询的字段,并在执行之前将每个字段的分析器(或search_analyzer)应用于查询字符串。
该组中的查询是:
1)匹配查询:用于执行全文查询的标准查询,包括模糊匹配和短语或邻近查询。//查询出包含 value 的数据 或者包含不连续的字符
QueryBuilder qb = matchQuery("field","value" );
示例:查询:QueryBuilder qb = matchQuery("name", "张三");
结果
数据:{"address":"benjing","age":"12","name":"张三2"} 数据:{"address":"benjing","age":"12","name":"aa张三3","num":12} 数据:{"address":"benjing","age":"12","name":"张爱三","num":12} 数据:{"address":"benjing","age":"12","name":"张三3"} 数据:{"address":"benjing","age":"12","name":"李宏旭 张三3","num":12} 数据:{"address":"benjing","age":"12","name":"张四","num":12}
2)多匹配查询:multi_match查询,匹配查询的多字段版本。
QueryBuilder qb = multiMatchQuery( "kimchy elasticsearch", //值 "user", "message" //字段 );
示例:查询:QueryBuilder qb = multiMatchQuery("张三 12", "name","age");
结果
数据:{"address":"benjing","age":"1","name":"张三"} 数据:{"address":"benjing","age":"12","name":"张三2"} 数据:{"address":"benjing","age":"12","name":"aa张三3","num":12} 数据:{"address":"benjing","age":"12","name":"张爱三","num":12} 数据:{"address":"benjing","age":"12","name":"张三3"} 数据:{"address":"benjing","age":"12","name":"李宏旭 张三3","num":12} 数据:{"address":"benjing","age":"12","name":"王五","num":3} 数据:{"address":"benjing","age":"12","name":"张四","num":12} 数据:{"address":"benjing","age":"12","name":"王五"}
3)common_terms查询:一个更专业的查询,它提供了对不常见单词的更多偏好。
QueryBuilder qb = commonTermsQuery("field","value");
示例:查询:QueryBuilder qb = commonTermsQuery("name", "张三");
结果:
数据:{"address":"benjing","age":"1","name":"张三"} 数据:{"address":"benjing","age":"12","name":"张三2"} 数据:{"address":"benjing","age":"12","name":"aa张三3","num":12} 数据:{"address":"benjing","age":"12","name":"张爱三","num":12} 数据:{"address":"benjing","age":"12","name":"张三3"} 数据:{"address":"benjing","age":"12","name":"李宏旭 张三3","num":12}
4)query_string查询:支持紧凑的Lucene查询字符串语法,允许您在单个查询字符串中指定AND | OR | NOT条件和多字段搜索。仅限专家用户。
QueryBuilder qb = queryStringQuery("+kimchy -elasticsearch"); //value
5)simple_query_string:query_string语法的更简单,更健壮的版本,适合直接向用户公开。
QueryBuilder qb = simpleQueryStringQuery("+kimchy -elasticsearch");//value
github:https://github.com/bjlhx15/spring-cloud-base/tree/master/service6-es