ElasticSearch 7.x

Elasticsearch 7.x

簡介

  • Elasticsearch是一個開源揪利,基于Apache Lucene庫構(gòu)建的Restful搜索引擎
  • Elasticsearch是在Solr之后幾年推出的符糊。它提供了一個分布式,多租戶能力的全文搜索引擎,具有HTTP Web界面(REST)和無架構(gòu)JSON文檔。 Elasticsearch的官方客戶端庫提供Java, Groovy, PHP鳞青, Ruby, Perl为朋, Python臂拓, .NET和Javascript

官網(wǎng)地址

核心概念

  • 索引(index)
    • 一個索引可以理解成一個關(guān)系型數(shù)據(jù)庫
  • 類型(type)
    • 一種type就像一類表,比如user表习寸, order表
    • 注意:
      • ES 5.x中一個index可以有多種type
      • ES 6.x中一個index只能有一種type
      • ES 7.x以后已經(jīng)移除type這個概念
  • 映射(mapping)
    • mapping定義了每個字段的類型等信息胶惰。相當(dāng)于關(guān)系型數(shù)據(jù)庫中的表結(jié)構(gòu)
  • 文檔(document)
    • 一個document相當(dāng)于關(guān)系型數(shù)據(jù)庫中的一行記錄
  • 字段(field)
    • 相當(dāng)于關(guān)系型數(shù)據(jù)庫表的字段
  • 集群(cluster)
    • 集群由一個或多個節(jié)點組成,一個集群有一個默認(rèn)名稱"elasticsearch"
  • 節(jié)點(node)
    • 集群的節(jié)點霞溪,一臺機?或者一個進程
  • 分片和副本(shard)
    • 副本是分片的副本孵滞。分片有主分片(primary Shard)和副本分片(replica Shard)之分
    • 一個Index數(shù)據(jù)在物理上被分布在多個主分片中,每個主分片只存放部分?jǐn)?shù)據(jù)
    • 每個主分片可以有多個副本鸯匹,叫副本分片坊饶,是主分片的復(fù)制

字段類型

核心數(shù)據(jù)類型

分類 類型 描述
字符串 text 用于全文索引,該類型的字段將通過分詞?進行分詞
字符串 keyword 不分詞殴蓬,只能搜索該字段的完整的值
數(shù)值型 long, integer, short, byte, double, float, half_float, scaled_float -
布爾 boolean -
二進制 binary 該類型的字段把值當(dāng)做經(jīng)過 base64 編碼的字符串匿级,默認(rèn)不存儲,且不可搜索
范圍類型 integer_range, float_range, long_range, double_range, date_range 范圍類型表示值是一個范圍,而不是一個具體的值痘绎;譬如 age 的類型是 integer_range津函,那么值可以是 {"gte" : 20, "lte" : 40};搜索 "term" :{"age": 21} 可以搜索該值
日期 date 由于Json沒有date類型孤页,所以es通過識別字符串是否符合format定義的格式來判斷是否為date類型尔苦;format默認(rèn)為strict_date_optiona_time||epoch_millis;格式"2022-01-01"散庶,"2022/01/01 12:10:30",或從開始紀(jì)元(1970年年1? 1?日 0點) 開始的毫秒數(shù)

復(fù)雜數(shù)據(jù)類型

  • 數(shù)組類型 Array

    • ES中沒有專門的數(shù)組類型, 直接使用[]定義即可凌净,數(shù)組中所有的值必須是同一種數(shù)據(jù)類型, 不支持混合數(shù)據(jù)類型的數(shù)組
    • 字符串?dāng)?shù)組 [ "one", "two" ] 悲龟,整數(shù)數(shù)組 [ 1, 2 ]
    • Object對象數(shù)組 [ { "name": "Louis", "age": 18 }, { "name": "Daniel", "age": 17 }]
    • 同一個數(shù)組只能存同類型的數(shù)據(jù),不能混存冰寻,譬如 [ 10, "some string" ] 是錯誤的
  • 對象類型 Object

    • 對象類型可能有內(nèi)部對象

      {
          "name": "李蒙",
          "age": 14,
          "sex": "0",
          "class": "7(2)班",
          "birthday": "2005-10-15"
          "hobbies": [
              "閱讀",
              "跑步"
          ],
          "address":
          {
              "province": "山東",
              "location":
              {
                  "city": "日照"
              }
          }
      }
      

專?用數(shù)據(jù)類型

  • IP類型

    IP類型的字段?用于存儲IPv4或IPv6的地址, 本質(zhì)上是?一個?長整型字段

索引

