美文网首页
es 调试 相关语句记录

es 调试 相关语句记录

作者: 张清柏 | 来源:发表于2020-04-27 17:53 被阅读0次

查看mapping结构

{
  "xcx_doctor1587975775" : {
    "mappings" : {
      "doctor" : {
        "properties" : {
          "adept_introduction" : {
            "type" : "text",
            "analyzer" : "ik_smart"
          },
          "answerauth" : {
            "type" : "keyword"
          },
          "authen_status" : {
            "type" : "short"
          },
          "big_head_image" : {
            "type" : "keyword"
          },
          "cnk_dept1_id" : {
            "type" : "integer"
          },
          "cnk_dept1_name" : {
            "type" : "keyword",
            "index" : false
          },
          "cnk_dept2_id" : {
            "type" : "integer"
          },
          "cnk_dept2_name" : {
            "type" : "keyword",
            "index" : false
          },
          "create_time" : {
            "type" : "date",
            "format" : "yyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
          },
          "dept1_id" : {
            "type" : "integer"
          },
          "dept1_name" : {
            "type" : "keyword",
            "index" : false
          },
          "dept2_id" : {
            "type" : "integer"
          },
          "dept_name" : {
            "type" : "text"
          },
          "doctor_id" : {
            "type" : "long"
          },
          "doctor_name" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword"
              }
            }
          },
          "head_image" : {
            "type" : "keyword"
          },
          "hospital_city_id" : {
            "type" : "integer"
          },
          "hospital_grade_id" : {
            "type" : "integer"
          },
          "hospital_id" : {
            "type" : "integer"
          },
          "hospital_name" : {
            "type" : "text",
            "analyzer" : "ik_smart"
          },
          "hospital_province_id" : {
            "type" : "integer"
          },
          "hospital_short_name" : {
            "type" : "text",
            "analyzer" : "ik_smart"
          },
          "medical_title" : {
            "type" : "keyword",
            "index" : false
          },
          "medical_title_id" : {
            "type" : "integer"
          },
          "service_amount" : {
            "type" : "float"
          },
          "source" : {
            "type" : "integer"
          },
          "uid" : {
            "type" : "integer"
          },
          "userstatus" : {
            "type" : "short"
          },
          "usertype" : {
            "type" : "short"
          }
        }
      }
    }
  }
}

1 打印mapping出来
2 建议需要term 精确匹配的,设置为keyword 类型
3 检索的时候,如果同时设置了text和keyword两种类型,建议关键词后加.keyword, 如:title.keyword

#pretty=true 格式化显示
   curl -H "Content-Type: application/json" -X  GET 'test.es.intra.youlai.cn:9200/xcx_doctor/doctor/_search?pretty=true'  -d '
{
  "query" : { "match" : { "doctor_name" : "刘薇" }},
  "size": 30
}'

elasticsearch 查询(match和term)
es中的查询请求有两种方式,一种是简易版的查询,另外一种是使用JSON完整的请求体,叫做结构化查询(DSL)。
由于DSL查询更为直观也更为简易,所以大都使用这种方式。
DSL查询是POST过去一个json,由于post的请求是json格式的,所以存在很多灵活性,也有很多形式。
这里有一个地方注意的是官方文档里面给的例子的json结构只是一部分,并不是可以直接黏贴复制进去使用的。一般要在外面加个query为key的机构。

match
最简单的一个match例子:

查询和"我的宝马多少马力"这个查询语句匹配的文档。

{
"query": {
"match": {
"content" : {
"query" : "我的宝马多少马力"
}
}
}
}
上面的查询匹配就会进行分词,比如"宝马多少马力"会被分词为"宝马 多少 马力", 所有有关"宝马 多少 马力", 那么所有包含这三个词中的一个或多个的文档就会被搜索出来。
并且根据lucene的评分机制(TF/IDF)来进行评分。

match_phrase
比如上面一个例子,一个文档"我的保时捷马力不错"也会被搜索出来,那么想要精确匹配所有同时包含"宝马 多少 马力"的文档怎么做?就要使用 match_phrase 了

{
"query": {
"match_phrase": {
"content" : {
"query" : "我的宝马多少马力"
}
}
}
}
完全匹配可能比较严,我们会希望有个可调节因子,少匹配一个也满足,那就需要使用到slop。

{
"query": {
"match_phrase": {
"content" : {
"query" : "我的宝马多少马力",
"slop" : 1
}
}
}
}
multi_match
如果我们希望两个字段进行匹配,其中一个字段有这个文档就满足的话,使用multi_match

