• 深入理解elasticsearch-读书笔记


    es基本查询

    添加索引

    put /new_test
    {
      "mappings": {
        "properties": {
          "author": {
            "type": "text"
          },
          "characters": {
            "type": "text"
          },
          "copies": {
            "type": "long"
          },
          "otitle": {
            "type": "text"
          },
          "tags": {
            "type": "keyword"
          },
          "title": {
            "type": "text"
          },
          "year": {
            "type": "long"
          },
          "available": {
            "type": "boolean"
          },
          "review": {
            "type": "nested",
            "properties": {
              "nickname": {
                "type": "text"
              },
              "text": {
                "type": "text"
              },
              "stars": {
                "type": "integer"
              }
            }
          }
        }
      }
    }
    

    批量插入数据

    post /new_test/_bulk
    { "index" : {  "_id" : "1" } }
    {"title":"All Quiet on the Western Front","otitle":"Im Westen nichts Neues","author":"Erich Maria Remarque","year":1929,"characters":["Paul Bäumer","Albert Kropp","Haie Westhus","Fredrich Müller","Stanislaus Katczinsky","Tjaden"],"tags":["novel"],"copies":1,"available":true,"section":3}
    {"index":{"_id":"2"}}
    {"title":"Catch-22","author":"Joseph Heller","year":1961,"characters":["John Yossarian","Captain Aardvark","Chaplain Tappman","Colonel Cathcart","Doctor Daneeka"],"tags":["novel"],"copies":6,"available":false,"section":1}
    {"index":{"_id":"3"}}
    {"title":"The Complete Sherlock Holmes","author":"Arthur Conan Doyle","year":1936,"characters":["Sherlock Holmes","Dr. Watson","G. Lestrade"],"tags":[],"copies": 0, "available":false, "section":12}
    

    查询[1,3]区间的数据

    get /new_test/_search
    {
      "query":{
        "range":{
          "copies":{
            "gte":1,
            "lte":3
          }
        }
      }
    }
    

    找出所有至少有一本的书籍,并对1950年后出版的书籍进行加权

    get /new_test/_search
    {
      "query":{
        "bool":{
          "must":[{"range":{"copies":{"gte":1}}}],
          "should":[{"range":{"year":{"gt":1950}}}]
            
        }
      }
    }
    

    查找出所有tags字段包含novel值的书籍

    get /new_test/_search
    {
      "query":{
         "term":{
           "tags":"novel"
         }
      }
    }
    

    前缀查询

    get /new_test/_search
    {
      "query":{
         "prefix":{
           "title":"qu"
         }
      }
    }
    

    匹配短语

    get /new_test/_search
    {
      "query":{
        "match_phrase":{
          "otitle":"nichts"
        }
      }
    }
    
    

    最佳字段匹配

    GET /library/_search?pretty
    {
     "query" : {
      "multi_match" : {
       "query" : "Holmes",
       "fields" : [ "title", "author", "characters" ],
       "type" : "best_fields",
       "tie_breaker" : 0.8
      }
     }
    }
    

    短语匹配

    GET /library/_search?pretty
    {
    
     "query" : {
      "multi_match" : {
       "query" : "sherlock holmes",
       "fields" : [ "title", "author" ],
       "type" : "phrase"
      }
     }
    }
    

    带前缀短语匹配

    GET /library/_search?pretty
    {
    
     "query" : {
      "multi_match" : {
       "query" : "Cat",
       "fields" : [ "title", "author" ],
       "type" : "phrase_prefix"
      }
     }
    }
     
    

    改正用户拼写错误

    get /bank/_search
    {
      "suggest":{
        "first_suggestion":{
          "text":"chorme",
          "term":{
            "field":"title"
          }
        }
      }
    }
    

    会得到不同的用户建议和分数

    text:Elasticsearch给出的建议词。
    score:建议词的得分,得分越高的建议词,其质量越高。
    freq:建议词在文档中出现的频率

  • 相关阅读:
    ad域的那些事儿
    关于vs无法创建虚拟目录的问题
    关于Java链接c#的webapi的注意事项
    创建.net framework webapi出现“Web 服务器被配置为不列出此目录的内容。”错误
    vs2017专业版和企业版的密钥
    数据库‘master’中拒绝CREATE DATABASE权限
    vue局部路由守卫使用
    记一次关于vantUI 下拉列表list加载数据的问题
    vue中使用require动态拼接img路径
    记录一次关于el-tree中让内容与左边有距离的爬坑记录
  • 原文地址:https://www.cnblogs.com/Baronboy/p/15343528.html
Copyright © 2020-2023  润新知