功能 請求方式 url 參數(shù)
新增 PUT(必須) localhost:9200/stu -
獲取 GET localhost:9200/stu -
刪除 DELETE localhost:9200/stu -
批量獲取 GET localhost:9200/stu,tea -
獲取所有1 GET localhost:9200/_all -
獲取所有2 GET localhost:9200/_cat/indices?v -
存在 HEAD localhost:9200/stu -
關(guān)閉 POST localhost:9200/stu/_close -
打開 POST localhost:9200/stu/_open -
自動創(chuàng)建索引 PUT localhost:9200/_cluster/settings 見下
數(shù)據(jù)復(fù)制 POST localhost:9200/_reindex 見下
  • 新增

    PUT localhost:9200/stu

    // 響應(yīng)
    {
        "acknowledged": true,
        "shards_acknowledged": true,
        "index": "stu"
    }
    
  • 獲取

    GET localhost:9200/stu

    // 響應(yīng)
    {
        "stu": {
            "aliases": {},//別名
            "mappings": {},//映射
            "settings": {
                "index": {
                    "creation_date": "1576139082806",//創(chuàng)建時間
                    "number_of_shards": "1",//分片
                    "number_of_replicas": "1",//副本
                    "uuid": "-ocQkbgoSyG2vDTsugK_9Q",
                    "version": {
                        "created": "7020099"
                    },
                    "provided_name": "stu"
                }
            }
        }
    }
    
  • 刪除

    DELETE localhost:9200/stu

    {
        "acknowledged": true
    }
    
  • 批量獲取

    GET localhost:9200/stu,tea

    // 響應(yīng)
    {
        "stu": {
            "aliases": {},
            "mappings": {},
            "settings": {
                "index": {
                    "creation_date": "1576139586417",
                    "number_of_shards": "1",
                    "number_of_replicas": "1",
                    "uuid": "H9dyTutEQg-4OsV2Byt-gA",
                    "version": {
                        "created": "7020099"
                    },
                    "provided_name": "stu"
                }
            }
        },
        "tea": {
            "aliases": {},
            "mappings": {},
            "settings": {
                "index": {
                    "creation_date": "1576139593175",
                    "number_of_shards": "1",
                    "number_of_replicas": "1",
                    "uuid": "nYhKuggbT_Wa2RI-M_COGA",
                    "version": {
                        "created": "7020099"
                    },
                    "provided_name": "tea"
                }
            }
        }
    }
    
  • 獲取所有1

    GET localhost:9200/_all

    // 響應(yīng)
    {
        "stu": {
            "aliases": {},
            "mappings": {},
            "settings": {
                "index": {
                    "creation_date": "1576139586417",
                    "number_of_shards": "1",
                    "number_of_replicas": "1",
                    "uuid": "H9dyTutEQg-4OsV2Byt-gA",
                    "version": {
                        "created": "7020099"
                    },
                    "provided_name": "stu"
                }
            }
        },
        "tea": {
            "aliases": {},
            "mappings": {},
            "settings": {
                "index": {
                    "creation_date": "1576139593175",
                    "number_of_shards": "1",
                    "number_of_replicas": "1",
                    "uuid": "nYhKuggbT_Wa2RI-M_COGA",
                    "version": {
                        "created": "7020099"
                    },
                    "provided_name": "tea"
                }
            }
        }
    }
    
  • 獲取所有2

    GET localhost:9200/_cat/indices?v

    // 響應(yīng)
    health status index                           uuid                   pri rep docs.count docs.deleted store.size pri.store.size
    green  open   .kibana_task_manager            IjZxE0H9TtmTrpBgzjr-qg   1   0          2            0     12.8kb         12.8kb
    yellow open   stu                             H9dyTutEQg-4OsV2Byt-gA   1   1          0            0       283b           283b
    yellow open   tea                             nYhKuggbT_Wa2RI-M_COGA   1   1          0            0       283b           283b
    
  • 存在

    HEAD localhost:9200/stu

    // 響應(yīng)-存在
    200 ok
    
  • 關(guān)閉

    POST localhost:9200/stu/_close

    // 響應(yīng)
    {
        "acknowledged": true,
        "shards_acknowledged": true
    }
    
  • 打開

    POST localhost:9200/stu/_open

    // 響應(yīng)
    {
        "acknowledged": true,
        "shards_acknowledged": true
    }
    
  • 自動創(chuàng)建索引

    插入文檔時(見下)是否自動創(chuàng)建索引

    GET 請求http://localhost:9200/_cluster/settings 查看auto_create_index 的狀態(tài)

    true自動創(chuàng)建

    • 修改auto_create_index 的狀態(tài)

      PUT localhost:9200/_cluster/settings

      // 參數(shù)
      {
          "persistent": {
              "action.auto_create_index": "true"http://true或false
          }
      }
      
  • 數(shù)據(jù)復(fù)制(結(jié)合索引別名须教,可以重建索引并導(dǎo)入數(shù)據(jù))

    POST localhost:9200/_reindex

    {
      "source": {
        "index": "stu"
      },
      "dest": {
        "index": "stu_oth"
      }
    }
    

索引別名

在開發(fā)中,隨著業(yè)務(wù)需求的迭代斩芭,較?的業(yè)務(wù)邏輯就要?臨更新甚?是重構(gòu)轻腺,?對于es來說,為了適應(yīng)新的業(yè)務(wù)邏輯划乖,可能就要對原有的索引做?些修改贬养,?如對某些字段做調(diào)整,甚?是重建索引琴庵。?做這些操作的時候误算,可能會對業(yè)務(wù)造成影響,甚?是停機調(diào)整等問題迷殿。由此儿礼,es提供了索引別名來解決這些問題。 索引別名就像?個快捷?式或是軟連接庆寺,可以指向?個或多個索引蚊夫,也可以給任意?個需要索引名的API來使?。別名的應(yīng)?為程序提供了極?地靈活性

多個索引可以指定同一個別名懦尝,一個索引也可以指定多個別名

功能 請求方式 url 參數(shù)
查詢 GET localhost:9200/_alias; localhost:9200/stu/_alias -
新增 POST localhost:9200/_aliases 見下
新增 PUT localhost:9200/stu/_alias/stu_v1.0 -
刪除 POST localhost:9200/_aliases 見下
刪除 DELETE localhost:9200/stu/_alias/stu_v1.0 -
重命名 POST localhost:9200/_aliases 見下
  • 新增

    POST localhost:9200/_aliases

    {
      "actions": [
        {
          "add": {
            "index": "stu",
            "alias": "stu_1214"
          }
        }
      ]
    }
    
  • 刪除

    POST localhost:9200/_aliases

    {
      "actions": [
        {
          "remove": {
            "index": "stu",
            "alias": "stu_v1.1"
          }
        }
      ]
    }
    
  • 重命名

    POST localhost:9200/_aliases

    {
      "actions": [
        {
          "remove": {
            "index": "stu",
            "alias": "stu_1214"
          }
        },
        {
          "add": {
            "index": "stu",
            "alias": "stu_1215"
          }
        }
      ]
    }
    
  • 當(dāng)別名指定了多個索引知纷,可以指定寫某個索引

    POST localhost:9200/_aliases

    {
      "actions": [
        {
          "add": {
            "index": "stu",
            "alias": "alia_v1.0",
            "is_write_index": "true"
          }
        },
        {
          "add": {
            "index": "tea",
            "alias": "alia_v1.0"
          }
        }
      ]
    }
    

映射

功能 請求方式 url 參數(shù)
新增 PUT localhost:9200/stu/_mapping 見下
獲取 GET localhost:9200/stu/_mapping -
批量獲取 GET localhost:9200/stu,tea/_mapping -
獲取所有1 GET localhost:9200/_mapping -
獲取所有2 GET localhost:9200/_all/_mapping -
修改 PUT localhost:9200/stu/_mapping 見下
  • 新增

    PUT localhost:9200/stu/_mapping

    // 參數(shù)
    {
        "properties":
        {
            "name":
            {
                "type": "text"
            },
            "age": {
                "type": "long"
            },
            "sex": {
                "type": "keyword"
            },
            "class": {
                "type": "keyword"
            }
        }
    }
    
  • 獲取

    GET localhost:9200/stu/_mapping

    // 響應(yīng)
    {
        "stu": {
            "mappings": {
                "properties": {
                    "age": {
                        "type": "long"
                    },
                    "class": {
                        "type": "keyword"
                    },
                    "name": {
                        "type": "text"
                    },
                    "sex": {
                        "type": "keyword"
                    }
                }
            }
        }
    }
    
  • 批量獲取

    GET localhost:9200/stu,tea/_mapping

    // 響應(yīng)
    {
        "tea": {
            "mappings": {}
        },
        "stu": {
            "mappings": {
                "properties": {
                    "age": {
                        "type": "long"
                    },
                    "class": {
                        "type": "keyword"
                    },
                    "name": {
                        "type": "text"
                    },
                    "sex": {
                        "type": "keyword"
                    }
                }
            }
        }
    }
    
  • 獲取所有1

    GET localhost:9200/_mapping

    // 響應(yīng)
    {
        "stu": {
            "mappings": {
                "properties": {
                    "age": {
                        "type": "long"
                    },
                    "class": {
                        "type": "keyword"
                    },
                    "name": {
                        "type": "text"
                    },
                    "sex": {
                        "type": "keyword"
                    }
                }
            }
        },
        "tea": {
            "mappings": {}
        },
    }
    
  • 獲取所有2

    GET localhost:9200/_all/_mapping

    // 響應(yīng)
    {
        "stu": {
            "mappings": {
                "properties": {
                    "age": {
                        "type": "long"
                    },
                    "class": {
                        "type": "keyword"
                    },
                    "name": {
                        "type": "text"
                    },
                    "sex": {
                        "type": "keyword"
                    }
                }
            }
        },
        "tea": {
            "mappings": {}
        },
    }
    
  • 修改

    注意:

    修改映射時,只能新增字段陵霉,不能修改或刪除已存在的字段

    PUT localhost:9200/stu/_mapping

    // 參數(shù)
    {
        "properties":
        {
            "name":
            {
                "type": "text"
            },
            "age": {
                "type": "long"
            },
            "sex": {
                "type": "keyword"
            },
            "class": {
                "type": "keyword"
            },
            "birthday": {
                "type": "date"
            }
        }
    }
    

