【ES】Elasticsearch的基本操作及相关概念-02

前言

所有操作基于kibana的 devs tools,相关的安装可以看:

【ES学习】ElasticSearch MacOS版 安装与使用(图文教程)

【ES学习】ElasticSearch在Kibana的使用 Kibana安装(MacOS版)

本文承接【ES】Elasticsearch的基本操作及相关概念-01

先再加一个文档

PUT /megacorp/employee/4
{
    "first_name" :  "Gang",
    "last_name" :   "Xiao",
    "age" :         23,
    "about" :       "I love everything about music",
    "interests":  [ "forestry", "music"]
}

query查询: interval - match

按照搜索词的特定排列,搜索对应的文档。并且可以规定搜索词之间的最大单词间隔。

我们输入:

============ 表达式 =============
GET /megacorp/employee/_search
{
  "query": {
    "intervals": {
      "目标字段": {
        "match": {
          "query": "搜索单词(串)",
          "max_gaps": 最大间隔数值,
          "ordered": 是否要有序(true/false)
        }
      }
    }
  }
}
================================
GET /megacorp/employee/_search
{
  "query": {
    "intervals": {
      "about": {
        "match": {
          "query": "I rock",
          "max_gaps": 4,
          "ordered": true
        }
      }
    }
  }
}

我们可以得到两个结果:

