1. 程式人生 > >Elasticsearch淺嘗搜尋

Elasticsearch淺嘗搜尋

一、場景簡述

既然是說Elasticsearch全文搜尋引擎,那麼筆者就在Elasticsearch中的一些簡單的搜尋來入門Elasticsearch,借用官網的一句話“你知道的,為了搜尋...”

二、場景實現

當然!記得啟動Elasticsearch

其次在來看一張圖,理解什麼是ES叢集、索引、文件、屬性,方便後文的理解

1、插入資料

  • PUT:向ES中新增文件
  • myindex:索引名稱
  • mydoc:文件名稱
  • 1 2 3..:特定使用者的編號
PUT /myindex/mydoc/1
{
    "name":"qianran",
    "age":20,
    "work":"java",
    "about":"I love to go rock climbing"
}
PUT /myindex/mydoc/2
{
    "name":"linjie",
    "age":29,
    "work":"java",
    "about":"I want to climbing"
}
PUT /myindex/mydoc/3
{
    "name":"jay",
    "age":23,
    "work":"C++",
    "about":"happy.."
}
PUT /myindex/mydoc/4
{
    "name":"lucy",
    "age":22,
    "work":"java",
    "about":"xixi.."
}

若返回結果如下,即代表新增資料成功

{
   "_index": "myindex",
   "_type": "mydoc",
   "_id": "4",
   "_version": 1,
   "result": "created",
   "_shards": {
      "total": 2,
      "successful": 1,
      "failed": 0
   },
   "created": true
}

2、GET簡單搜尋

GET /myindex/mydoc/1

即可返回相應的文件

{
   "_index": "myindex",
   "_type": "mydoc",
   "_id": "1",
   "_version": 1,
   "found": true,
   "_source": {
      "name": "qianran",
      "age": 20,
      "work": "java",
      "about": "I love to go rock climbing"
   }
}

3、GET搜尋所有文件

GET /myindex/mydoc/_search

即可返回所有文件

{
   "took": 168,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 4,
      "max_score": 1,
      "hits": [
         {
            "_index": "myindex",
            "_type": "mydoc",
            "_id": "2",
            "_score": 1,
            "_source": {
               "name": "linjie",
               "age": 29,
               "work": "java",
               "about": "I want to climbing"
            }
         },
         {
            "_index": "myindex",
            "_type": "mydoc",
            "_id": "4",
            "_score": 1,
            "_source": {
               "name": "lucy",
               "age": 22,
               "work": "java",
               "about": "xixi.."
            }
         },
         {
            "_index": "myindex",
            "_type": "mydoc",
            "_id": "1",
            "_score": 1,
            "_source": {
               "name": "qianran",
               "age": 20,
               "work": "java",
               "about": "I love to go rock climbing"
            }
         },
         {
            "_index": "myindex",
            "_type": "mydoc",
            "_id": "3",
            "_score": 1,
            "_source": {
               "name": "jay",
               "age": 23,
               "work": "C++",
               "about": "happy.."
            }
         }
      ]
   }
}

4、GET條件搜尋

使用_search端點,並將查詢本身賦值給引數q

GET /myindex/mydoc/_search?q=work:java

即可返回work是java的文件

{
   "took": 61,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 3,
      "max_score": 0.2876821,
      "hits": [
         {
            "_index": "myindex",
            "_type": "mydoc",
            "_id": "1",
            "_score": 0.2876821,
            "_source": {
               "name": "qianran",
               "age": 20,
               "work": "java",
               "about": "I love to go rock climbing"
            }
         },
         {
            "_index": "myindex",
            "_type": "mydoc",
            "_id": "2",
            "_score": 0.18232156,
            "_source": {
               "name": "linjie",
               "age": 29,
               "work": "java",
               "about": "I want to climbing"
            }
         },
         {
            "_index": "myindex",
            "_type": "mydoc",
            "_id": "4",
            "_score": 0.18232156,
            "_source": {
               "name": "lucy",
               "age": 22,
               "work": "java",
               "about": "xixi.."
            }
         }
      ]
   }
}

5、GET表示式搜尋

它支援構建更加複雜和健壯的查詢

GET /myindex/mydoc/_search
{
    "query": {
        "match": {
            "work":"java"
        }
    }
}

即可返回work是java的文件

