elasticsearch之结构化查询

#################结构化查询#################

结构化搜索,是指对结构化数据的搜索 日期、布尔类型和数字都是结构化的

ES中的结构化

布尔,时间,日期和数字之类的结构化数据,有精确的格式,我们可以对这些格式进行逻辑操作,包括比较数字或时间的范围,或判断俩个值的大小,
结构化的文本可以做精确匹配或者部分匹配 term查询/prefix前缀查询
结构哈的结果只有是 或 否 俩个值 也可以根据需要,决定是否对结构化搜索进行打分
#结构化搜索,精确匹配
DELETE products
POST /products/_bulk
{ "index": { "_id": 1 }}
{ "price" : 10,"avaliable":true,"date":"2018-01-01", "productID" : "XHDK-A-1293-#fJ3" }
{ "index": { "_id": 2 }}
{ "price" : 20,"avaliable":true,"date":"2019-01-01", "productID" : "KDKE-B-9947-#kL5" }
{ "index": { "_id": 3 }}
{ "price" : 30,"avaliable":true, "productID" : "JODL-X-1937-#pV7" }
{ "index": { "_id": 4 }}
{ "price" : 30,"avaliable":false, "productID" : "QQPX-R-3956-#aD8" }


GET products/_mapping

对布尔值 term查询,有算分

https://blog.csdn.net/lijingjingchn/article/details/88896389

使用profile可以看到一个搜索聚合请求,是如何拆分成底层的 Lucene 请求,并且显示每部分的耗时情况。
explain方法,能够解释Document的Score是怎么得来的,具体每一部分的得分都可以详细地打印出来

下面的查询可以使用通过文本的结构化查询 匹配到 置为false的数据

POST products/_search
{
  "profile": "true",
  "explain": true,
  "query": {
    "term": {
      "avaliable": {
        "value": "false"
      }
    }
  }
}

不计算得分匹配符合条件的数据 通过constant score 转成 filtering,没有算分

POST products/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "term": {
          "avaliable": "false"
        }
      },
      "boost": 1.2
    }
  }
}

数字的结构化查询 数字精确匹配

数字term

POST products/_search
{
  "query": {
    "term": {
      "price": {
        "value": "30"
      }
    }
  }
}

数字terms 单词多字段查询

POST products/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "terms": {
          "price": [
            "20",
            "30"
          ]
        }
      },
      "boost": 1.2
    }
  }
}

数字range 查询

GET products/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "range": {
          "price": {
            "gte": 20,
            "lte": 30
          }
        }
      },
      "boost": 1.2
    }
  }
}

exists查询 查询出索引中存在date字段的数据

POST products/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "exists": {
          "field": "date"
        }
      },
      "boost": 1.2
    }
  }
}


#处理多值字段
POST /movies/_bulk
{ "index": { "_id": 1 }}
{ "title" : "Father of the Bridge Part II","year":1995, "genre":"Comedy"}
{ "index": { "_id": 2 }}
{ "title" : "Dave","year":1993,"genre":["Comedy","Romance"] }

#处理多值字段,term 查询是包含,而不是等于

处理数组类型多值字段,term查询会找出包含该字段的数据。而不是而且等于

POST movies/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "term": {
          "genre.keyword": "Comedy"
        }
      },
      "boost": 1.2
    }
  }
}

#字符类型 terms 多字段进行匹配
POST products/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "terms": {
          "productID.keyword": [
            "QQPX-R-3956-#aD8",
            "JODL-X-1937-#pV7"
          ]
        }
      }
    }
  }
}

profile 底层如何进行查询的

explain 底层如何计算得分的

对于日期类型他底层是不计算得分的。进行结构化查询,找出符合条件的数据

POST products/_search
{
  "profile": "true",
  "explain": true,
  "query": {
    "term": {
      "date": {
        "value": "2019-01-01"
      }
    }
  }
}

POST products/_search
{
  "profile": "true",
  "explain": true,
  "query": {
    "match": {
      "date": "2019-01-01"
    }
  }
}


### 组合查询,找出不包含date字段的数据
POST products/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "bool": {
          "must_not": {
            "exists": {
              "field": "date"
            }
          }
        }
      },
      "boost": 1.2
    }
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值