文檔

功能 請求方式 url 參數(shù)
新增(指定id) PUT localhost:9200/stu/_doc/1 見下
新增(不指定id) POST(必須) localhost:9200/stu/_doc 見下
指定操作類型 PUT localhost:9200/stu/_doc/1?op_type=create 見下
查看 GET localhost:9200/stu/_doc/1 -
查看多個?文檔 POST localhost:9200/_mget 見下
修改 POST localhost:9200/stu/_update/1 見下
刪除 DELETE localhost:9200/stu/_doc/1 -
刪除全部 POST localhost:9200/stu/_delete_by_query
  • 新增(指定id)

    PUT localhost:9200/stu/_doc/1

    // 參數(shù)
    {
        "name": "楊光",
        "age": 14,
        "sex": "1",
        "class": "7(2)班",
        "birthday": "2005-08-26"
    }
    
    // 響應(yīng)
    {
        "_index": "stu",
        "_type": "_doc",
        "_id": "1",
        "_version": 1,
        "result": "created",
        "_shards": {
            "total": 2,
            "successful": 1,
            "failed": 0
        },
        "_seq_no": 0,
        "_primary_term": 3
    }
    
  • 新增(不指定id)

    不指定id屈扎,系統(tǒng)會自動分配id

    POST localhost:9200/stu/_doc

    // 參數(shù)
    {
        "name": "張世杰",
        "age": 13,
        "sex": "0",
        "class": "7(5)班",
        "birthday": "2004-11-01"
    }
    
    // 響應(yīng)
    {
        "_index": "stu",
        "_type": "_doc",
        "_id": "C_SI-W4Bj7nk6pLmw4Er",//系統(tǒng)分配id
        "_version": 1,
        "result": "created",
        "_shards": {
            "total": 2,
            "successful": 1,
            "failed": 0
        },
        "_seq_no": 1,
        "_primary_term": 3
    }
    
  • 指定操作類型

    若不指定插入時的操作類型,向已存在的id插入數(shù)據(jù)撩匕,原數(shù)據(jù)會被更新掉鹰晨,并生成一個新的版本

    PUT localhost:9200/stu/_doc/1

    {
     "name": "楊光11",
     "age": 14,
     "sex": "1",
     "class": "7(2)班",
     "birthday": "2005-08-26"
    }
    
    {
     "_index": "stu",
     "_type": "_doc",
     "_id": "1",
     "_version": 2,//產(chǎn)生新的版本
     "result": "updated",//執(zhí)行結(jié)果時updated,而不是created
     "_shards": {
         "total": 2,
         "successful": 1,
         "failed": 0
     },
     "_seq_no": 2,
     "_primary_term": 3
    }
    

    PUT localhost:9200/stu/_doc/1?op_type=create (向已存在的id插入數(shù)據(jù)會報錯)

    // 參數(shù)
    {
        "name": "楊光22",
        "age": 14,
        "sex": "1",
        "class": "7(2)班",
        "birthday": "2005-08-26"
    }
    
    // 響應(yīng)
    {
        "error": {
            "root_cause": [
                {
                    "type": "version_conflict_engine_exception",
                    "reason": "[1]: version conflict, document already exists (current version [2])",
                    "index_uuid": "H9dyTutEQg-4OsV2Byt-gA",
                    "shard": "0",
                    "index": "stu"
                }
            ],
            "type": "version_conflict_engine_exception",
            "reason": "[1]: version conflict, document already exists (current version [2])",
            "index_uuid": "H9dyTutEQg-4OsV2Byt-gA",
            "shard": "0",
            "index": "stu"
        },
        "status": 409
    }
    
  • 查看

    GET localhost:9200/stu/_doc/1

    // 響應(yīng)
    {
        "_index": "stu",
        "_type": "_doc",
        "_id": "1",
        "_version": 2,
        "_seq_no": 2,
        "_primary_term": 3,
        "found": true,
        "_source": {
            "name": "楊光11",
            "age": 14,
            "sex": "1",
            "class": "7(2)班",
            "birthday": "2005-08-26"
        }
    }
    
  • 查看多個?文檔

    1. 方式一

      POST localhost:9200/_mget

      // 參數(shù)
      {
          "docs": [
          {
              "_index": "stu",
              "_type": "_doc",
              "_id": "1"
          },
          {
              "_index": "stu",
              "_type": "_doc",
              "_id": "C_SI-W4Bj7nk6pLmw4Er"
          }]
      }
      
      // 響應(yīng)
      {
          "docs": [
              {
                  "_index": "stu",
                  "_type": "_doc",
                  "_id": "1",
                  "_version": 3,
                  "_seq_no": 3,
                  "_primary_term": 3,
                  "found": true,
                  "_source": {
                      "name": "楊光33",
                      "age": 14,
                      "sex": "1",
                      "class": "7(2)班",
                      "birthday": "2005-08-26"
                  }
              },
              {
                  "_index": "stu",
                  "_type": "_doc",
                  "_id": "C_SI-W4Bj7nk6pLmw4Er",
                  "_version": 1,
                  "_seq_no": 1,
                  "_primary_term": 3,
                  "found": true,
                  "_source": {
                      "name": "張世杰",
                      "age": 13,
                      "sex": "0",
                      "class": "7(5)班",
                      "birthday": "2004-11-01"
                  }
              }
          ]
      }
      
    2. 方式二

      POST localhost:9200/stu/_mget

      // 參數(shù)
      {
          "docs": [
          {
              "_type": "_doc",
              "_id": "1"
          },
          {
              "_type": "_doc",
              "_id": "C_SI-W4Bj7nk6pLmw4Er"
          }]
      }
      
    3. 方式三

      POST localhost:9200/stu/_doc/_mget

      // 參數(shù)
      {
          "docs": [
          {
              "_id": "1"
          },
          {
              "_id": "C_SI-W4Bj7nk6pLmw4Er"
          }]
      }
      
  • 修改

    1. 根據(jù)提供的?文檔?片段更更新數(shù)據(jù)

      POST localhost:9200/stu/_update/1

      // 參數(shù)
      {
          "doc": {
              "name": "楊光33",
              "age": 14,
              "sex": "1",
              "class": "7(2)班",
              "birthday": "2005-08-26"
          }
      }
      
    2. 向_source字段,增加一個字段

      POST localhost:9200/stu/_update/1

      // 參數(shù)
      {
         "script": "ctx._source.height = \"173cm\""
      }
      
    3. 從_source字段模蜡,刪除一個字段

      POST localhost:9200/stu/_update/1

      // 參數(shù)
      {
         "script": "ctx._source.remove(\"height\")"
      }
      
  • 刪除

    DELETE localhost:9200/stu/_doc/1

    // 響應(yīng)
    {
        "_index": "stu",
        "_type": "_doc",
        "_id": "1",
        "_version": 6,
        "result": "deleted",
        "_shards": {
            "total": 2,
            "successful": 1,
            "failed": 0
        },
        "_seq_no": 6,
        "_primary_term": 3
    }
    
  • 刪除全部

    POST localhost:9200/stu/_delete_by_query

    // 參數(shù)
    {
        "query": {
            "match_all": {
                
            }
        }   
    }
    

