• (13)ElasticSearch match查询


      match 查询知道分词器的存在,会对field进行分词操作,然后再查询。而term不会分词,会把field当成一个整体查询。

      1、数据准备

    PUT /lib
    {
        "settings":{
            "number_of_shards":3,
            "number_of_replicas":0
          },
            "mappings":{
                "user":{
                    "properties":{
                        "name":{"type":"text"},
                        "address":{"type":"text"},
                        "age":{"type":"integer"},
                        "interests":{"type":"text"},
                        "birthday":{"type":"date"}
                    }
                }
            }
    }
    put /lib/user/1
    {
        "name":"zhaoliu",
        "address":"hei long jiang sheng tie ling shi",
        "age":50,
        "birthday":"1970-12-12",
        "interests":"xi huang hejiu,duanlian,lvyou"
    }
    
    put /lib/user/2
    {
        "name":"zhaoming",
        "address":"bei jing hai dian qu qing he zhen",
        "age":20,
        "birthday":"1998-10-12",
        "interests":"xi huan hejiu,duanlian,changge"
    }
    
    put /lib/user/3
    {
        "name":"lisi",
        "address":"bei jing hai dian qu qing he zhen",
        "age":23,
        "birthday":"1998-10-12",
        "interests":"xi huan hejiu,duanlian,changge"
    }
    
    put /lib/user/4
    {
        "name":"wangwu",
        "address":"bei jing hai dian qu qing he zhen",
        "age":26,
        "birthday":"1998-10-12",
        "interests":"xi huan biancheng,tingyinyue,lvyou"
    }
    
    put /lib/user/5
    {
        "name":"zhangsan",
        "address":"bei jing chao yang qu",
        "age":29,
        "birthday":"1988-10-12",
        "interests":"xi huan tingyinyue,changge,tiaowu"
    }

      2、match基本操作 

      1)、用match查询name是zhaoliu或者zhaoming的,能查出两条记录,而用term查询不出来,因为倒排索引列表中没有 "zhaoliu zhaoming"。如下:

    GET /lib/user/_search
    {
      "query":{
        "match":{
          "name":"zhaoliu zhaoming"
        }
      }
    }

      结果如下:

    {
      "took": 2,
      "timed_out": false,
      "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
      },
      "hits": {
        "total": 2,
        "max_score": 0.9808292,
        "hits": [
          {
            "_index": "lib",
            "_type": "user",
            "_id": "2",
            "_score": 0.9808292,
            "_source": {
              "name": "zhaoming",
              "address": "bei jing hai dian qu qing he zhen",
              "age": 20,
              "birthday": "1998-10-12",
              "interests": "xi huan hejiu,duanlian,changge"
            }
          },
          {
            "_index": "lib",
            "_type": "user",
            "_id": "1",
            "_score": 0.6931472,
            "_source": {
              "name": "zhaoliu",
              "address": "hei long jiang sheng tie ling shi",
              "age": 50,
              "birthday": "1970-12-12",
              "interests": "xi huang hejiu,duanlian,lvyou"
            }
          }
        ]
      }
    }

      换成term

    GET /lib/user/_search
    {
      "query":{
        "term":{
          "name":"zhaoliu zhaoming"
        }
      }
    }

      结果如下:

    {
      "took": 2,
      "timed_out": false,
      "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
      },
      "hits": {
        "total": 0,
        "max_score": null,
        "hits": []
      }
    }

      2)、查询年龄是20的

    GET /lib/user/_search
    {
      "query":{
          "match":{"age":20
          }
        }
    }

      3、查询所有的match_all

    GET /lib/user/_search
    {
      "query":{
        "match_all":{}
      }
    }

      4、可以指定多个字段multi_match

      如下:interests或者name中含有lvyou的都会搜索出来。

    GET /lib/user/_search
    {
      "query":{
        "multi_match":{
          "query":"lvyou","fields":["interests","name"]
        }
      }
    }

      5、短语匹配 match_phrase

      ElasticSearch引擎首先分析(analyze)查询字符串,从分析后的文本中构建短语查询,这意味着必须匹配短语中的所有分词,并且保证各个分词的相对位置不变。(理解为完全匹配)。如下第一个不能查询到文档,第二个可以查询到文档。

    GET /lib/user/_search
    {
        "query":{
            "match_phrase":{
                "interests":"duanlian,shuoxiangsheng"
            }
        }
    }
    
    GET /lib/user/_search
    {
        "query":{
            "match_phrase":{
                "interests":"duanlian,changge"
            }
        }
    }

      6、指明需要哪些字段_source

      1)查询 interests 是 changge,结果中只显示 address、name字段:

    GET /lib/user/_search
    {
        "_source":["address","name"],
        "query":{
            "match":{
                "interests":"changge"
            }
        }
    }

      2)查询所有的,结果只显示name,address字段

    GET /lib/user/_search
    {
        "query":{
            "match_all":{}
        },
        "_source":{
            "includes":["name","address"]
        }
    }

      3)查询所有的,结果不包含age、birthday字段

    GET /lib/user/_search
    {
        "query":{
            "match_all":{}
        },
        "_source":{
            "excludes":["age","birthday"]
        }
    }

      4)通配符。查询所有的,字段名以addr开头,不包括name,不包括以bir开头的字段

    GET /lib3/user/_search
    {
        "_source":{
            "includes":"addr*",
            "excludes":["name","bir*"]
        },
        "query":{
            "match_all":{}
        }
    }

      7、排序使用sort实现排序:desc降序,asc升序

    GET /lib/user/_search
    {
        "query":{
            "match_all":{}
            },
            "sort":{
                "age":{"order":"desc"}
            }
    }

      8、前缀匹配查询match_phrase_prefix

      查询出name以zhao开头的文档

    GET /lib/user/_search
    {
        "query":{
            "match_phrase_prefix":{
                "name":{
                    "query":"zhao"
                }
            }
        }
    }

      9、范围查询range

      参数:from,to,include_lower,include_upper,boost。

      include_lower:是否包含范围的左边界,默认是true,

      include_upper:是否包含范围的右边界,默认是true

    GET /lib/user/_search
    {
        "query":{
            "range":{
                "birthday":{
                    "from":"1990-10-10",
                    "to":"2018-05-01"
                }
            }
        }
    }
    GET /lib/user/_search
    {
        "query":{
            "range":{
                "age":{
                    "from":20,
                    "to":25,
                    "include_lower":true,
                    "include_upper":false
                }
            }
        }
    }

      9、wildcard查询

      允许使用通配符*和?来进行查询

      *代表0个或者多个字符
      ?代表任意一个字符

    GET /lib/user/_search
    {
        "query":{
            "wildcard":{
                "name":"li?i"
            }
        }
    }

      10、fuzzy 实现模糊查询

      value:查询的关键字
      boost:查询的权值,默认值是1.0
      min_similarity:设置匹配的最小相似度,默认值为 0.5,对于字符串,取值
             为0-1(包括0和1);对于数值 ,取值可能大于1;对于日期型取值为1d,1m等 ,1d就代表1天
      prefix_length:指明区分词项的共同前缀长度,默认是0
      max_expansions:查询中的词项可以扩展的数目,默认可以无限大

      如下,name的值是zholiu可以查询出name是zhaoliu的文档,interests的值是chagge可以查询出interests的值是chagge的文档

    GET /lib/user/_search
    {
        "query":{
            "fuzzy":{
                "name":"zholiu"
            }
        }
    }
    GET /lib/user/_search
    {
        "query":{
            "fuzzy":{
                "interests":{
                    "value":"chagge"
                }
            }
        }
    }

      11、高亮查询highlight

      搜索出来的文档,在搜索关键字的地方加了标签,如<em>changge</em>,下面是个示例。

    GET /lib/user/_search
    {
        "query":{
            "match":{
                "interests":"changge"
            }
        },
        "highlight":{
            "fields":{
                "interests":{}
            }
        }
    }

      结果如下:

    {
      "took": 83,
      "timed_out": false,
      "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
      },
      "hits": {
        "total": 3,
        "max_score": 0.6931472,
        "hits": [
          {
            "_index": "lib",
            "_type": "user",
            "_id": "3",
            "_score": 0.6931472,
            "_source": {
              "name": "lisi",
              "address": "bei jing hai dian qu qing he zhen",
              "age": 23,
              "birthday": "1998-10-12",
              "interests": "xi huan hejiu,duanlian,changge"
            },
            "highlight": {
              "interests": [
                "xi huan hejiu,duanlian,<em>changge</em>"
              ]
            }
          },
          {
            "_index": "lib",
            "_type": "user",
            "_id": "2",
            "_score": 0.47000363,
            "_source": {
              "name": "zhaoming",
              "address": "bei jing hai dian qu qing he zhen",
              "age": 20,
              "birthday": "1998-10-12",
              "interests": "xi huan hejiu,duanlian,changge"
            },
            "highlight": {
              "interests": [
                "xi huan hejiu,duanlian,<em>changge</em>"
              ]
            }
          },
          {
            "_index": "lib",
            "_type": "user",
            "_id": "5",
            "_score": 0.47000363,
            "_source": {
              "name": "zhangsan",
              "address": "bei jing chao yang qu",
              "age": 29,
              "birthday": "1988-10-12",
              "interests": "xi huan tingyinyue,changge,tiaowu"
            },
            "highlight": {
              "interests": [
                "xi huan tingyinyue,<em>changge</em>,tiaowu"
              ]
            }
          }
        ]
      }
    }
  • 相关阅读:
    使用SHA256WithRSA来签名和验签(.NET/C#)
    对2个hex(16进制)字符串进行异或操作
    Java DESede 加解密("DESede/ECB/PKCS5Padding")
    获取公钥证书的DN(Distinguished Name)
    Java DES 加解密("DES/EBC/NoPadding")
    Porting .Net RSA xml keys to Java
    Linux使用Shell脚本实现ftp的自动上传下载
    Lombok 安装、入门
    一段对16进制字符串进行异或的代码
    一个封装的使用Apache HttpClient进行Http请求(GET、POST、PUT等)的类。
  • 原文地址:https://www.cnblogs.com/javasl/p/11405391.html
Copyright © 2020-2023  润新知