1. 程式人生 > >ElasticSearch 6.x 學習筆記:15.檢索入門

ElasticSearch 6.x 學習筆記:15.檢索入門

15.1 準備資料

[es@node1 ~]$ vi  website.json 
[es@node1 ~]$ cat website.json
{ "index":{ "_index": "website", "_type": "blog", "_id": "1" }}
{ "title": "Ambari原始碼編譯","author":"程裕強","postdate":"2016-12-21","abstract":"CentOS7.x下的Ambari2.4原始碼編譯","url":"http://url.cn/53788351"}
{ "index":{ "_index": "website"
, "_type": "blog", "_id": "2" }} { "title": "watchman原始碼編譯","author":"程裕強","postdate":"2016-12-23","abstract":"CentOS7.x的watchman原始碼編譯","url":"http://url.cn/53844169"} { "index":{ "_index": "website", "_type": "blog", "_id": "3" }} { "title": "CentOS升級gcc","author":"程裕強","postdate":"2016-12-25","abstract"
:"CentOS升級gcc","url":"http://url.cn/53868915"} { "index":{ "_index": "website", "_type": "blog", "_id": "4" }} { "title": "vmware複製虛擬機器","author":"程裕強","postdate":"2016-12-29","abstract":"vmware複製虛擬機器","url":"http://url.cn/53946664"} { "index":{ "_index": "website", "_type": "blog", "_id": "5" }} { "title"
: "libstdc++.so.6","author":"程裕強","postdate":"2016-12-30","abstract":"libstdc++.so.6問題解決","url":"http://url.cn/53946911"} { "index":{ "_index": "website", "_type": "blog", "_id": "6" }} { "title": "CentOS更換國內yum源","author":"程裕強","postdate":"2016-12-30","abstract":"CentOS更換國內yum源","url":"http://url.cn/53946911"} { "index":{ "_index": "website", "_type": "blog", "_id": "7" }} { "title": "搭建Ember開發環境","author":"程裕強","postdate":"2016-12-30","abstract":"CentOS下搭建Ember開發環境","url":"http://url.cn/53947507"} { "index":{ "_index": "website", "_type": "blog", "_id": "8" }} { "title": "es高亮","author":"程裕強","postdate":"2017-01-03","abstract":"Elasticsearch查詢關鍵字高亮","url":"http://url/53991802"} { "index":{ "_index": "website", "_type": "blog", "_id": "9" }} { "title": "to be or not to be","author":"somebody","postdate":"2018-01-03","abstract":"to be or not to be,that is the question","url":"http://url/63991802"} [es@node1 ~]$

15.2 建立索引

PUT website
{
  "settings": {
    "number_of_replicas": 1,
    "number_of_shards": 5
  },
  "mappings": {
    "blog":{
      "properties": {
        "title":{
          "type":"text",
          "analyzer": "ik_max_word"
        },
        "author":{
          "type":"text"
        },
        "postdate":{
          "type":"date",
          "format": "yyyy-MM-dd"
        },
        "abstract":{
          "type":"text",
          "analyzer": "ik_max_word"
        },
        "url":{
          "type":"text"
        }
      }
    }  
  }
}

15.3 批量匯入

