wildcard
通配符查询,其中【?】代表任意一个字符【*】代表任意的一个或多个字符,例如我们想查名字结尾为林的文档:
{ "query": { "wildcard": { "name": "*林" } } }
返回结果为:
{ "took": 19, "timed_out": false, "_shards": { "total": 3, "successful": 3, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 3, "relation": "eq" }, "max_score": 1.0, "hits": [ { "_index": "tml-userinfo", "_type": "_doc", "_id": "5", "_score": 1.0, "_source": { "age": 28, "sex": "男", "name": "李小林" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "2", "_score": 1.0, "_source": { "age": 28, "sex": "男", "name": "森小林" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "6", "_score": 1.0, "_source": { "age": 28, "sex": "男", "name": "李小林" } } ] } }
prefix
前缀查询,我们为了找到所有姓名以森开头的文档,可以使用这种方式:
{ "query": { "prefix": { "name": "森" } } }
返回结果为:
{ "took": 4, "timed_out": false, "_shards": { "total": 3, "successful": 3, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 6, "relation": "eq" }, "max_score": 1.0, "hits": [ { "_index": "tml-userinfo", "_type": "_doc", "_id": "2", "_score": 1.0, "_source": { "age": 28, "sex": "男", "name": "森小林" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "3", "_score": 1.0, "_source": { "age": 8, "sex": "男", "name": "森小贤" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "1", "_score": 1.0, "_source": { "age": 18, "sex": "男", "name": "森小辰" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "8", "_score": 1.0, "_source": { "age": 8, "sex": "女", "name": "森小美" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "9", "_score": 1.0, "_source": { "age": 18, "sex": "女", "name": "森小玲" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "11", "_score": 1.0, "_source": { "age": 28, "sex": "女", "name": "森小捷" } } ] } }
regexp
顾明思意,ES兼容了正则的查询方式,例如我们想查询性别为汉字字符的文档,为了验证,所以我们插入一条数据:
查询语句为
{ "query": { "regexp": { "sex": "[\u4e00-\u9fa5]" } } }
检索结果为13条,除了性别为空的和英文的都检索到了
{ "took": 9, "timed_out": false, "_shards": { "total": 3, "successful": 3, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 13, "relation": "eq" }, "max_score": 1.0, "hits": [ { "_index": "tml-userinfo", "_type": "_doc", "_id": "5", "_score": 1.0, "_source": { "age": 28, "sex": "男", "name": "李小林" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "7", "_score": 1.0, "_source": { "age": 8, "sex": "女", "name": "李小美" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "13", "_score": 1.0, "_source": { "age": 18, "sex": "女", "name": "李小男" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "2", "_score": 1.0, "_source": { "age": 28, "sex": "男", "name": "森小林" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "3", "_score": 1.0, "_source": { "age": 8, "sex": "男", "name": "森小贤" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "4", "_score": 1.0, "_source": { "age": 8, "sex": "男", "name": "李小贤" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "10", "_score": 1.0, "_source": { "age": 18, "sex": "女", "name": "李小玲" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "12", "_score": 1.0, "_source": { "age": 18, "sex": "男", "name": "李小辰" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "1", "_score": 1.0, "_source": { "age": 18, "sex": "男", "name": "森小辰" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "6", "_score": 1.0, "_source": { "age": 28, "sex": "男", "name": "李小林" } } ] } }
fuzzy
fuzzy 纠错检索,让输入条件有容错性,例如我要检索性别为woman的数据,但是我拼错了,输入的是wman,用fuzzy照样可以检索到:
{ "query": { "fuzzy": { "sex": "wman" } } }
返回结果为:
{ "took": 26, "timed_out": false, "_shards": { "total": 3, "successful": 3, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 1, "relation": "eq" }, "max_score": 1.2554823, "hits": [ { "_index": "tml-userinfo", "_type": "_doc", "_id": "15", "_score": 1.2554823, "_source": { "age": 18, "sex": "woman", "name": "赵小男" } } ] } }
复合查询
复合查询通俗的说就是多个条件拼接查询,就是用Bool去拼接一系列的查询条件,来完成表达式的查询方式,其实就是将普通条件进行重新组合,常用的有四种复合类型:
- filter:只过滤符合条件的文档,不计算相关系得分,但因为有缓存,所以性能高
- must:用must连接的多个条件必须都满足,是and的关系,逻辑&与的关系。
- should:用should连接的多个条件只要满足一个即可,是or的关系,逻辑||或的关系
- must_not:用must_not绑定的条件表示一定不能满足该条件,是not的关系,逻辑^非的关系。
用这些条件的连接词将多个查询条件连接起来就能进行复杂的复合查询了。
以下是一个bool过滤器的组成格式
{ "bool" : { "must" : [], "should" : [], "must_not" : [], "filter": [] } }