ElasticSearch 5.5.0学习笔记(二)

ElasticSearch 5.5.0学习笔记(二)

1. QueryDSL检索

1.1 范围查询

  • GET user/customer/_search
    {
      "query": {
        "range": {
          "age": {
            "gte": 10,
            "lte": 20
          }
        }
      }
    }
    
    返回结果:
    {
      "took": 0,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
      },
      "hits": {
        "total": 1,
        "max_score": 1,
        "hits": [
          {
            "_index": "user",
            "_type": "customer",
            "_id": "AXveKbYSN3SMJmlGOrdz",
            "_score": 1,
            "_source": {
              "name": "zs",
              "age": 20,
              "bir": "2021-08-10"
            }
          }
        ]
      }
    }
    
  • gte:大于等于

  • lte:小于等于

  • gt:大于

  • lt:小于

1.2 前缀查询

  • GET user/customer/_search
    {
      "query": {
        "prefix": {
          "name": {
            "value": "h"
          }
        }
      }
    }
    
    返回结果:
    {
      "took": 6,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
      },
      "hits": {
        "total": 1,
        "max_score": 1,
        "hits": [
          {
            "_index": "user",
            "_type": "customer",
            "_id": "1",
            "_score": 1,
            "_source": {
              "id": 1,
              "name": "hjx",
              "age": 24,
              "bir": "2021-09-10"
            }
          }
        ]
      }
    }
    
  • 查询name以h开头的数据

1.3 通配符查询(wildcard)

  • GET /user/customer/_search
    {
      "query": {
        "wildcard": {
          "name": {
            "value": "h*"
          }
        }
      }
    }
    
    GET /user/customer/_search
    {
      "query": {
        "wildcard": {
          "name": {
            "value": "?jx"
          }
        }
      }
    }
    
    返回结果:
    {
      "took": 3,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
      },
      "hits": {
        "total": 1,
        "max_score": 1,
        "hits": [
          {
            "_index": "user",
            "_type": "customer",
            "_id": "1",
            "_score": 1,
            "_source": {
              "id": 1,
              "name": "hjx",
              "age": 24,
              "bir": "2021-09-10"
            }
          }
        ]
      }
    }
    
  • ?表示匹配一个字符,* 表示匹配0到多个字符

1.4 多id查询

  • GET /user/customer/_search
    {
      "query": {
        "ids": {
          "values": ["AXveKbYSN3SMJmlGOrdz","1","5"]
        }
      }
    }
    
    返回结果:
    {
      "took": 2,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
      },
      "hits": {
        "total": 3,
        "max_score": 1,
        "hits": [
          {
            "_index": "user",
            "_type": "customer",
            "_id": "5",
            "_score": 1,
            "_source": {
              "name": "小明",
              "age": 23,
              "bir": "2011-11-11"
            }
          },
          {
            "_index": "user",
            "_type": "customer",
            "_id": "AXveKbYSN3SMJmlGOrdz",
            "_score": 1,
            "_source": {
              "name": "zs",
              "age": 20,
              "bir": "2021-08-10"
            }
          },
          {
            "_index": "user",
            "_type": "customer",
            "_id": "1",
            "_score": 1,
            "_source": {
              "id": 1,
              "name": "hjx",
              "age": 24,
              "bir": "2021-09-10"
            }
          }
        ]
      }
    }
    

1.5 模糊查询

  • GET /user/customer/_search
    {
      "query": {
        "fuzzy": {
          "name": "hjc"
        }
      }
    }
    
    返回结果:
    {
      "took": 8,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
      },
      "hits": {
        "total": 1,
        "max_score": 0.19178805,
        "hits": [
          {
            "_index": "user",
            "_type": "customer",
            "_id": "1",
            "_score": 0.19178805,
            "_source": {
              "id": 1,
              "name": "hjx",
              "age": 24,
              "bir": "2021-09-10"
            }
          }
        ]
      }
    }
    
  • 模糊查询 最大错误在0-2之间,

  • 当关键词在0-2个单词之间,必须完全匹配

  • 当关键词在3-5个单词之间,允许有一个单词错误

  • 当关键词大于5个单词,最大允许两个单词错误