[es@node1 ~]$ curl -XPOST "http://node1:9200/_bulk?pretty" -H "Content-Type: application/json;charset=UTF-8" --data-binary @website.json
{
  "took" : 28,
  "errors" : false,
  "items" : [
    {
      "index" : {
        "_index" : "website",
        "_type" : "blog",
        "_id" : "1",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 0,
        "_primary_term" : 1,
        "status" : 201
      }
    },
    {
      "index" : {
        "_index" : "website",
        "_type" : "blog",
        "_id" : "2",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 0,
        "_primary_term" : 1,
        "status" : 201
      }
    },
    {
      "index" : {
        "_index" : "website",
        "_type" : "blog",
        "_id" : "3",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 0,
        "_primary_term" : 1,
        "status" : 201
      }
    },
    {
      "index" : {
        "_index" : "website",
        "_type" : "blog",
        "_id" : "4",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 1,
        "_primary_term" : 1,
        "status" : 201
      }
    },
    {
      "index" : {
        "_index" : "website",
        "_type" : "blog",
        "_id" : "5",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 0,
        "_primary_term" : 1,
        "status" : 201
      }
    },
    {
      "index" : {
        "_index" : "website",
        "_type" : "blog",
        "_id" : "6",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 2,
        "_primary_term" : 1,
        "status" : 201
      }
    },
    {
      "index" : {
        "_index" : "website",
        "_type" : "blog",
        "_id" : "7",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 1,
        "_primary_term" : 1,
        "status" : 201
      }
    },
    {
      "index" : {
        "_index" : "website",
        "_type" : "blog",
        "_id" : "8",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 1,
        "_primary_term" : 1,
        "status" : 201
      }
    },
    {
      "index" : {
        "_index" : "website",
        "_type" : "blog",
        "_id" : "9",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 2,
        "_primary_term" : 1,
        "status" : 201
      }
    }
  ]
}
[es@node1 ~]$ 

15.4 term查詢

GET website/_search
{
  "query": {
    "term": {
        "title": "vmware"
    }
  }
}
{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.9227539,
    "hits": [
      {
        "_index": "website",
        "_type": "blog",
        "_id": "4",
        "_score": 0.9227539,
        "_source": {
          "title": "vmware複製虛擬機器",
          "author": "程裕強",
          "postdate": "2016-12-29",
          "abstract": "vmware複製虛擬機器",
          "url": "http://url/53946664"
        }
      }
    ]
  }
}

15.5 分頁

GET website/_search
{
  "from":0,
  "size":3,
  "query": {
    "match_all": {}
  }
}
{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 8,
    "max_score": 1,
    "hits": [
      {
        "_index": "website",
        "_type": "blog",
        "_id": "5",
        "_score": 1,
        "_source": {
          "title": "libstdc++.so.6",
          "author": "程裕強",
          "postdate": "2016-12-30",
          "abstract": "libstdc++.so.6問題解決",
          "url": "http://url/53946911"
        }
      },
      {
        "_index": "website",
        "_type": "blog",
        "_id": "8",
        "_score": 1,
        "_source": {
          "title": "es高亮",
          "author": "程裕強",
          "postdate": "2017-01-03",
          "abstract": "Elasticsearch查詢關鍵字高亮",
          "url": "http://url/53991802"
        }
      },
      {
        "_index": "website",
        "_type": "blog",
        "_id": "2",
        "_score": 1,
        "_source": {
          "title": "watchman原始碼編譯",
          "author": "程裕強",
          "postdate": "2016-12-23",
          "abstract": "CentOS7.x的watchman原始碼編譯",
          "url": "http://url.cn/53844169"
        }
      }
    ]
  }
}

15.6 過濾欄位

GET website/_search
{
  "_source": ["title","author"], 
  "query": {
    "term": {
        "title": "centos"
    }
  }
}
{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.9227539,
    "hits": [
      {
        "_index": "website",
        "_type": "blog",
        "_id": "6",
        "_score": 0.9227539,
        "_source": {
          "author": "程裕強",
          "title": "CentOS更換國內yum源"
        }
      },
      {
        "_index": "website",
        "_type": "blog",
        "_id": "3",
        "_score": 0.2876821,
        "_source": {
          "author": "程裕強",
          "title": "CentOS升級gcc"
        }
      }
    ]
  }
}

15.7 顯示version

GET website/_search
{
  "_source": ["title"], 
  "version": true, 
  "query": {
    "term": {
        "title": "centos"
    }
  }
}
{
  "took": 5,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.9227539,
    "hits": [
      {
        "_index": "website",
        "_type": "blog",
        "_id": "6",
        "_version": 1,
        "_score": 0.9227539,
        "_source": {
          "title": "CentOS更換國內yum源"
        }
      },
      {
        "_index": "website",
        "_type": "blog",
        "_id": "3",
        "_version": 1,
        "_score": 0.2876821,
        "_source": {
          "title": "CentOS升級gcc"
        }
      }
    ]
  }
}

15.8 評分過濾

