第九章:深入搜索--结构化搜索

一.精确搜索,多个精确搜索

 1.term 查询

    term 查询可以用它处理数字(numbers)、布尔值(Booleans)、日期(dates)以及文本(text).

使用bulk创建文件文档

 POST /my_store/products/_bulk
{ "index": { "_id": 1 }}
{ "price" : 10, "productID" : "XHDK-A-1293-#fJ3" }
{ "index": { "_id": 2 }}
{ "price" : 20, "productID" : "KDKE-A-9947-#kL5" }
{ "index": { "_id": 3 }}
{ "price" : 30, "productID" : "JODL-X-1937-#pV7" }
{ "index": { "_id": 4 }}
{ "price" : 30, "productID" : "QQPX-R-3956-#aD8" }
{ "index": { "_id": 5 }}
{ "price" : 40, "productID" : "你好ds" }

term处理数字 :在 Elasticsearch 的查询表达式(query DSL)中,使用 term 查询会查找我们指定的精确值。

constant_score : 查询以非评分模式

GET /my_store/products/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "term": {
          "price":40
        }
      }
    }
  }
}

term处理文本: 在处理文本的时候,ES的analyze API会把文本才分成一个单词,大写的会变成小写,特殊的字符会进行过滤

例如 :"XHDK-A-1293-#fJ3" 会才分成  "xhdk", "a","1293","fj3"

可以使用 GET /{索引}/_analyze 语法进行查询才分的情况,如下

GET /my_store/_analyze
{
  "field": "productID",
  "text": "XHDK-A-1293-#fJ3"
}

term的文本查询

GET /my_store/products/_search
{
    "query" : {
        "_score" : {
            "filter" : {
                "terms" : {
                    "productID": ["a","你"]
                }
            }
        }
    }
}

2.多个精确搜索

多个精确搜索,只要在 term 后面加个s

GET /my_store/products/_search
{
    "query" : {
        "constant_score" : {
            "filter" : {
                "terms" : {
                    "price" : [20, 30]
                }
            }
        }
    }
}

二.组合过滤器

1.bool查询

bool (布尔)过滤器。 这是个 复合过滤器(compound filter) ,它可以接受多个其他过滤器作为参数,并将这些过滤器结合成各式各样的布尔(逻辑)组合。

bool 过滤器由三部分组成:

{
   "bool" : {
      "must" :     [],
      "should" :   [],
      "must_not" : [],
   }
}

must : 所有的语句都 必须(must) 匹配,与 AND 等价。

must_not : 所有的语句都 不能(must not) 匹配,与 NOT 等价。

should : 至少有一个语句要匹配,与 OR 等价.

如此sql:

SELECT product FROM products

WHERE (price = 20 OR productID = "XHDK-A-1293-#fJ3")

AND (price != 30)

Elasticsearch中用bool表达此sql的语句 如下:

GET /my_store/products/_search
{
   "query" : {
     "constant_score": {
         "filter" : {
            "bool" : {
              "should" : [//自少有一个条件符合
                 { "term" : {"price" : 20}}, 
                 { "term" : {"productID" : "fj3"}} 
              ],
              "must_not" : {//排除条件
                 "term" : {"price" : 30} 
              }
           }
         }
     }
   }
}

三.范围查询

range 查询

range 查询可同时提供包含(inclusive)和不包含(exclusive)这两种范围表达式,可供组合的选项如下:

  • gt: > 大于(greater than)
  • lt: < 小于(less than)
  • gte: >= 大于或等于(greater than or equal to)
  • lte: <= 小于或等于(less than or equal to)

如下事例:

GET /my_store/products/_search
{
    "query" : {
        "constant_score" : {
            "filter" : {
                "range" : {
                    "price" : {
                        "gte" : 20,
                        "lt"  : 40
                    }
                }
            }
        }
    }
}

四.处理NULL值

1.exists 

 exists :判断不是NULL   与sql的 is not null 相同 

 使用ES的exists 表示 tags IS NOT NULL的判断

SELECT tags
FROM   posts
WHERE  tags IS NOT NULL

如下

GET /my_index/posts/_search
{
    "query" : {
        "constant_score" : {
            "filter" : {
                "exists" : { "field" : "tags" }
            }
        }
    }
}

2.missing :判断是NULL   与sql的 is  null 相同 

 使用ES的missing 表示 tags IS  NULL的判断

SELECT tags
FROM   posts
WHERE  tags IS NULL

如下:

GET /my_index/posts/_search
{
    "query" : {
        "constant_score" : {
            "filter": {
                "missing" : { "field" : "tags" }
            }
        }
    }
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值