天天看點

ElasticSearch學習筆記 | Match和Match_phrase比對搜尋

本文測試資料為官方提供的測試資料,導入方法在學習筆記本章節第一篇中

一、基本類型(非​​字元串​​​),精确比對

查詢 account_number 是 20 的所有結果:

GET bank/_search

{

  "query": {

    "match": {

      "account_number": 20

    }

  }

}

傳回内容:

{

  "took" : 6,

  "timed_out" : false,

  "_shards" : {

    "total" : 1,

    "successful" : 1,

    "skipped" : 0,

    "failed" : 0

  },

  "hits" : {

    "total" : {

      "value" : 1,

      "relation" : "eq"

    },

    "max_score" : 1.0,

    "hits" : [

      {

        "_index" : "bank",

        "_type" : "account",

        "_id" : "20",

        "_score" : 1.0,

        "_source" : {

          "account_number" : 20,

          "balance" : 16418,

          "firstname" : "Elinor",

          "lastname" : "Ratliff",

          "age" : 36,

          "gender" : "M",

          "address" : "282 Kings Place",

          "employer" : "Scentric",

          "email" : "[email protected]",

          "city" : "Ribera",

          "state" : "WA"

        }

      }

    ]

  }

}

二、字元串模糊比對查詢

比如我們希望查詢所有 address 中包含 Kings 的資料:

GET bank/_search

{

  "query": {

    "match": {

      "address": "Kings"

    }

  }

}

傳回結果:

可以看到 “282 Kings Place” 和 “305 Kings Hwy” 兩條記錄都傳回了

全文檢索會按照評分進行排序,會對檢索條件進行分詞比對。

{

  "took" : 157,

  "timed_out" : false,

  "_shards" : {

    "total" : 1,

    "successful" : 1,

    "skipped" : 0,

    "failed" : 0

  },

  "hits" : {

    "total" : {

      "value" : 2,

      "relation" : "eq"

    },

    "max_score" : 5.990829,

    "hits" : [

      {

        "_index" : "bank",

        "_type" : "account",

        "_id" : "20",

        "_score" : 5.990829,

        "_source" : {

          "account_number" : 20,

          "balance" : 16418,

          "firstname" : "Elinor",

          "lastname" : "Ratliff",

          "age" : 36,

          "gender" : "M",

          "address" : "282 Kings Place",

          "employer" : "Scentric",

          "email" : "[email protected]",

          "city" : "Ribera",

          "state" : "WA"

        }

      },

      {

        "_index" : "bank",

        "_type" : "account",

        "_id" : "722",

        "_score" : 5.990829,

        "_source" : {

          "account_number" : 722,

          "balance" : 27256,

          "firstname" : "Roberts",

          "lastname" : "Beasley",

          "age" : 34,

          "gender" : "F",

          "address" : "305 Kings Hwy",

          "employer" : "Quintity",

          "email" : "[email protected]",

          "city" : "Hayden",

          "state" : "PA"

        }

      }

    ]

  }

}

三、Match_phrase 短語比對

預設的match搜尋會對搜尋内容進行分詞,比如:mill lane 會分成 mill 和 lane 之後搜尋的結果可能包含僅有其中一項的結果,但是此類結果分數較低。

如果不希望被分詞可以使用 match_phrase 進行搜尋

例子:

查詢 位址 包含 mill lane 的結果:

繼續閱讀