查詢搜索

數(shù)據(jù)準(zhǔn)備:批量導(dǎo)入數(shù)據(jù)-ES提供了一個叫 bulk 的API 來進行批量操作

  • 數(shù)據(jù)

    {"index": {"_index": "stu", "_type": "_doc", "_id": 1}}
    {"name":"楊光","age":14,"sex":"1","class":"7(2)班","birthday":"2005-08-26"}
    {"index": {"_index": "stu", "_type": "_doc", "_id": 2}}
    {"name":"張世杰","age":13,"sex":"0","class":"7(5)班","birthday":"2004-11-01"}
    {"index": {"_index": "stu", "_type": "_doc", "_id": 3}}
    {"name":"李蒙","age":14,"sex":"0","class":"7(2)班","birthday":"2005-10-15"}
    {"index": {"_index": "stu", "_type": "_doc", "_id": 4}}
    {"name":"李沁","age":15,"sex":"0","class":"7(3)班","birthday":"2004-10-15"}
    {"index": {"_index": "stu", "_type": "_doc", "_id": 5}}
    {"name":"王昭","age":14,"sex":"1","class":"7(3)班","birthday":"2005-01-26"}
    {"index": {"_index": "stu", "_type": "_doc", "_id": 6}}
    {"name":"李明","age":14,"sex":"1","class":"7(2)班","birthday":"2005-03-26"}
    {"index": {"_index": "stu", "_type": "_doc", "_id": 7}}
    {"name":"張璐","age":14,"sex":"1","class":"7(5)班","birthday":"2005-06-02"}
    {"index": {"_index": "stu", "_type": "_doc", "_id": 8}}
    {"name":"李思敏","age":14,"sex":"1","class":"7(3)班","birthday":"2005-06-02"}
    {"index": {"_index": "stu", "_type": "_doc", "_id": 9}}
    {"name":"吳民錫","age":13,"sex":"1","class":"7(5)班","birthday":"2006-04-02"}
    {"index": {"_index": "stu", "_type": "_doc", "_id": 10}}
    {"name":"趙曦","age":14,"sex":"0","class":"7(2)班","birthday":"2005-09-02"}
    
    
  • POST bulk

    curl -X POST "localhost:9200/_bulk" -H 'Content-Type: application/json' --data-binary @name
    

term(詞條)查詢

單詞級別查詢-詞條查詢不會分析查詢條件漠趁,只有當(dāng)詞條和查詢字符串完全匹配時,才匹配搜索忍疾;這些查詢通常用于結(jié)構(gòu)化的數(shù)據(jù)闯传,比如: number, date, keyword等,而不是對text卤妒。也就是說甥绿,全文本查詢之前要先對文本內(nèi)容進行分詞,而單詞級別的查詢直接在相應(yīng)字段的反向索引中精確查找则披,單詞級別的查詢一般用于數(shù)值共缕、日期等類型的字段上。

功能 請求方式 url 參數(shù) 描述
單條term查詢 POST localhost:9200/stu/_search 見下 -
多條term查詢 POST localhost:9200/stu/_search 見下 -
Exsit Query POST localhost:9200/stu/_search 見下 特定的字段中查找?非空值的?文檔
Prefix Query POST localhost:9200/stu/_search 見下 查找包含帶有指定前綴term的?文檔
Wildcard Query POST localhost:9200/stu/_search 見下 支持通配符查詢士复, *表示任意字符图谷, ?表示任意單個字符
Regexp Query POST localhost:9200/stu/_search 見下 正則表達式查詢
Ids Query POST localhost:9200/stu/_search 見下 通過id查詢文檔
  • 單條term查詢

    POST localhost:9200/stu/_search

    // 參數(shù)
    {
        "query":{
            "term":{
                "sex": "1"
            }
        }
    }
    
    // 響應(yīng)
    {
        "took": 1,
        "timed_out": false,
        "_shards": {
            "total": 1,
            "successful": 1,
            "skipped": 0,
            "failed": 0
        },
        "hits": {
            "total": {
                "value": 1,
                "relation": "eq"
            },
            "max_score": 0.9808292,
            "hits": [
                {
                    "_index": "stu",
                    "_type": "_doc",
                    "_id": "1",
                    "_score": 0.9808292,
                    "_source": {
                        "name": "楊光",
                        "age": 14,
                        "sex": "1",
                        "class": "7(2)班",
                        "birthday": "2005-08-26"
                    }
                }
            ]
        }
    }
    
  • 多條term查詢

    POST localhost:9200/stu/_search

    // 參數(shù)
    {
        "query":{
            "terms":{
                "sex": ["0","1"]
            }
        }
    }
    
  • Exsit Query

    POST localhost:9200/stu/_search

    // 參數(shù)
    {
      "query": {
        "exists": {
          "field": "birthday"
        }
      }
    }
    
  • Prefix Query

    POST localhost:9200/stu/_search

    // 參數(shù)
    {
      "query": {
        "prefix": {
          "class": {
            "value": "7"
          }
        }
      }
    }
    
  • Wildcard Query

    POST localhost:9200/stu/_search

    // 參數(shù)
    {
      "query": {
        "wildcard": {
          "class": {
            "value": "*2*"
          }
        }
      }
    }
    
  • Regexp Query

    POST localhost:9200/stu/_search

    // 參數(shù)
    {
      "query": {
        "regexp": {
          "class": "7.*"
        }
      }
    }
    
  • Ids Query

    POST localhost:9200/stu/_search

    // 參數(shù)
    {
      "query": {
        "ids": {
          "values": [1,2]
        }
      }
    }
    

full text(全文)查詢

ElasticSearch引擎會先分析查詢字符串,將其拆分成多個分詞阱洪,只要已分析的字段中包含詞條的任意一個便贵,或全部包含,就匹配查詢條件冗荸,返回該文檔承璃;如果不包含任意一個分詞,表示沒有任何?文檔匹配查詢條件