{
   "took": 17,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 3,
      "max_score": 0.2876821,
      "hits": [
         {
            "_index": "myindex",
            "_type": "mydoc",
            "_id": "1",
            "_score": 0.2876821,
            "_source": {
               "name": "qianran",
               "age": 20,
               "work": "java",
               "about": "I love to go rock climbing"
            }
         },
         {
            "_index": "myindex",
            "_type": "mydoc",
            "_id": "2",
            "_score": 0.18232156,
            "_source": {
               "name": "linjie",
               "age": 29,
               "work": "java",
               "about": "I want to climbing"
            }
         },
         {
            "_index": "myindex",
            "_type": "mydoc",
            "_id": "4",
            "_score": 0.18232156,
            "_source": {
               "name": "lucy",
               "age": 22,
               "work": "java",
               "about": "xixi.."
            }
         }
      ]
   }
}

6、GET表示式搜尋多條件

work:java       age大於25

gt:_great than 表示大於

當然還有許多,如下:

eq相等   ne、neq不相等,   gt大於, lt小於 gte、ge大於等於   lte、le 小於等於   not非   mod求模   is [not] div by是否能被某數整除   is [not] even是否為偶數   is [not] even by $b即($a / $b) % 2 == 0   is [not] odd是否為奇   is not odd by $b即($a / $b) % 2 != 0

GET /myindex/mydoc/_search
{
    "query" : {
        "bool": {
            "must": {
                "match" : {
                    "work" : "java" 
                }
            },
            "filter": {
                "range" : {
                    "age" : { "gt" : 25 } 
                }
            }
        }
    }
}

即可返回age大於25,並且work是java的文件

{
   "took": 70,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 0.18232156,
      "hits": [
         {
            "_index": "myindex",
            "_type": "mydoc",
            "_id": "2",
            "_score": 0.18232156,
            "_source": {
               "name": "linjie",
               "age": 29,
               "work": "java",
               "about": "I want to climbing"
            }
         }
      ]
   }
}

7、GET全文搜尋

在全文中搜索某句,得到文件結果 結果會有_score屬性,即相關性得分,得到分數越高,匹配程度越高

GET /myindex/mydoc/_search
{
    "query": {
        "match": {
            "about":"climbing"
        }
    }
}

即可返回about屬性中有climbing的文件,並且有_score屬性來顯示相關性得分

{
   "took": 23,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 2,
      "max_score": 0.5565415,
      "hits": [
         {
            "_index": "myindex",
            "_type": "mydoc",
            "_id": "2",
            "_score": 0.5565415,
            "_source": {
               "name": "linjie",
               "age": 29,
               "work": "java",
               "about": "I want to climbing"
            }
         },
         {
            "_index": "myindex",
            "_type": "mydoc",
            "_id": "1",
            "_score": 0.26742277,
            "_source": {
               "name": "qianran",
               "age": 20,
               "work": "java",
               "about": "I love to go rock climbing"
            }
         }
      ]
   }
}

8、GET短語搜尋

精確搜尋某短語,需要將match改成match_phrase

GET /myindex/mydoc/_search
{
    "query": {
        "match_phrase": {
            "about":"I want"
        }
    }
}

即可返回有about中有I want,如果是全文搜尋,只要有I的也會被搜尋出來,這就是短語搜尋和全文搜素的區別

{
   "took": 28,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 1.113083,
      "hits": [
         {
            "_index": "myindex",
            "_type": "mydoc",
            "_id": "2",
            "_score": 1.113083,
            "_source": {
               "name": "linjie",
               "age": 29,
               "work": "java",
               "about": "I want to climbing"
            }
         }
      ]
   }
}

9、GET高亮搜尋

讓使用者知道為何該文件符合查詢結果

GET /myindex/mydoc/_search
{
    "query": {
        "match": {
            "about":"I want"
        }
    },
    "highlight": {
        "fields": {
            "about":{}
        }
    }
}

即可返回有I  或者有want 的文件,並且通過<em>標籤進行高亮

{
   "took": 56,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 2,
      "max_score": 1.113083,
      "hits": [
         {
            "_index": "myindex",
            "_type": "mydoc",
            "_id": "2",
            "_score": 1.113083,
            "_source": {
               "name": "linjie",
               "age": 29,
               "work": "java",
               "about": "I want to climbing"
            },
            "highlight": {
               "about": [
                  "<em>I</em> <em>want</em> to climbing"
               ]
            }
         },
         {
            "_index": "myindex",
            "_type": "mydoc",
            "_id": "1",
            "_score": 0.26742277,
            "_source": {
               "name": "qianran",
               "age": 20,
               "work": "java",
               "about": "I love to go rock climbing"
            },
            "highlight": {
               "about": [
                  "<em>I</em> love to go rock climbing"
               ]
            }
         }
      ]
   }
}

以上就是ES的簡單搜尋