穀粒商城學習——P112-114、117 es進階-match全文檢索、match_phra短語匹配、multi_match多字段匹配,term、keyword

match匹配查詢

  基本類型(非字符串),會進行精確匹配。

  字符串,會進行全文檢索,還會進行分詞

 

基本類型(非字符串),match返回account_number=20的

GET bank/_search
{
  "query": {
    "match": {
      "account_number": "20"
    }
  }
}

字符串,會按照評分進行排序,並會對檢索條件進行分詞匹配,記錄中包含Holmes 和Lane的都會返回

GET bank/_search
{
  "query": {
    "match": {
      "address": "Holmes Lane"
    }
  }
}

全文檢索返回結果

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 16,
      "relation" : "eq"
    },
    "max_score" : 10.605789,
    "hits" : [
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "1",
        "_score" : 10.605789,
        "_source" : {
          "account_number" : 1,
          "balance" : 39225,
          "firstname" : "Amber",
          "lastname" : "Duke",
          "age" : 32,
          "gender" : "M",
          "address" : "880 Holmes Lane",
          "employer" : "Pyrami",
          "email" : "[email protected]",
          "city" : "Brogan",
          "state" : "IL"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "70",
        "_score" : 4.1042743,
        "_source" : {
          "account_number" : 70,
          "balance" : 38172,
          "firstname" : "Deidre",
          "lastname" : "Thompson",
          "age" : 33,
          "gender" : "F",
          "address" : "685 School Lane",
          "employer" : "Netplode",
          "email" : "[email protected]",
          "city" : "Chestnut",
          "state" : "GA"
        }
      },
     ……
    ]
  }
}

 如果不想分詞,可使用match_phrase語法,即將match替換成match_phrase即可

GET bank/_search
{
  "query": {
    "match_phrase": {
      "address": "Holmes Lane"
    }
  }
}

全部返回:

{
  "took" : 9,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 10.605789,
    "hits" : [
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "1",
        "_score" : 10.605789,
        "_source" : {
          "account_number" : 1,
          "balance" : 39225,
          "firstname" : "Amber",
          "lastname" : "Duke",
          "age" : 32,
          "gender" : "M",
          "address" : "880 Holmes Lane",
          "employer" : "Pyrami",
          "email" : "[email protected]",
          "city" : "Brogan",
          "state" : "IL"
        }
      }
    ]
  }
}

 

多字段匹配

在字段address或city包含的mill Movico

GET bank/_search
{
  "query": {
    "multi_match": {
      "query": "mill Movico", 
     "fields": ["address","city"]
    }
  }
}

返回結果:

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : 6.505949,
    "hits" : [
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "472",
        "_score" : 6.505949,
        "_source" : {
          "account_number" : 472,
          "balance" : 25571,
          "firstname" : "Lee",
          "lastname" : "Long",
          "age" : 32,
          "gender" : "F",
          "address" : "288 Mill Street",
          "employer" : "Comverges",
          "email" : "[email protected]",
          "city" : "Movico",
          "state" : "MT"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "970",
        "_score" : 5.4032025,
        "_source" : {
          "account_number" : 970,
          "balance" : 19648,
          "firstname" : "Forbes",
          "lastname" : "Wallace",
          "age" : 28,
          "gender" : "M",
          "address" : "990 Mill Road",
          "employer" : "Pheast",
          "email" : "[email protected]",
          "city" : "Lopezo",
          "state" : "AK"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "136",
        "_score" : 5.4032025,
        "_source" : {
          "account_number" : 136,
          "balance" : 45801,
          "firstname" : "Winnie",
          "lastname" : "Holland",
          "age" : 38,
          "gender" : "M",
          "address" : "198 Mill Lane",
          "employer" : "Neteria",
          "email" : "[email protected]",
          "city" : "Urie",
          "state" : "IL"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "345",
        "_score" : 5.4032025,
        "_source" : {
          "account_number" : 345,
          "balance" : 9812,
          "firstname" : "Parker",
          "lastname" : "Hines",
          "age" : 38,
          "gender" : "M",
          "address" : "715 Mill Avenue",
          "employer" : "Baluba",
          "email" : "[email protected]",
          "city" : "Blackgum",
          "state" : "KY"
        }
      }
    ]
  }
}

從返回結果來看,多字段匹配時仍會分詞

 

term:和match一樣。匹配某個屬性的值,如果用term修飾text,會查不出來數據

  全文檢索字段用match,

  其他非text字段匹配用term。

GET bank/_search
{
  "query": {
    "term": {
      "balance":39225
    }
  }
}

keyword:也是精確匹配,和match_phrase的區別是,match_phrase的功能相當於java的String.contains,.keyword相當於java的String.equals

GET bank/_search
{
  "query": {
    "match": {
      "address.keyword": "Holmes Lane"##address必須完全等於Holmes Lane
    }
  }
}

結果:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章