類型 請求方式 url 參數(shù) 描述
match_all POST localhost:9200/stu/_search 見下 查詢?nèi)?/td>
match POST localhost:9200/stu/_search 見下 分詞匹配查詢
multi_match POST localhost:9200/stu/_search 見下 多字段查詢
match_phrase POST localhost:9200/stu/_search 見下 精確匹配
match_phrase_prefix POST localhost:9200/stu/_search 見下 模糊匹配(text)
  • match_all

    POST localhost:9200/nba/_search

    // 參數(shù)
    {
        "query":{
            "match_all":{}
        },
        "from": 0,
        "size": 10
    }
    
  • match

    POST localhost:9200/nba/_search

    // 參數(shù)
    {
      "query": {
        "match": {
          "name": "張"
        }
      }
    }
    
  • multi_match

    POST localhost:9200/nba/_search

    // 參數(shù)
    {
      "query": {
        "multi_match": {
          "query": "世",// 查詢條件
          "fields": ["name","class"]//查詢哪些字段
        }
      }
    }
    
  • match_phrase

    POST localhost:9200/nba/_search

    // 參數(shù)
    {
      "query": {
        "match_phrase": {
          "class": "7(2)班"
        }
      }
    }
    
  • match_phrase_prefix

    // 參數(shù)
    {
      "query": {
        "match_phrase_prefix": {
          "name": "世杰"
        }
      }
    }
    

范圍查詢

范圍查詢--日期蚌本、數(shù)字或字符串

POST localhost:9200/nba/_search

// 查詢年齡14-15歲的學(xué)生
{
  "query": {
    "range": {
      "age": {
        "gte": 14,
        "lte": 15
      }
    }
  }
}
// 查詢2003年到2004年出生的學(xué)生
{
  "query": {
    "range": {
      "birthday": {
        "gte": "2003",
        "lte": "31-12-2004",
        "format": "dd-MM-yyyy||yyyy"
      }
    }
  }
}

布爾查詢

類型 請求方式 url 參數(shù) 描述
must POST localhost:9200/nba/_search 見下 必須出現(xiàn)在匹配文檔中
filter POST localhost:9200/nba/_search 必須出現(xiàn)在文檔中绸硕,但是不打分
must_not POST localhost:9200/nba/_search 不能出現(xiàn)在文檔中
should POST localhost:9200/nba/_search 應(yīng)該出現(xiàn)在文檔中
  • must

    POST localhost:9200/nba/_search

    // 查詢sex為"0",name中含有"曦"的學(xué)生
    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "name": "曦"
              }
            },
            {
              "term": {
                "sex": {
                  "value": "0"
                }
              }
            }
          ]
        }
      }
    }
    
    // 響應(yīng)
    {
      "took" : 1,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 1,
          "relation" : "eq"
        },
        "max_score" : 2.9985561,
        "hits" : [
          {
            "_index" : "stu",
            "_type" : "_doc",
            "_id" : "10",
            "_score" : 2.9985561,// 分?jǐn)?shù)
            "_source" : {
              "name" : "趙曦",
              "age" : 14,
              "sex" : "0",
              "class" : "7(2)班",
              "birthday" : "2005-09-02"
            }
          }
        ]
      }
    }
    
    
  • filter

    效果同must魂毁,但是不打分

    POST localhost:9200/nba/_search

    {
      "query": {
        "bool": {
          "filter": [
            {
              "match": {
                "name": "曦"
              }
            },
            {
              "term": {
                "sex": {
                  "value": "0"
                }
              }
            }
          ]
        }
      }
    }
    
  • must_not

    POST localhost:9200/nba/_search

    // 查詢name包含"張",sex不是"0"的學(xué)生
    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "name": "張"
              }
            }
          ],
          "must_not": [
            {
              "term": {
                "sex": {
                  "value": "0"
                }
              }
            }
          ]
        }
      }
    }
    
  • should

    POST localhost:9200/nba/_search

    // 查詢sex為"1"的學(xué)生
    {
      "query": {
        "bool": {
          "should": [
            {
              "term": {
                "sex": {
                  "value": "1"
                }
              }
            }
          ]
        }
      }
    }
    

    與其他模式結(jié)合使用時即使匹配不到也返回玻佩,只是評分不同

    // 查詢name中包含"李",age在13-14之間的學(xué)生
    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "name": "李"
              }
            }
          ],
          "should": [
            {
              "range": {
                "age": {
                  "gte": 13,
                  "lte": 14
                }
              }
            }
          ]
        }
      }
    }
    

排序查詢

POST localhost:9200/nba/_search

// 查詢7(5)班學(xué)生,age倒序排列
{
  "query": {
    "term": {
      "class": {
        "value": "7(5)班"
      }
    }
  },
  "sort": [
    {
      "age": {
        "order": "desc"
      }
    }
  ]
}

聚合查詢

  • 聚合分析是數(shù)據(jù)庫中重要的功能特性席楚,完成對一個查詢的數(shù)據(jù)集中數(shù)據(jù)的聚合計算咬崔,如:找出某字段(或計算表達式的結(jié)果)的最大值、最小值烦秩,計算和垮斯、平均值等。 ES作為搜索引擎兼數(shù)據(jù)庫只祠,同樣提供了強大的聚合分析能力
  • 對一個數(shù)據(jù)集求最大兜蠕、最小、和抛寝、平均值等指標(biāo)的聚合熊杨,在ES中稱為指標(biāo)聚合
  • 而關(guān)系型數(shù)據(jù)庫中除了有聚合函數(shù)外曙旭,還可以對查詢出的數(shù)據(jù)進行分組group by,再在組上進行指標(biāo)聚合晶府。在ES中稱為桶聚合