GET website/_search
{
  "min_score":"0.5",
  "query": {
    "term": {
        "title": "centos"
    }
  }
}
{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.9227539,
    "hits": [
      {
        "_index": "website",
        "_type": "blog",
        "_id": "6",
        "_score": 0.9227539,
        "_source": {
          "title": "CentOS更換國內yum源",
          "author": "程裕強",
          "postdate": "2016-12-30",
          "abstract": "CentOS更換國內yum源",
          "url": "http://url/53946911"
        }
      }
    ]
  }
}

15.9 高亮關鍵字

GET website/_search
{
  "query": {
    "term": {
        "title": "centos"
    }
  },
  "highlight": {
    "fields": {
      "title": {}
    }
  }
}
{
  "took": 6,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.9227539,
    "hits": [
      {
        "_index": "website",
        "_type": "blog",
        "_id": "6",
        "_score": 0.9227539,
        "_source": {
          "title": "CentOS更換國內yum源",
          "author": "程裕強",
          "postdate": "2016-12-30",
          "abstract": "CentOS更換國內yum源",
          "url": "http://url/53946911"
        },
        "highlight": {
          "title": [
            "<em>CentOS</em>更換國內yum源"
          ]
        }
      },
      {
        "_index": "website",
        "_type": "blog",
        "_id": "3",
        "_score": 0.2876821,
        "_source": {
          "title": "CentOS升級gcc",
          "author": "程裕強",
          "postdate": "2016-12-25",
          "abstract": "CentOS升級gcc",
          "url": "http://url/53868915"
        },
        "highlight": {
          "title": [
            "<em>CentOS</em>升級gcc"
          ]
        }
      }
    ]
  }
}

相關推薦

ElasticSearch 6.x 學習筆記15.檢索入門