{
"query": {
"multi_match": {
"query" : "我的宝马多少马力",
"fields" : ["title", "content"]
}
}
}
但是multi_match就涉及到匹配评分的问题了。

我们希望完全匹配的文档占的评分比较高,则需要使用best_fields
{
"query": {
"multi_match": {
"query": "我的宝马发动机多少",
"type": "best_fields",
"fields": [
"tag",
"content"
],
"tie_breaker": 0.3
}
}
}
意思就是完全匹配"宝马 发动机"的文档评分会比较靠前,如果只匹配宝马的文档评分乘以0.3的系数

我们希望越多字段匹配的文档评分越高,就要使用most_fields
{
"query": {
"multi_match": {
"query": "我的宝马发动机多少",
"type": "most_fields",
"fields": [
"tag",
"content"
]
}
}
}
我们会希望这个词条的分词词汇是分配到不同字段中的,那么就使用cross_fields
{
"query": {
"multi_match": {
"query": "我的宝马发动机多少",
"type": "cross_fields",
"fields": [
"tag",
"content"
]
}
}
}
term
term是代表完全匹配,即不进行分词器分析,文档中必须包含整个搜索的词汇

{
"query": {
"term": {
"content": "汽车保养"
}
}
}
查出的所有文档都包含"汽车保养"这个词组的词汇。

使用term要确定的是这个字段是否“被分析”(analyzed),默认的字符串是被分析的。

拿官网上的例子举例:

mapping是这样的:

PUT my_index
{
"mappings": {
"my_type": {
"properties": {
"full_text": {
"type": "string"
},
"exact_value": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}

PUT my_index/my_type/1
{
"full_text": "Quick Foxes!",
"exact_value": "Quick Foxes!"
}
其中的full_text是被分析过的,所以full_text的索引中存的就是[quick, foxes],而extra_value中存的是[Quick Foxes!]。

那下面的几个请求:

GET my_index/my_type/_search
{
"query": {
"term": {
"exact_value": "Quick Foxes!"
}
}
}
请求的出数据,因为完全匹配

GET my_index/my_type/_search
{
"query": {
"term": {
"full_text": "Quick Foxes!"
}
}
}
请求不出数据的,因为full_text分词后的结果中没有[Quick Foxes!]这个分词。

bool联合查询: must,should,must_not
如果我们想要请求"content中带宝马,但是tag中不带宝马"这样类似的需求,就需要用到bool联合查询。
联合查询就会使用到must,should,must_not三种关键词。

这三个可以这么理解

must: 文档必须完全匹配条件
should: should下面会带一个以上的条件,至少满足一个条件,这个文档就符合should
must_not: 文档必须不匹配条件
比如上面那个需求:

{
"query": {
"bool": {
"must": {
"term": {
"content": "宝马"
}
},
"must_not": {
"term": {
"tags": "宝马"
}
}
}
}
}

相关文章

  • es 调试 相关语句记录

    查看mapping结构 1 打印mapping出来2 建议需要term 精确匹配的,设置为keyword 类型3 ...

  • ES 相关语句

    ES: 查看索引 添加 创建索引 创建类型 查询

  • Console 对象常用方法

    记录一些常用的控制台调试语句:

  • elasticsearch的嵌套查询nested

    基于es7,kibana7 如果一条记录中有多个评论,记录视为对象,评论也视为对象,我们使用以下语句插入语句,默认...

  • ES Low Level REST Client With Au

    前言:公司ES集群由ES5.5升级到了ES7.4,而且加了用户认证,本文记录下涉及的相关的内容 目录一、相关代码—...

  • 4.3-搜索的相关性算分

    相关性和相关性算分 相关性 – Relevance搜索的相关性算分,描述了⼀个⽂档和查询语句匹配的程度。ES 会对...

  • vscode调试

    调试node服务 支持es6的mocha测试 sourceMaps开启,才能让调试位置不发生偏移 支持es6调试 ...

  • ES - 基本概念

    * ES集群会在生产环境被长期实践, 一些重要概念, 包括应用和优化调试方法值得记录分享 * 所以, 会有关于ES...

  • 【elasticsearch】14、搜索相关性算分

    相关性和相关性算分 相关性 -relevance搜索的相关性算分,描述了一个文档和查询语句匹配的程序。es会对每个...

  • Flink1.10.0与Hive3.1.2集成详细步骤

    背景 Flink Hive 集成介绍 Flink集群设置 本地开发调试本地开发调试提示 Git相关项目 本文记录了...

网友评论

      本文标题:es 调试 相关语句记录

      本文链接:https://www.haomeiwen.com/subject/iojuwhtx.html