........
    "hits" : [
      {
        "_index" : "megacorp",
        "_type" : "employee",
        "_id" : "1",
        "_score" : 0.19999999,
        "_source" : {
          "first_name" : "John",
          "last_name" : "Smith",
          "age" : 25,
          "about" : "I love to go rock climbing",
          "interests" : [
            "sports",
            "music"
          ]
        }
      },
      {
        "_index" : "megacorp",
        "_type" : "employee",
        "_id" : "2",
        "_score" : 0.19999999,
        "_source" : {
          "first_name" : "Jane",
          "last_name" : "Smith",
          "age" : 32,
          "about" : "I like to collect rock albums",
          "interests" : [
            "music"
          ]
        }
      }
      ......

如果我们把参数"max_gaps"改为2,就什么都搜不到了。因为I单词和rock单词间隔小于等于2的,在所有文档的about字段都不存在。

query查询: term

term查询时判断某个document是否包含某个具体的值。term不会对查询语句进行分词处理,直接拿查询输入的文本去检索:

我们输入:

============ 表达式 ============
GET /megacorp/employee/_search
{
  "query" : {
    "term": {
      "目标字段": {
        "value": "单词(串)"
      }
    }
  }
}

================================
GET /megacorp/employee/_search
{
  "query" : {
    "term": {
      "about": {
        "value": "love"
      }
    }
  }
}

可以很顺利得到所有在about字段中包含love单词的结果:

    "hits" : [
      {
        "_index" : "megacorp",
        "_type" : "employee",
        "_id" : "4",
        "_score" : 0.55584013,
        "_source" : {
          "first_name" : "Gang",
          "last_name" : "Xiao",
          "age" : 23,
          "about" : "I love everything about music",
          "interests" : [
            "forestry",
            "music"
          ]
        }
      },
      {
        "_index" : "megacorp",
        "_type" : "employee",
        "_id" : "1",
        "_score" : 0.51556194,
        "_source" : {
          "first_name" : "John",
          "last_name" : "Smith",
          "age" : 25,
          "about" : "I love to go rock climbing",
          "interests" : [
            "sports",
            "music"
          ]
        }
      }

但term查询不能分词处理,如果我们输入:

GET /megacorp/employee/_search
{
  "query" : {
    "term": {
      "about": {
        "value": "love to"
      }
    }
  }
}

结果什么都查不到。

query查询: terms

terms和term的查询语法基本类似,但terms支持多个文本查询:

GET /megacorp/employee/_search
{
  "query" : {
    "terms": {
      "about": ["build", "climbing"]
    }
  }
}

最终就会很干净的搜出两条文档,他们分别拥有build单词和climbing单词。

GET /_search
{
  "query" : {
    "terms": {
      "about": ["build", "climbing"]
    }
  }
}

query查询: range

query - range表达式,可以对搜索字段的数值进行约束。

其中gte表示大于等于,lte表示小于等于,gt表示大于,lt表示小于。boost表示对此次query进行相关性算分权重,默认是1.0

========== 表达式 =========
GET /megacorp/employee/_search
{
  "query" : {
    "range" : {
      "目标字段": {
        "gte": 数值,
        "lte": 数值,
        "boost": 数值
      }
    }
  }
}
=========================
GET /megacorp/employee/_search
{
  "query" : {
    "range" : {
      "age": {
        "gte": 20,
        "lte": 30,
        "boost": 1
      }
    }
  }
}

query查询: exist

返回指定字段不为空的所有文档。包括字段对应的值是null,或者[],或者没有为这个字段建立索引。

============ 表达式 =============
GET /megacorp/employee/_search
{
  "query" : {
    "exists": {
      "field" : "目标字段"
    }
  }
}
=============================
GET /megacorp/employee/_search
{
  "query" : {
    "exists": {
      "field" : "about"
    }
  }
}

query查询: ids

根据文档的_id搜索对应的文档:

我们输入:

=========== 表达式 ============
GET /megacorp/employee/_search
{
  "query": {
    "ids": {
      "values": ["索引id","索引id","索引id"]
    }
  }
}
============================
GET /megacorp/employee/_search
{
  "query": {
    "ids": {
      "values": ["1","4","999"]
    }
  }
}

得到:

....
    "hits" : [
      {
        "_index" : "megacorp",
        "_type" : "employee",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "first_name" : "John",
          "last_name" : "Smith",
          "age" : 25,
          "about" : "I love to go rock climbing",
          "interests" : [
            "sports",
            "music"
          ]
        }
      },
      {
        "_index" : "megacorp",
        "_type" : "employee",
        "_id" : "4",
        "_score" : 1.0,
        "_source" : {
          "first_name" : "Gang",
          "last_name" : "Xiao",
          "age" : 23,
          "about" : "I love everything about music",
          "interests" : [
            "forestry",
            "music"
          ]
        }
      }
      ......

query查询: fuzzy

一个非常好玩的api。

fuzzy查询是一种模糊查询,会根据搜索单词的编辑距离(Levenshtein Distance)来判断是否匹配。一个编辑距离就是对单词进行一个字符的修改,这种修改可能是

  • 修改一个字符,比如box到fox
  • 删除一个字符,比如black到lack
  • 插入一个字符,比如sic到sick
  • 交换两个相邻的字符的位置,比如act到cat

我们输入:

============ 表达式 =============
GET /megacorp/employee/_search
{
  "query": {
    "fuzzy": {
      "目标字段": "搜索词"
    }
  }
}
==============================
GET /megacorp/employee/_search
{
  "query": {
    "fuzzy": {
      "about": "buil"
    }
  }
}

得到:

    "hits" : [
      {
        "_index" : "megacorp",
        "_type" : "employee",
        "_id" : "3",
        "_score" : 0.9378587,
        "_source" : {
          "first_name" : "Douglas",
          "last_name" : "Fir",
          "age" : 35,
          "about" : "I like to build cabinets",
          "interests" : [
            "forestry"
          ]
        }
      }
    ]

所以我们模糊搜索搜到的词是build。

query查询: prefix

搜索出所有,包含以搜索词为前缀的单词的字段,的文档。

我们输入:

========= 表达式 =========
GET /megacorp/employee/_search
{
  "query": {
    "prefix": {
      "目标字段": "搜索词"
    }
  }
}
========================
GET /megacorp/employee/_search
{
  "query": {
    "prefix": {
      "about": "ro"
    }
  }
}

可以得到:

.....
      {
        "_index" : "megacorp",
        "_type" : "employee",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "first_name" : "John",
          "last_name" : "Smith",
          "age" : 25,
          "about" : "I love to go rock climbing",
          "interests" : [
            "sports",
            "music"
          ]
        }
      },
      {
        "_index" : "megacorp",
        "_type" : "employee",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "first_name" : "Jane",
          "last_name" : "Smith",
          "age" : 32,
          "about" : "I like to collect rock albums",
          "interests" : [
            "music"
          ]
        }
      }

.....

query查询: wildcard

搜索包含通配符的搜索词的文档。

支持两种通配符:

  • ?:匹配任何单一的字符
  • *:匹配0个或者多个字符

我们输入:

=========== 表达式 ============
GET /megacorp/employee/_search
{
  "query": {
    "wildcard": {
      "目标字段": {
        "value": "搜索词和通配符"
      }
    }
  }
}
================================
GET /megacorp/employee/_search
{
  "query": {
    "wildcard": {
      "about": {
        "value": "ro*"
      }
    }
  }
}