15.1 準備資料 [es@node1 ~]$ vi website.json [es@node1 ~]$ cat website.json { "index":{ "_index": "website", "_type": "blog", "_id"

ElasticSearch 6.x 學習筆記12.欄位型別

12.1 欄位型別概述 一級分類 二級分類 具體型別 核心型別 字串型別 string,text,keyword 整數型別 integer,long,

ElasticSearch 6.x 學習筆記6.索引

6.1 建立索引 (1)簡單方式 PUT test { "acknowledged": true, "shards_acknowledged": true, "index": "t

ElasticSearch 6.x 學習筆記2.head外掛

2.1 Head外掛簡介 ElasticSearch-head是一個H5編寫的ElasticSearch叢集操作和管理工具,可以對叢集進行傻瓜式操作。 顯示叢集的拓撲,並且能夠執行索引和節點級別操作 搜尋介面能夠查詢叢集中原始json或表格格式的檢索資料

ElasticSearch 6.x 學習筆記31.Java API之詞項查詢

1、term查詢 Find documents which contain the exact term specified in the field specified. package cn.hadron; import cn.hadron.

ElasticSearch 6.x 學習筆記7.文件

7.1 新建文件 index/type/id1 (1)一般格式 PUT blog/csdn/1 { "id":1, "title":"Elastic

ElasticSearch 6.x 學習筆記20.搜尋排序

20.1 按照文件新增順序排序 GET website/_search GET website/_search { "query": { "match_all": {} } }

Spark2.x學習筆記6、在Windows平臺下搭建Spark開發環境(Intellij IDEA)

Spark2.x學習筆記 6、 在Windows平臺下搭建Spark開發環境(Intellij IDEA+Maven) 6.1 整合開發環境IDE 為了方便應用程式開發與測試,提高開發效率,一般使用整合開發工具IDE。同樣,為了方便Spark應用程式編

LogStash官方文件6.x學習筆記---------day1

上個星期之前看過一段時間,對logstash基礎有一定了解。現在來好好記錄一下我的學習的東西。 logstash的工作流程:    由input--->filter---->output構成。 執行模型: Logstash事件處理管道協調input--

Spark2.x學習筆記3、 Spark核心概念RDD

Spark學習筆記:3、Spark核心概念RDD 3.1 RDD概念 彈性分散式資料集(Resilient Distributed Datasets,RDD) ,可以分三個層次來理解: 資料集:故名思議,RDD 是資料集合的抽象,是複雜物理介質上存在資料的一種邏輯檢視。

Spark2.x學習筆記2、Scala簡單例子

2、 Scala簡單例子2.1 互動式程式設計spark-shell是Spark互動式執行模式,提供了互動式程式設計,邊敲程式碼邊執行,不需要建立程式原始檔,方便除錯程式,有利於快速學習Spark。[[email protected] spark-2.2.0]# bin/spark-shell U

Spark2.x學習筆記14、Spark SQL程式設計

Spark2.x學習筆記:14、 Spark SQL程式設計 14.1 RDD的侷限性 RDD僅表示資料集,RDD沒有元資料,也就是說沒有欄位語義定義。 RDD需要使用者自己優化程式,對程式設計師要求較高。 從不同資料來源讀取資料相對困難。 合併多個數

Cocos2d-x 3.x學習筆記猩先生帶你打飛機(四)遊戲場景:背景與我機的建立、敵機的建立、物理世界構建

一、背景與我機的建立 現在我們要建立新的一個場景了。選擇開始遊戲即從選單場景跳到遊戲場景。現在先完善HelloWorldScene的程式碼 找到我們開始遊戲的回撥方法,新增程式碼: //開始遊戲 void HelloWorld::menuStartCa

ES[7.6.x]學習筆記(七)IK中文分詞器

在上一節中,我們給大家介紹了ES的分析器,我相信大家對ES的全文搜尋已經有了深刻的印象。分析器包含3個部分:字元過濾器、分詞器、分詞過濾器。在上一節的例子,大家發現了,都是英文的例子,是吧?因為ES是外國人寫的嘛,中國如果要在這方面趕上來,還是需要螢幕前的小夥伴們的~ 英文呢,我們可以按照空格將一句話、一

ES[7.6.x]學習筆記(八)資料的增刪改

在前面幾節的內容中,我們學習索引、欄位對映、分析器等,這些都是使用ES的基礎,就像在資料庫中建立表一樣,基礎工作做好以後,我們就要真正的使用它了,這一節我們要看看怎麼向索引裡寫入資料、修改資料、刪除資料,至於搜尋嘛,因為ES的主要功能就是搜尋,所以搜尋的相關功能我們後面會展開講。 ## Document的建

ES[7.6.x]學習筆記(九)搜尋

搜尋是ES最最核心的內容,沒有之一。前面章節的內容,索引、動態對映、分詞器等都是鋪墊,最重要的就是最後點選搜尋這一下。下面我們就看看點選搜尋這一下的背後,都做了哪些事情。 ## 分數(score) ES的搜尋結果是按照相關分數的高低進行排序的,咦?! 怎麼沒說搜尋先說搜尋結果的排序了?咱們這裡先把這個概念

ES[7.6.x]學習筆記(十一)與SpringBoot結合

在前面的章節中,我們把ES的基本功能都給大家介紹完了,從ES的搭建、建立索引、分詞器、到資料的查詢,大家發現,我們都是通過ES的API去進行呼叫,那麼,我們在專案當中怎麼去使用ES呢?這一節,我們就看看ES如何與我們的SpringBoot專案結合。 ## 版本依賴 SpringBoot預設是有Elasti

ES[7.6.x]學習筆記(十二)高亮 和 搜尋建議

ES當中大部分的內容都已經學習完了,今天呢算是對前面內容的查漏補缺,把ES中非常實用的功能整理一下,在以後的專案開發中,這些功能肯定是對你的專案加分的,我們來看看吧。 ## 高亮 高亮在搜尋功能中是十分重要的,我們希望搜尋的內容在搜尋結果中重點突出,讓使用者聚焦在搜尋的內容上。我們看看在ES當中是怎麼實現

Keras學習筆記Chapter1-Keras入門

Keras Keras是一個高層神經網路庫。Keras是由Python編寫而成的,後端實現是基於Tensorflow,CNTK或Theano(Keras是一個庫,可以理解為一個基於多種不同機器學習庫提供相同api的庫)。 Keras發展的要義是:將想

Python全棧學習筆記day 15內建函式

全部內建函式列表:     Built-in Functions     abs() dict() help() min()