• elasticsearch02Request Body深入搜索


    02. Request Body深入搜索

    
    # 获取健康值
    GET _cat/health
    
    
    # 创建一个_id=1的记录!
    PUT /test1/_doc/1
    {
      "name":"haima",
      "age":35,
      "gender":"男"
    }
    
    # 获取mappings信息
    GET test1
    GET test1/_mapping
    GET test1/_mapping?pretty
    
    
    # 获取所有索引所有信息
    GET _search
    
    # 获取test1所有信息
    GET test1/_search
    
    # 获取test1索引 类型为_doc所有信息
    GET test1/_doc/_search
    
    # 获取test1所有信息
    GET test1/_search
    {
      "query": {
        "match_all": {}
      }
    }
    
    # 修改单个字段
    POST test1/_update/1
    {
     "doc": {
       "age":37
     }
    }
    
    # 获取id为1的
    GET /test1/_doc/1
    
    # 删除test1索引的命令
    DELETE test1
    
    # 删除test1索引 _id=2的记录
    DELETE test1/_doc/2
    
    

    1.1 term查询

    term是表达语义的最小单位,在搜索的时候基本都要使用到term,精确查询,不会分词。
    term查询的种类有:Term Query、Terms Query、Range Query等。
    Term Query: 查单个字段 匹配单个单词
    Terms Query: 查单个字段 匹配多个单词
    Range Query: 范围查询
    Constant Score: 将查询转换为一个filter,避免算分,利用缓存

    在ES中,Term查询不会对输入进行分词处理,将输入作为一个整体,在倒排索引中查找准确的词项。 我们也可以使用 Constant Score 将查询转换为一个filter,避免算分,利用缓存,提高查询的效率。

    1.1.1 term 与 terms

    
    # 查movies所有记录
    GET movies/_search
    
    # query term查询 
    # 查单个字段 匹配单个单词
    # 查询电影名字中包含有 beautiful 这个单词的所有的电影,用于查询的单词不会进行分词的处理
    GET movies/_search
    {
      "query": {
        "term": {
          "title": {
            "value": "beautiful"
          }
        }
      }
    }
    
    # query terms查询
    # 查单个字段 匹配多个单词
    # 查询电影名字中包含有 beautiful 或者 mind 这两个单词的所有的电影,用于查询的单词不会进行 分词的处理
    GET movies/_search
    {
      "query": {
        "terms": {
          "title": [
            "beautiful",
            "mind"
          ]
        }
      }
    }
    
    

    1.1.2 range 范围查询

    
    # 查询上映在2016到2018年的所有的电影,再根据上映时间的倒序进行排序 desc / asc
    GET movies/_search
    {
      "query": {
        "range": {
          "year": {
            "gte": 2016,
            "lte": 2018
          }
        }
      }
      ,"sort": [
        {
          "year": {
            "order": "desc"
          }
        }
      ]
    }
    
    

    1.1.3 Constant Score

    # 查询title中包含有beautiful的所有的电影,不进行相关性算分,查询的数据进行缓存,提高效率
    GET movies/_search
    {
      "query": {
        "constant_score": {
          "filter": {
            "term": {
              "title": "beautiful"
            }
          }
        }
      }
    }
    

    1.2 全文查询

    全文查询的种类有: Match Query、Match Phrase Query、Query String Query等
    Match Query: 查单个字段 匹配单个或多个单词
    Match Phrase Query:
    Query String Query:
    索引和搜索的时候都会进行分词,在查询的时候,会对输入进行分词,然后每个词项会逐个到底层进行 查询,将最终的结果进行合并

    1.2.1 match

    # 查询电影名字中包含有beautiful的所有电影,每页十条,取第二页的数据
    GET movies/_search
    {
      "query": {
        "match": {
          "title": "beautiful"
        }
      },
      "size": 10,
      "from": 10
    }
    
    # 查询电影名字中包含有beautiful的所有电影
    # 用关键词查询,keyword会不分词,会区分大小写,
    # 每页十条,取第一页的数据
    GET movies/_search
    {
      "query": {
        "match": {
          "title.keyword": "Beautiful"
        }
      },
      "size": 10,
      "from": 0
    }
    
    
    # 查询电影名字中包含有 beautiful 或者 mind 的所有的数据,但是只查询title和id两个属性
    GET movies/_search
    {
      "_source": ["id","title"],
      "query": {
        "match": {
          "title": "beautiful mind"
        }
      }
    }
    
    
    # query match查询 
    GET /test1/_search
    {
      "query": {
        "match": {
          "name.keyword": "haima"
        }
      }
    }
    
    # 前置匹配
    GET /test1/_search
    {
      "query": {
        "prefix": {
          "name.keyword": {
            "value": "hai"
          }
        }
      }
    }
    
    # filter
    GET /test1/_search
    {
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "name.keyword": "haima"
              }
            }
          ],
          "filter": [
            {
              "term": {
                "name.keyword": "haima"
              }
            }
          ]
        }
      }
    }
    
    GET /test1/_search
    {
      "query": {
        "bool": {
          "must": [
            {
              "match_all": {}
            }
          ],
          "filter": [
            {
              "ids": {
                "values": [
                  "1"
                ]
              }
            }
          ]
        }
      }
    }
    
    
    GET /kibana_sample_data_flights/_search
    {
       "query": {
          "bool":{
            "filter":{
                "term":{
                    "DestCountry":"CN"
                }
            }
          }
       },
       "aggs": {
         "agg_OriginWeather": {
           "terms": {
             "field":"OriginWeather",
             "size": 10
           }
         },
          "agg_DestCityName": {
           "terms": {
             "field":"DestCityName",
             "size": 10
           }
         }
       }, 
       "size": 1
    }
    
    # 分词器
    GET _analyze
    {
      "analyzer":"ik_smart",
      "text": "我是中国人"
    }
    
    GET _analyze
    {
      "analyzer":"ik_max_word",
      "text": "我是中国人"
    }
    
    GET _analyze
    {
      "analyzer":"ik_max_word",
      "text":"狂神说java"
    }
    
    PUT /test2
    {
      "mappings": {
        "_doc": {
          "properties": {
            "name": {
              "type": "text"
            },
            "age": {
              "type": "long"
            },
            "birthday": {
              "type": "date"
            }
          }
        }
      }
    }
    
    # 时间类型
    PUT /test2
    {
      "mappings": {
          "properties": {
            "date": {
              "type":   "date",
              "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd ||HH:mm:ss ||epoch_millis"
            }
          }
      }
    }
    
    GET /test2/_mapping?pretty
    
    # 获取所有
    GET /test2/_doc/_search
    
    # 获取所有
    POST /test2/_search
    {
      "query": {
       "match_all": {}
      }
    }
    
    DELETE test2
    
    POST _bulk
    {"index":{"_index":"test2","_type":"_doc","_id":1}}
    { "date": "2022-01-02" }
    {"index":{"_index":"test2","_type":"_doc","_id":2}}
    { "date": "12:00:00" }
    {"index":{"_index":"test2","_type":"_doc","_id":3}}
    { "date": "1420070400001" }
    {"index":{"_index":"test2","_type":"_doc","_id":4}}
    { "date": "2018-10-01 12:00:00" }
    
    
    
  • 相关阅读:
    组合数,错排——HDU-2049
    欧拉函数——POJ-2480
    欧拉函数——HYSBZ
    数论——HYSBZ
    cordova js调用原生
    Backbone js 学习
    最优二叉搜索树 java实现 学习 备忘
    chrome允许加载本地文件
    IOS、Android html5页面输入的表情符号变成了乱码”???“
    创建第一个android应用
  • 原文地址:https://www.cnblogs.com/haima/p/16375615.html
Copyright © 2020-2023  润新知