可以搜索到所有about字段包含rock单词的文档。

query复合查询: bool

复合查询可以对多个字段过滤筛选,类比mysql的where多条件查询。

  • must:根据must中的条件过滤文档,返回的结果文档必须严格匹配条件,会影响相关性算分

  • filter:根据must中的条件过滤文档,返回的结果文档必须严格匹配条件,和must不同的是,filter不会影响相关性算分

  • should:根据should中的条件进行筛选,返回的结果文档应该包含should的条件,影响相关性 算分

  • must_not:根据must_not中的条件过滤文档,返回的结果文档必须不包含must_not条件,会影响相关性算分t

我们可以输入:

============ 表达式 ===========
GET /megacorp/employee/_search
{
  "query" : {
    "bool" : {
      "must": [
        {
          "查询子句": {
            ......
          }
        }
      ],
      "filter": [
        {
          "查询子句": {
            ......
          }
        }
      ],
      "should": [
        {
          "查询子句": {
            ......
          }
        }
      ],
      "must_not": [
        {
          "查询子句": {
            ......
          }
        }
      ]
    }
  }
}
==============================
GET /megacorp/employee/_search
{
  "query" : {
    "bool" : {
      "must": [
        {
          "match" : {
            "about" : "like"
          }
        }
      ],
      "filter": [
        {
          "range" : {
            "age": {
              "gte": 25,
              "lte": 40
            }
          }
        }
      ],
      "should": [
        {
          "term": {
            "about": {
              "value": "to"
            }
          },
        }
      ]
    }
  }
}

可以得到结果:

	.....
    "hits" : [
      {
        "_index" : "megacorp",
        "_type" : "employee",
        "_id" : "3",
        "_score" : 1.4586673,
        "_source" : {
          "first_name" : "Douglas",
          "last_name" : "Fir",
          "age" : 35,
          "about" : "I like to build cabinets",
          "interests" : [
            "forestry"
          ]
        }
      },
      {
        "_index" : "megacorp",
        "_type" : "employee",
        "_id" : "2",
        "_score" : 1.3529669,
        "_source" : {
          "first_name" : "Jane",
          "last_name" : "Smith",
          "age" : 32,
          "about" : "I like to collect rock albums",
          "interests" : [
            "music"
          ]
        }
      }
      ......

参考

官方中文文档
https://www.elastic.co/guide/cn/elasticsearch/guide/current/_document_oriented.html

https://blog.csdn.net/fanrenxiang/article/details/86477019

https://www.cnblogs.com/sunshuyi/p/12716828.html

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
在JavaScript中,可以使用流畅的DSL(Domain Specific Language)方法来比较数字、字符串和布尔值的值。其中包括eq(等于)、neq(不等于)、gt(大于)、lt(小于)、gte(大于等于)和lte(小于等于)等比较器方法。例如,如果我们有一个路径path.to.value,其值为1,那么使用eq方法可以将其与另一个值进行比较,如果相等则返回true。 另外,通过使用字符的charCodeAt()方法,可以获取字符的Unicode编码值。例如,对于字符'a'和'b','a'的Unicode编码值为97,'b'的Unicode编码值为98。因此,可以使用比较操作符'<'来比较这两个字符的大小。同样,对于字符串'aa'和'ab',按照字典序进行比较,第一个字符相等,比较第二个字符。如果某个字符缺失,则缺失的字符永远小于存在的字符。 在Lodash库中,还提供了常用的比较函数。例如,eq函数用于判断两个值是否相等。如果是对象,只有引用相同才会返回true;如果是字符串,将根据字符的比较规则来判断是否相等;如果是NaN,会返回true。 至于在MongoDB的查询中,可以使用字符串或数组的长度来进行数据查询。例如,可以使用gte(大于等于)运算符来查询字符串长度大于等于某个值的数据。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [fluent-comparators:用于数字、字符串和布尔值的流畅 javascript dsl 比较器(eq、neq、gtltgte、lte)](https://download.csdn.net/download/weixin_42121754/19212190)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [js【详解】比较(数字与数字比较、数字与字符串比较、字符串与字符串比较、字符串与非数字比较……)](https://blog.csdn.net/weixin_41192489/article/details/116736255)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [mongo根据字符串/数组长度查询](https://blog.csdn.net/Long861774/article/details/124835111)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

锥栗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值