指標(biāo)聚合

  • max min sum avg

    POST localhost:9200/nba/_search

    // max-7(3)班最大年齡
    {
      "query": {
        "term": {
          "class": {
            "value": "7(3)班"
          }
        }
      },
      "aggs": {
        "maxAge": {// 自定義名稱
          "max": {
            "field": "age"
          }
        }
      },
      "size": 0
    }
    
    // min-7(3)班最小年齡
    {
      "query": {
        "term": {
          "class": {
            "value": "7(3)班"
          }
        }
      },
      "aggs": {
        "minAge": {
          "min": {
            "field": "age"
          }
        }
      },
      "size": 0
    }
    
    // sum
    {
      "query": {
        "term": {
          "class": {
            "value": "7(3)班"
          }
        }
      },
      "aggs": {
        "sumAge": {
          "sum": {
            "field": "age"
          }
        }
      }, 
      "size": 0
    }
    
    // avg-7(3)班平均年齡
    {
      "query": {
        "term": {
          "class": {
            "value": "7(3)班"
          }
        }
      },
      "aggs": {
        "avgAge": {// 自定義名稱
          "avg": {
            "field": "age"
          }
        }
      },
      "size": 0
    }
    
  • value_count

    統(tǒng)計非空字段的文檔數(shù)

    POST localhost:9200/nba/_search

    // 查詢7(3)班年齡非空的學(xué)生總數(shù)
    {
      "query": {
        "term": {
          "class": {
            "value": "7(3)班"
          }
        }
      },
      "aggs": {
        "countAge": {
          "value_count": {
            "field": "age"
          }
        }
      }, 
      "size": 0
    }
    
  • Cardinality

    值去重計數(shù)

    POST localhost:9200/nba/_search

    // 7(3)班age去重統(tǒng)計
    {
      "query": {
        "term": {
          "class": {
            "value": "7(3)班"
          }
        }
      },
      "aggs": {
        "cardinalityAge": {
          "cardinality": {
            "field": "age"
          }
        }
      }, 
      "size": 0
    }
    
  • stats

    統(tǒng)計count max min avg sum 5個值

    POST localhost:9200/nba/_search

    {
      "query": {
        "term": {
          "class": {
            "value": "7(3)班"
          }
        }
      },
      "aggs": {
        "statsAge": {
          "stats": {
            "field": "age"
          }
        }
      }, 
      "size": 0
    }
    
  • Extended stats

    比stats多4個統(tǒng)計結(jié)果: 平方和桂躏、方差、標(biāo)準(zhǔn)差川陆、平均值加/減兩個標(biāo)準(zhǔn)差的區(qū)間

    POST localhost:9200/nba/_search

    {
      "query": {
        "term": {
          "class": {
            "value": "7(3)班"
          }
        }
      },
      "aggs": {
        "extendedAge": {
          "extended_stats": {
            "field": "age"
          }
        }
      }, 
      "size": 0
    }
    
  • Percentiles

    占比百分位對應(yīng)的值統(tǒng)計剂习,默認(rèn)返回[ 1, 5, 25, 50, 75, 95, 99 ]分位上的值

    POST localhost:9200/nba/_search

    {
      "query": {
        "term": {
          "class": {
            "value": "7(3)班"
          }
        }
      },
      "aggs": {
        "percentilesAge": {
          "percentiles": {
            "field": "age"
          }
        }
      }, 
      "size": 0
    }
    
    // 指定分位值
    {
      "query": {
        "term": {
          "class": {
            "value": "7(3)班"
          }
        }
      },
      "aggs": {
        "percentilesAge": {
          "percentiles": {
            "field": "age",
            "percents": [
              20,
              50,
              75
            ]
          }
        }
      },
      "size": 0
    }
    

桶聚合

  • Terms Aggregation 根據(jù)字段項分組聚合

    POST localhost:9200/nba/_search

    // 7(3)班按照age分組
    {
      "query": {
        "term": {
          "class": {
            "value": "7(3)班"
          }
        }
      },
      "aggs": {
        "aggsAge": {
          "terms": {
            "field": "age",
            "size": 5
          }
        }
      },
      "size": 0
    }
    
  • order 分組聚合排序

    POST localhost:9200/nba/_search

    // 7(3)班按照age分組,分組信息通過年齡從大到小排序
    {
      "query": {
        "term": {
          "class": {
            "value": "7(3)班"
          }
        }
      },
      "aggs": {
        "aggsAge": {
          "terms": {
            "field": "age",
            "size": 5,
            "order": {
              "_key": "desc"
            }
          }
        }
      },
      "size": 0
    }
    
    // 7(3)班按照age分組,分組信息通過文檔數(shù)從大到小排序
    {
      "query": {
        "term": {
          "class": {
            "value": "7(3)班"
          }
        }
      },
      "aggs": {
        "aggsAge": {
          "terms": {
            "field": "age",
            "size": 5,
            "order": {
              "_count": "desc"
            }
          }
        }
      },
      "size": 0
    }
    
    // 根據(jù)class分組,根據(jù)分組后的平均age倒排
    {
      "aggs": {
        "aggsClass": {
          "terms": {
            "field": "class",
            "size": 10,
            "order": {
              "aggsAge": "desc"
            }
          },
          "aggs": {
            "aggsAge": {
              "avg": {
                "field": "age"
              }
            }
          }
        }
      },
      "size": 0
    }
    
  • 篩選分組聚合

    POST localhost:9200/nba/_search

    {
      "aggs": {
        "aggsClass": {
          "terms": {
            "field": "class",
            "include": ["7(3)班", "7(2)班", "7(5)班"],// 包含
            "exclude": ["7(5)班"],// 排除
            "size": 10,
            "order": {
              "aggsAge": "desc"
            }
          },
          "aggs": {
            "aggsAge": {
              "avg": {
                "field": "age"
              }
            }
          }
        }
      },
      "size": 0
    }
    
    // 正則匹配
    // include较沪,exclude類型要一致
    {
      "aggs": {
        "aggsClass": {
          "terms": {
            "field": "class",
            "include": "7.*",
            "exclude": "7(5)班",
            "size": 10,
            "order": {
              "aggsAge": "desc"
            }
          },
          "aggs": {
            "aggsAge": {
              "avg": {
                "field": "age"
              }
            }
          }
        }
      },
      "size": 0
    }
    
  • Range Aggregation 范圍分組聚合

    POST localhost:9200/nba/_search

    // -13,13-14,15-  范圍分組
    {
      "aggs": {
        "aggsrange": {
          "range": {
            "field": "age",
            "ranges": [
              {
                "to": 13
              },
              {
                "from": 13,
                "to": 14
              },
              {
                "from": 15
              }
            ]
          }
        }
      },
      "size": 0
    }
    
    // 范圍分組-別名
    {
      "aggs": {
        "aggsrange": {
          "range": {
            "field": "age",
            "ranges": [
              {
                "to": 13,
                "key":"A"
              },
              {
                "from": 13,
                "to": 14,
                "key":"B"
              },
              {
                "from": 15,
                "key":"C"
              }
            ]
          }
        }
      },
      "size": 0
    }
    
  • Date Range Aggregation 時間范圍分組聚合

    POST localhost:9200/nba/_search

    // Date 時間范圍分組聚合
    {
      "aggs": {
        "aggsrange": {
          "date_range": {
            "field": "birthday",
            "format": "yyyy-MM",
            "ranges": [
              {
                "to": "2004-12",
                "key":"A"
              },
              {
                "from": "2005-01",
                "to": "2005-12",
                "key":"B"
              },
              {
                "from": "2006-01",
                "key":"C"
              }
            ]
          }
        }
      },
      "size": 0
    }
    
  • Date Histogram Aggregation 時間柱狀圖聚合

    按天鳞绕、月、年等進行聚合統(tǒng)計尸曼∶呛危可按 year (1y), quarter (1q), month (1M), week (1w), day(1d), hour (1h), minute (1m), second (1s) 間隔聚合

    POST localhost:9200/nba/_search

    {
      "aggs": {
        "aggsrange": {
          "date_histogram": {
            "field": "birthday",
            "format": "yyyy",
            "calendar_interval": "year"
          }
        }
      },
      "size": 0
    }
    