1.6 布尔查询

  • bool关键字:组合多个条件实现复杂查询

  • must:多个条件同时成立才满足

  • should:其中一个条件成立就满足

  • must_not:一个也不能匹配才算满足

  • GET /user/customer/_search
    {
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "age": {
                  "value": "24"
                }
              }
            },
            {
              "term": {
                "name": {
                  "value": "hjx"
                }
              }
            }
          ]
        }
      }
    }
    查询年龄为24,名字为hjx的数据,同时满足
    返回结果:
    {
      "took": 0,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
      },
      "hits": {
        "total": 1,
        "max_score": 1.287682,
        "hits": [
          {
            "_index": "user",
            "_type": "customer",
            "_id": "1",
            "_score": 1.287682,
            "_source": {
              "id": 1,
              "name": "hjx",
              "age": 24,
              "bir": "2021-09-10"
            }
          }
        ]
      }
    }
        
    GET /user/customer/_search
    {
      "query": {
        "bool": {
          "should": [
            {
              "term": {
                "age": {
                  "value": "20"
                }
              }
            },
            {
              "term": {
                "name": {
                  "value": "hjx"
                }
              }
            }
          ]
        }
      }
    }
    查询年龄为20 或者 名称为 hjx 的数据,满足其中一个就可以
    返回结果:
    {
      "took": 1,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
      },
      "hits": {
        "total": 2,
        "max_score": 1,
        "hits": [
          {
            "_index": "user",
            "_type": "customer",
            "_id": "AXveKbYSN3SMJmlGOrdz",
            "_score": 1,
            "_source": {
              "name": "zs",
              "age": 20,
              "bir": "2021-08-10"
            }
          },
          {
            "_index": "user",
            "_type": "customer",
            "_id": "1",
            "_score": 0.2876821,
            "_source": {
              "id": 1,
              "name": "hjx",
              "age": 24,
              "bir": "2021-09-10"
            }
          }
        ]
      }
    }
    
    GET /user/customer/_search
    {
      "query": {
        "bool": {
          "must_not": [
            {
              "term": {
                "age": {
                  "value": "20"
                }
              }
            },
            {
              "term": {
                "name": {
                  "value": "hjx"
                }
              }
            }
          ]
        }
      }
    }
    查询年龄不是20 名称不是 hjx 的数据
    返回结果:
    {
      "took": 2,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
      },
      "hits": {
        "total": 1,
        "max_score": 1,
        "hits": [
          {
            "_index": "user",
            "_type": "customer",
            "_id": "5",
            "_score": 1,
            "_source": {
              "name": "小明",
              "age": 23,
              "bir": "2011-11-11"
            }
          }
        ]
      }
    }
    

1.7 多字段查询(multi_match)

  • GET /user/customer/_search
    {
      "query": {
        "multi_match": {
          "query": "20",
          "fields": ["age","name","bir"]
        }
      }
    }
    
    在age、name、bir三个字段中搜索20关键词
    如果搜索的字段可以分词,会对query中的关键词先分词再搜索
    如果搜索的字段不可以分词,则进行全匹配搜索
        
    返回结果:
    {
      "took": 5,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
      },
      "hits": {
        "total": 1,
        "max_score": 1,
        "hits": [
          {
            "_index": "user",
            "_type": "customer",
            "_id": "AXveKbYSN3SMJmlGOrdz",
            "_score": 1,
            "_source": {
              "name": "zs",
              "age": 20,
              "bir": "2021-08-10"
            }
          }
        ]
      }
    }
    

2. 过滤查询(filter query)

  • ES中的查询实际上可以分为两种:查询(query)和过滤(filter)

    • 查询(query):对对应的索引进行查询,计算每个的得分,并按照得分排序,将结果返回给用户

    • 过滤(filter):只筛选符合条件的文档,并不计算得分,且可以缓存文档,后续查询可以在其基础上进行操作

    • 查询适合精确匹配数据,过滤适合大范围筛选数据,

    • GET /user/customer/_search
      {
        "query": {
          "bool": {
            "must": [
              {
                "term": {
                  "name": {
                    "value": "hjx"
                  }
                }
              }
            ],
            "filter": {
              "range": {
                "age": {
                  "gt": 20,
                  "lte": 30
                }
              }
            }
          }
        }
      }
      
      查询年龄小于等于30 大于20 名字为hjx 的数据
      
      ES在执行查询时,会先执行filter里的内容,筛选出年龄小于等于30 大于 20 的数据
      在这个基础上查询名字为 hjx 的数据
          
      另外,ES会缓存常用的filter过滤器,以加快性能
          
      返回结果:
      {
        "took": 9,
        "timed_out": false,
        "_shards": {
          "total": 5,
          "successful": 5,
          "failed": 0
        },
        "hits": {
          "total": 1,
          "max_score": 0.2876821,
          "hits": [
            {
              "_index": "user",
              "_type": "customer",
              "_id": "1",
              "_score": 0.2876821,
              "_source": {
                "id": 1,
                "name": "hjx",
                "age": 24,
                "bir": "2021-09-10"
              }
            }
          ]
        }
      }
      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值