zoukankan      html  css  js  c++  java
  • Elasticsearch 数据查询

    数据准备:

    PUT /shop
    {
      "settings": {
        "number_of_shards": 3,
        "number_of_replicas": 2
      }
    }
    
    PUT /shop/_mapping/goods
    {
      "properties": {
        "title": {
          "type": "text",
          "analyzer": "ik_max_word"
        },
        "price": {
          "type": "float"
        },
        "stock": {
          "type": "integer"
        }
      }
    }
    
    POST /shop/goods/_bulk
    {"index":{}}
    {"title": "小米8 全面屏游戏智能手机 6GB+64GB 黑色 全网通4G 双卡双待","price": 2299.00,"stock": 8800}
    {"index":{}}
    {"title": "OPPO Find X曲面全景屏 波尔多红 8GB+128GB 全网通 移动联通电信全网通4G 双卡双待手机","price": 4999.00,"stock": 5600}
    {"index":{}}
    {"title": "联想(Lenovo)拯救者Y7000P英特尔酷睿 i7 15.6英寸游戏笔记本电脑(i7-8750H 8G 512G SSD GTX1060 144Hz黑)","price": 8599.00,"stock": 1900}
    {"index":{}}
    {"title": "TP-LINK TL-WDR5620 1200M 5G双频智能无线路由器 四天线智能wifi 稳定穿墙高速家用路由器","price": 109.00,"stock": 9970}

    一、基本查询

    语法:

    GET /索引库名/_search
    {
      "query": {
        "查询类型": {
          "查询条件": "查询条件值"
        }
      }
    }

    查询类型:match_all,match,term,range,fuzzy,bool 等等

    查询条件:查询条件会根据类型的不同,写法也有差异

    1.1 查询所有(match_all)

    查询指令:

    GET /shop/_search
    {
      "query": {
        "match_all": {}
      }
    }

    查询结果:

    {
      "took": 13,
      "timed_out": false,
      "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
      },
      "hits": {
        "total": 4,
        "max_score": 1,
        "hits": [
          {
            "_index": "shop",
            "_type": "goods",
            "_id": "P4EGtmgBBdkQnU_d8b7I",
            "_score": 1,
            "_source": {
              "title": "联想(Lenovo)拯救者Y7000P英特尔酷睿 i7 15.6英寸游戏笔记本电脑(i7-8750H 8G 512G SSD GTX1060 144Hz黑)",
              "price": 8599,
              "stock": 1900
            }
          },
          {
            "_index": "shop",
            "_type": "goods",
            "_id": "QIEGtmgBBdkQnU_d-r6T",
            "_score": 1,
            "_source": {
              "title": "TP-LINK TL-WDR5620 1200M 5G双频智能无线路由器 四天线智能wifi 稳定穿墙高速家用路由器",
              "price": 109,
              "stock": 9970
            }
          },
          {
            "_index": "shop",
            "_type": "goods",
            "_id": "PYEGtmgBBdkQnU_d4b4m",
            "_score": 1,
            "_source": {
              "title": "小米8 全面屏游戏智能手机 6GB+64GB 黑色 全网通4G 双卡双待",
              "price": 2299,
              "stock": 8800
            }
          },
          {
            "_index": "shop",
            "_type": "goods",
            "_id": "PoEGtmgBBdkQnU_d6b4Q",
            "_score": 1,
            "_source": {
              "title": "OPPO Find X曲面全景屏 波尔多红 8GB+128GB 全网通 移动联通电信全网通4G 双卡双待手机",
              "price": 4999,
              "stock": 5600
            }
          }
        ]
      }
    }

    对查询结果进行分页

    GET /shop/_search
    {
      "query": {
        "match_all": {}
      },
      "from": 1,
      "size": 2
    }

    1.2 匹配查询(match)

    or 关系:会把查询条件进行分词,然后进行查询,多个词条之间是or的关系

    查询指令:

    GET /shop/_search
    {
      "query": {
        "match": {
          "title": {
            "query": "小米手机",
            "operator": "or"
          }
        }
      }
    }

    查询结果:

    {
      "took": 3,
      "timed_out": false,
      "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
      },
      "hits": {
        "total": 2,
        "max_score": 1.6051829,
        "hits": [
          {
            "_index": "demo",
            "_type": "goods",
            "_id": "MIHNs2gBBdkQnU_d_r6o",
            "_score": 1.6051829,
            "_source": {
              "title": "华为手机",
              "price": "2199.00"
            }
          },
          {
            "_index": "demo",
            "_type": "goods",
            "_id": "LYHJs2gBBdkQnU_dt75n",
            "_score": 0.2876821,
            "_source": {
              "title": "小米手机",
              "price": "1699.00"
            }
          }
        ]
      }
    }

    and关系:会把查询条件进行分词,然后进行查询,多个词条之间是and的关系

    查询指令:

    GET /shop/_search
    {
      "query": {
        "match": {
          "title": {
            "query": "小米手机",
            "operator": "and"
          }
        }
      }
    }

    查询结果:

    {
      "took": 10,
      "timed_out": false,
      "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
      },
      "hits": {
        "total": 1,
        "max_score": 0.92355466,
        "hits": [
          {
            "_index": "shop",
            "_type": "goods",
            "_id": "PYEGtmgBBdkQnU_d4b4m",
            "_score": 0.92355466,
            "_source": {
              "title": "小米8 全面屏游戏智能手机 6GB+64GB 黑色 全网通4G 双卡双待",
              "price": 2299,
              "stock": 8800
            }
          }
        ]
      }
    }

    1.3 词条查询

    单值查询(term)

    查询指令:

    GET /shop/_search
    {
      "query": {
        "term": {
          "price": 109.00
        }
      }
    }

    查询结果:

    {
      "took": 3,
      "timed_out": false,
      "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
      },
      "hits": {
        "total": 1,
        "max_score": 1,
        "hits": [
          {
            "_index": "shop",
            "_type": "goods",
            "_id": "QIEGtmgBBdkQnU_d-r6T",
            "_score": 1,
            "_source": {
              "title": "TP-LINK TL-WDR5620 1200M 5G双频智能无线路由器 四天线智能wifi 稳定穿墙高速家用路由器",
              "price": 109,
              "stock": 9970
            }
          }
        ]
      }
    }

    多值查询(terms)

    查询指令:

    GET /shop/_search
    {
      "query": {
        "terms": {
          "price": [8599.00,109.00]
        }
      }
    }

    查询结果:

    {
      "took": 15,
      "timed_out": false,
      "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
      },
      "hits": {
        "total": 2,
        "max_score": 1,
        "hits": [
          {
            "_index": "shop",
            "_type": "goods",
            "_id": "P4EGtmgBBdkQnU_d8b7I",
            "_score": 1,
            "_source": {
              "title": "联想(Lenovo)拯救者Y7000P英特尔酷睿 i7 15.6英寸游戏笔记本电脑(i7-8750H 8G 512G SSD GTX1060 144Hz黑)",
              "price": 8599,
              "stock": 1900
            }
          },
          {
            "_index": "shop",
            "_type": "goods",
            "_id": "QIEGtmgBBdkQnU_d-r6T",
            "_score": 1,
            "_source": {
              "title": "TP-LINK TL-WDR5620 1200M 5G双频智能无线路由器 四天线智能wifi 稳定穿墙高速家用路由器",
              "price": 109,
              "stock": 9970
            }
          }
        ]
      }
    }

    二、结果过滤

     默认情况下,elasticsearch在搜索的结果中,会把文档中保存在 _source 的所有字段都返回。

    如果我们只想获取其中的部分字段,我们可以添加 _source 字段进行过滤

    2.1 包含字段查询(includes)

    查询指令:

    GET /shop/_search
    {
      "query": {
        "match_all": {}
      },
      "_source": {
        "includes": ["title","price"]
      }
    }

    查询结果:

    {
      "took": 4,
      "timed_out": false,
      "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
      },
      "hits": {
        "total": 4,
        "max_score": 1,
        "hits": [
          {
            "_index": "shop",
            "_type": "goods",
            "_id": "P4EGtmgBBdkQnU_d8b7I",
            "_score": 1,
            "_source": {
              "price": 8599,
              "title": "联想(Lenovo)拯救者Y7000P英特尔酷睿 i7 15.6英寸游戏笔记本电脑(i7-8750H 8G 512G SSD GTX1060 144Hz黑)"
            }
          },
          {
            "_index": "shop",
            "_type": "goods",
            "_id": "QIEGtmgBBdkQnU_d-r6T",
            "_score": 1,
            "_source": {
              "price": 109,
              "title": "TP-LINK TL-WDR5620 1200M 5G双频智能无线路由器 四天线智能wifi 稳定穿墙高速家用路由器"
            }
          },
          {
            "_index": "shop",
            "_type": "goods",
            "_id": "PYEGtmgBBdkQnU_d4b4m",
            "_score": 1,
            "_source": {
              "price": 2299,
              "title": "小米8 全面屏游戏智能手机 6GB+64GB 黑色 全网通4G 双卡双待"
            }
          },
          {
            "_index": "shop",
            "_type": "goods",
            "_id": "PoEGtmgBBdkQnU_d6b4Q",
            "_score": 1,
            "_source": {
              "price": 4999,
              "title": "OPPO Find X曲面全景屏 波尔多红 8GB+128GB 全网通 移动联通电信全网通4G 双卡双待手机"
            }
          }
        ]
      }
    }

     2.2 排除字段查询(excludes)

    查询指令

    GET /shop/_search
    {
      "query": {
        "match_all": {}
      },
      "_source": {
        "excludes": "price"
      }
    }

    查询结果:

    {
      "took": 3,
      "timed_out": false,
      "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
      },
      "hits": {
        "total": 4,
        "max_score": 1,
        "hits": [
          {
            "_index": "shop",
            "_type": "goods",
            "_id": "P4EGtmgBBdkQnU_d8b7I",
            "_score": 1,
            "_source": {
              "title": "联想(Lenovo)拯救者Y7000P英特尔酷睿 i7 15.6英寸游戏笔记本电脑(i7-8750H 8G 512G SSD GTX1060 144Hz黑)",
              "stock": 1900
            }
          },
          {
            "_index": "shop",
            "_type": "goods",
            "_id": "QIEGtmgBBdkQnU_d-r6T",
            "_score": 1,
            "_source": {
              "title": "TP-LINK TL-WDR5620 1200M 5G双频智能无线路由器 四天线智能wifi 稳定穿墙高速家用路由器",
              "stock": 9970
            }
          },
          {
            "_index": "shop",
            "_type": "goods",
            "_id": "PYEGtmgBBdkQnU_d4b4m",
            "_score": 1,
            "_source": {
              "title": "小米8 全面屏游戏智能手机 6GB+64GB 黑色 全网通4G 双卡双待",
              "stock": 8800
            }
          },
          {
            "_index": "shop",
            "_type": "goods",
            "_id": "PoEGtmgBBdkQnU_d6b4Q",
            "_score": 1,
            "_source": {
              "title": "OPPO Find X曲面全景屏 波尔多红 8GB+128GB 全网通 移动联通电信全网通4G 双卡双待手机",
              "stock": 5600
            }
          }
        ]
      }
    }

    三、高级查询

    3.1 范围查询(range)

    查询找出那些落在指定区间内的数字或者时间

    查询指令:

    GET /shop/_search
    {
      "query": {
        "range": {
          "price": {
            "gte": 2000,
            "lte": 5000
          }
        }
      }
    }

    查询结果:

    {
      "took": 3,
      "timed_out": false,
      "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
      },
      "hits": {
        "total": 2,
        "max_score": 1,
        "hits": [
          {
            "_index": "shop",
            "_type": "goods",
            "_id": "PYEGtmgBBdkQnU_d4b4m",
            "_score": 1,
            "_source": {
              "title": "小米8 全面屏游戏智能手机 6GB+64GB 黑色 全网通4G 双卡双待",
              "price": 2299,
              "stock": 8800
            }
          },
          {
            "_index": "shop",
            "_type": "goods",
            "_id": "PoEGtmgBBdkQnU_d6b4Q",
            "_score": 1,
            "_source": {
              "title": "OPPO Find X曲面全景屏 波尔多红 8GB+128GB 全网通 移动联通电信全网通4G 双卡双待手机",
              "price": 4999,
              "stock": 5600
            }
          }
        ]
      }
    }

    range查询允许以下操作符

     3.2 布尔查询(bool)

    bool把各种其它查询通过must(与)、must_not(非)、should(或)的方式进行组合

    查询指令:

    GET /shop/_search
    {
      "query": {
        "bool": {
          "should": [
            {
              "match": {
                "title": "笔记本"
              }
            },
            {
              "term": {
                "price": 109.00
              }
            }
          ]
        }
      }
    }

    查询结果:

    {
      "took": 15,
      "timed_out": false,
      "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
      },
      "hits": {
        "total": 2,
        "max_score": 1.9646256,
        "hits": [
          {
            "_index": "shop",
            "_type": "goods",
            "_id": "P4EGtmgBBdkQnU_d8b7I",
            "_score": 1.9646256,
            "_source": {
              "title": "联想(Lenovo)拯救者Y7000P英特尔酷睿 i7 15.6英寸游戏笔记本电脑(i7-8750H 8G 512G SSD GTX1060 144Hz黑)",
              "price": 8599,
              "stock": 1900
            }
          },
          {
            "_index": "shop",
            "_type": "goods",
            "_id": "QIEGtmgBBdkQnU_d-r6T",
            "_score": 1,
            "_source": {
              "title": "TP-LINK TL-WDR5620 1200M 5G双频智能无线路由器 四天线智能wifi 稳定穿墙高速家用路由器",
              "price": 109,
              "stock": 9970
            }
          }
        ]
      }
    }

    四、过滤查询

    所有的查询都会影响到文档的评分及排名。如果我们需要在查询结果中进行过滤,并且不希望过滤条件影响评分,

    那么就不要把过滤条件作为查询条件来用,而是使用filter方式。

    查询指令:

    GET /shop/_search
    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "title": "手机"
              }
            }
          ],
          "filter": {
            "range": {
              "price": {
                "gt": 3000
              }
            }
          }
        }
      }
    }

    查询结果:

    {
      "took": 5,
      "timed_out": false,
      "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
      },
      "hits": {
        "total": 1,
        "max_score": 0.17329858,
        "hits": [
          {
            "_index": "shop",
            "_type": "goods",
            "_id": "PoEGtmgBBdkQnU_d6b4Q",
            "_score": 0.17329858,
            "_source": {
              "title": "OPPO Find X曲面全景屏 波尔多红 8GB+128GB 全网通 移动联通电信全网通4G 双卡双待手机",
              "price": 4999,
              "stock": 5600
            }
          }
        ]
      }
    }

    注意:filter中还可以再次进行bool组合条件过滤。 

    五、排序查询

    sort 可以让我们按照不同的字段进行排序,并且通过order指定排序的方式。

    查询指令:

    GET /shop/_search
    {
      "query": {
        "match_all": {}
      },
      "sort": [
        {
          "price": {
            "order": "desc"
          }
        }
      ]
    }

    查询结果:

    {
      "took": 55,
      "timed_out": false,
      "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
      },
      "hits": {
        "total": 4,
        "max_score": null,
        "hits": [
          {
            "_index": "shop",
            "_type": "goods",
            "_id": "P4EGtmgBBdkQnU_d8b7I",
            "_score": null,
            "_source": {
              "title": "联想(Lenovo)拯救者Y7000P英特尔酷睿 i7 15.6英寸游戏笔记本电脑(i7-8750H 8G 512G SSD GTX1060 144Hz黑)",
              "price": 8599,
              "stock": 1900
            },
            "sort": [
              8599
            ]
          },
          {
            "_index": "shop",
            "_type": "goods",
            "_id": "PoEGtmgBBdkQnU_d6b4Q",
            "_score": null,
            "_source": {
              "title": "OPPO Find X曲面全景屏 波尔多红 8GB+128GB 全网通 移动联通电信全网通4G 双卡双待手机",
              "price": 4999,
              "stock": 5600
            },
            "sort": [
              4999
            ]
          },
          {
            "_index": "shop",
            "_type": "goods",
            "_id": "PYEGtmgBBdkQnU_d4b4m",
            "_score": null,
            "_source": {
              "title": "小米8 全面屏游戏智能手机 6GB+64GB 黑色 全网通4G 双卡双待",
              "price": 2299,
              "stock": 8800
            },
            "sort": [
              2299
            ]
          },
          {
            "_index": "shop",
            "_type": "goods",
            "_id": "QIEGtmgBBdkQnU_d-r6T",
            "_score": null,
            "_source": {
              "title": "TP-LINK TL-WDR5620 1200M 5G双频智能无线路由器 四天线智能wifi 稳定穿墙高速家用路由器",
              "price": 109,
              "stock": 9970
            },
            "sort": [
              109
            ]
          }
        ]
      }
    }
  • 相关阅读:
    Oracle DUL/AUL/ODU 工具说明
    Mysql 不同版本 说明
    Oracle RAC ASM disk header 备份 恢复 与 重建 示例说明
    Oracle checkpoint 说明
    Oracle RBA(Redo Byte Address) 说明
    Oracle wrap 和 unwrap( 加密与解密) 说明
    Oracle 10.2.0.5.4 Patch Set Update (PSU) – Patch No: p12419392
    Oracle RAC CSS 超时计算 及 参数 misscount, Disktimeout 说明
    Oracle 估算数据库大小的方法
    Oracle 11g 11.2.0.2 Bug 10082277 – Excessive allocation in PCUR or KGLH0 heap of “kkscsAddChildNo”
  • 原文地址:https://www.cnblogs.com/heqiuyong/p/10351176.html
Copyright © 2011-2022 走看看