query_string查詢

  • 單個字段查詢

    POST localhost:9200/nba/_search

    {
      "query": {
        "query_string": {
          "default_field": "name",
          "query": "李 AND 思 OR 敏"
        }
      }
    }
    
  • 多個字段查詢

    POST localhost:9200/nba/_search

    {
      "query": {
        "query_string": {
          "fields": ["name", "sex"],
          "query": "李 AND 0"
        }
      }
    }
    

分詞器

將?用戶輸入的一段文本,按照一定邏輯骡苞,分析成多個詞語的一種工具

內(nèi)置分詞器

  • standard analyzer (標(biāo)準(zhǔn)分詞器)

    標(biāo)準(zhǔn)分析?是默認(rèn)分詞?垂蜗,如果未指定楷扬,則使用該分詞?

  • simple analyzer

    simple 分析?當(dāng)它遇到只要不是字母的字符解幽,就將文本解析成term,而且所有的term都是
    小寫的

  • whitespace analyzer

    whitespace 分析?烘苹,當(dāng)它遇到空白字符時躲株,就將文本解析成terms

  • stop analyzer

    stop 分析? 和 simple 分析?很像,唯一不同的是镣衡, stop 分析?增加了對刪除停止詞的支持霜定,默認(rèn)使?用了english停止詞

    stopwords 預(yù)定義的停止詞列表,比如 (the,a,an,this,of,at)等

  • language analyzer

    特定的語言的分詞?廊鸥,比如說望浩, english,英語分詞?),內(nèi)置語言: arabic, armenian,basque, bengali, brazilian, bulgarian, catalan, cjk, czech, danish, dutch, english, finnish,french, galician, german, greek, hindi, hungarian, indonesian, irish, italian, latvian,lithuanian, norwegian, persian, portuguese, romanian, russian, sorani, spanish,swedish, turkish, thai

  • pattern analyzer

    用正則表達式來將文本分割成terms惰说,默認(rèn)的正則表達式是\W+(非單詞字符)

eg:

GET /_analyze
{
  "analyzer": "simple", 
  "text": "Deploy a 14-day trial of Elasticsearch Service."
}
{
  "tokens" : [
    {
      "token" : "deploy",
      "start_offset" : 0,// 開始偏移量
      "end_offset" : 6,// 結(jié)束偏移量
      "type" : "word",
      "position" : 0 // 索引
    },
    {
      "token" : "a",
      "start_offset" : 7,
      "end_offset" : 8,
      "type" : "word",
      "position" : 1
    },
    {
      "token" : "day",
      "start_offset" : 12,
      "end_offset" : 15,
      "type" : "word",
      "position" : 2
    },
    {
      "token" : "trial",
      "start_offset" : 16,
      "end_offset" : 21,
      "type" : "word",
      "position" : 3
    },
    {
      "token" : "of",
      "start_offset" : 22,
      "end_offset" : 24,
      "type" : "word",
      "position" : 4
    },
    {
      "token" : "elasticsearch",
      "start_offset" : 25,
      "end_offset" : 38,
      "type" : "word",
      "position" : 5
    },
    {
      "token" : "service",
      "start_offset" : 39,
      "end_offset" : 46,
      "type" : "word",
      "position" : 6
    }
  ]
}

中文分詞器

  • smartCN

    一個簡單的中文或中英文混合文本的分詞?

    • 安裝 (重啟服務(wù)后使用)

      sh elasticsearch-plugin install analysis-smartcn 
      
    • eg:

      GET /_analyze
      {
        "analyzer": "smartcn", 
        "text": "有限公司"
      }
      
      {
        "tokens" : [
          {
            "token" : "有限公司",
            "start_offset" : 0,
            "end_offset" : 4,
            "type" : "word",
            "position" : 0
          }
        ]
      }
      
  • IK分詞器

    更智能更友好的中文分詞器

    • 下載 https://github.com/medcl/elasticsearch-analysis-ik/releases (版本要對應(yīng))

    • 安裝 解壓到es安裝目錄-plugins目錄

    • eg:

      GET /_analyze
      {
        "analyzer": "ik_max_word", 
        "text": "有限公司"
      }
      
      {
        "tokens" : [
          {
            "token" : "有限公司",
            "start_offset" : 0,
            "end_offset" : 4,
            "type" : "CN_WORD",
            "position" : 0
          },
          {
            "token" : "有限",
            "start_offset" : 0,
            "end_offset" : 2,
            "type" : "CN_WORD",
            "position" : 1
          },
          {
            "token" : "公司",
            "start_offset" : 2,
            "end_offset" : 4,
            "type" : "CN_WORD",
            "position" : 2
          }
        ]
      }
      

refresh

新的數(shù)據(jù)已添加到索引中??就能搜索到磨德,但是真實情況不是這樣的

  • 先添加?個?檔,再?刻搜索吆视,獲取不到新添加的數(shù)據(jù)

    curl -X PUT localhost:9200/stu/_doc/666 -H 'Content-Type:application/json' -d '{ "name": "王絲菲" }'
    curl -X GET localhost:9200/stu/_doc/_search?pretty
    
  • 強制刷新

    curl -X PUT localhost:9200/stu/_doc/667?refresh -H 'Content-Type:application/json' -d '{ "name": "王豆豆" }'
    curl -X GET localhost:9200/stu/_doc/_search?pretty
    
  • 修改默認(rèn)更新時間(默認(rèn)時間是1s)

    PUT localhost:9200/stu/_settings

    {
      "index": {
        "refresh_interval": "5s"
      }
    }
    
  • 將refresh關(guān)閉

    PUT localhost:9200/stu/_settings

    {
      "index": {
        "refresh_interval": "-1"
      }
    }
    

高亮查詢

  • 高亮查詢

    POST localhost:9200/stu/_search

    // 參數(shù)
    {
      "query": {
        "match": {
          "name": "趙"
        }
      },
      "highlight": {
        "fields": {
          "name": {}
        }
      }
    }
    
    // 相應(yīng)
    {
      "took" : 4,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 1,
          "relation" : "eq"
        },
        "max_score" : 2.4191523,
        "hits" : [
          {
            "_index" : "stu",
            "_type" : "_doc",
            "_id" : "10",
            "_score" : 2.4191523,
            "_source" : {
              "name" : "趙曦",
              "age" : 14,
              "sex" : "0",
              "class" : "7(2)班",
              "birthday" : "2005-09-02"
            },
            "highlight" : {
              "name" : [
                "<em>趙</em>曦"
              ]
            }
          }
        ]
      }
    }
    
  • 自定義高亮查詢

    POST localhost:9200/stu/_search

    // 參數(shù)
    {
      "query": {
        "match": {
          "name": "趙"
        }
      },
      "highlight": {
        "fields": {
          "name": {
            "pre_tags": ["<p>"],
            "post_tags": ["</p>"]
          }
        }
      }
    }
    
    // 響應(yīng)
    {
      "took" : 6,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 1,
          "relation" : "eq"
        },
        "max_score" : 2.4191523,
        "hits" : [
          {
            "_index" : "stu",
            "_type" : "_doc",
            "_id" : "10",
            "_score" : 2.4191523,
            "_source" : {
              "name" : "趙曦",
              "age" : 14,
              "sex" : "0",
              "class" : "7(2)班",
              "birthday" : "2005-09-02"
            },
            "highlight" : {
              "name" : [
                "<p>趙</p>曦"
              ]
            }
          }
        ]
      }
    }
    

查詢建議

查詢建議典挑,是為了給?戶提供更好的搜索體驗。包括:詞條檢查啦吧,?動補全

字段類型

類型 描述
text 指定搜索文本
field 獲取建議器的搜索字段
analyzer 指定分詞器
size 每個詞返回的最大建議詞數(shù)
sort 如何對建議詞進行排序您觉,可用選項:score-先按評分排序,再按文檔頻率排序授滓,term順序琳水;frequency:先按文檔頻率排序肆糕,再按評分排序,term順序炫刷;
suggest_mode 建議模式擎宝,控制提供建議詞的方式:missing-僅在搜索的詞項在索引中不存在時才提供建議詞,默認(rèn)值浑玛;popular-僅建議文檔頻率比搜索詞項高的詞绍申;always-總是提供匹配的建議詞;

suggester

  • Term suggester

    term 詞條建議器顾彰,對給輸?的文本進?分詞极阅,為每個分詞提供詞項建議

    POST localhost:9200/stu/_search

    // 參數(shù)
    {
      "suggest": {
        "MY_SUGGESTION": {
          "text": "7(6)班",
          "term": {
            "suggest_mode": "missing",
            "field": "class"
          }
        }
      }
    }
    
    {
      "took" : 105,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 0,
          "relation" : "eq"
        },
        "max_score" : null,
        "hits" : [ ]
      },
      "suggest" : {
        "MY_SUGGESTION" : [
          {
            "text" : "7(6)班",
            "offset" : 0,
            "length" : 5,
            "options" : [
              {
                "text" : "7(2)班",
                "score" : 0.8,
                "freq" : 4
              },
              {
                "text" : "7(3)班",
                "score" : 0.8,
                "freq" : 3
              },
              {
                "text" : "7(5)班",
                "score" : 0.8,
                "freq" : 3
              }
            ]
          }
        ]
      }
    }
    
  • Phrase suggester

    phrase 短語建議,在term的基礎(chǔ)上涨享,會考量多個term之間的關(guān)系筋搏,?如是否同時出現(xiàn)在索

    引的原文里,相鄰程度厕隧,以及詞頻等

    POST localhost:9200/stu/_search

    // 參數(shù)
    {
      "suggest": {
        "MY_SUGGESTION": {
          "text": "7(2) 班",
          "phrase": {
            "field": "class"
          }
        }
      }
    }
    
    // 響應(yīng)
    {
      "took" : 17,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 0,
          "relation" : "eq"
        },
        "max_score" : null,
        "hits" : [ ]
      },
      "suggest" : {
        "MY_SUGGESTION" : [
          {
            "text" : "7(2) 班",
            "offset" : 0,
            "length" : 6,
            "options" : [
              {
                "text" : "7(2)班",
                "score" : 0.4678218
              },
              {
                "text" : "7(3)班",
                "score" : 0.37474233
              },
              {
                "text" : "7(5)班",
                "score" : 0.37474233
              }
            ]
          }
        ]
      }
    }
    
  • Completion suggester

    完成建議奔脐,自動補充查詢內(nèi)容后面的內(nèi)容

    POST localhost:9200/stu/_search

    // 要查詢字段的類型必須是 completion
    {
      "suggest": {
        "MY_SUGGESTION": { // 自定義名稱
          "text": "I like",
          "completion": {
            "field": "selfDesc"
          }
        }
      }
    }
    
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市吁讨,隨后出現(xiàn)的幾起案子髓迎,更是在濱河造成了極大的恐慌,老刑警劉巖建丧,帶你破解...
    沈念sama閱讀 221,273評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件排龄,死亡現(xiàn)場離奇詭異,居然都是意外死亡翎朱,警方通過查閱死者的電腦和手機橄维,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,349評論 3 398
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來拴曲,“玉大人争舞,你說我怎么就攤上這事〕鹤疲” “怎么了竞川?”我有些...
    開封第一講書人閱讀 167,709評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長蕉汪。 經(jīng)常有香客問我流译,道長,這世上最難降的妖魔是什么者疤? 我笑而不...
    開封第一講書人閱讀 59,520評論 1 296
  • 正文 為了忘掉前任福澡,我火速辦了婚禮,結(jié)果婚禮上驹马,老公的妹妹穿的比我還像新娘革砸。我一直安慰自己除秀,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 68,515評論 6 397
  • 文/花漫 我一把揭開白布算利。 她就那樣靜靜地躺著册踩,像睡著了一般。 火紅的嫁衣襯著肌膚如雪效拭。 梳的紋絲不亂的頭發(fā)上暂吉,一...
    開封第一講書人閱讀 52,158評論 1 308
  • 那天,我揣著相機與錄音缎患,去河邊找鬼慕的。 笑死,一個胖子當(dāng)著我的面吹牛挤渔,可吹牛的內(nèi)容都是我干的肮街。 我是一名探鬼主播,決...
    沈念sama閱讀 40,755評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼判导,長吁一口氣:“原來是場噩夢啊……” “哼嫉父!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起眼刃,我...
    開封第一講書人閱讀 39,660評論 0 276
  • 序言:老撾萬榮一對情侶失蹤绕辖,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后鸟整,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體引镊,經(jīng)...
    沈念sama閱讀 46,203評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡朦蕴,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,287評論 3 340
  • 正文 我和宋清朗相戀三年篮条,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片吩抓。...
    茶點故事閱讀 40,427評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡涉茧,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出疹娶,到底是詐尸還是另有隱情伴栓,我是刑警寧澤,帶...
    沈念sama閱讀 36,122評論 5 349
  • 正文 年R本政府宣布雨饺,位于F島的核電站钳垮,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏额港。R本人自食惡果不足惜饺窿,卻給世界環(huán)境...
    茶點故事閱讀 41,801評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望移斩。 院中可真熱鬧肚医,春花似錦绢馍、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,272評論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至你稚,卻和暖如春瓷耙,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背刁赖。 一陣腳步聲響...
    開封第一講書人閱讀 33,393評論 1 272
  • 我被黑心中介騙來泰國打工哺徊, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人乾闰。 一個月前我還...
    沈念sama閱讀 48,808評論 3 376
  • 正文 我出身青樓落追,卻偏偏與公主長得像,于是被迫代替她去往敵國和親涯肩。 傳聞我的和親對象是個殘疾皇子轿钠,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,440評論 2 359

推薦閱讀更多精彩內(nèi)容