實踐006-elasticsearch查詢之1-URI Search查詢

niewj發表於2022-05-05

[toc]


實踐006-elasticsearch查詢之1-URI Search查詢

查詢基於movielens資料索引。

一、URI Search詳解

1.1. URI Search介紹

GET movies/_search?q=2012&df=title&sort=year:desc&from=0&size=10&timeout=1s
{
  "profile": "true"
}
  • q: 指定查詢語句。使用 Query String Syntax
  • df(default field): 預設欄位,不指定時,會對所有欄位進行查詢。
  • sort: 排序
  • from/size: 用於分頁
  • profile: 可以檢視查詢是如何被執行的

結果:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "105254",
        "_score" : null,
        "_source" : {
          "id" : "105254",
          "genre" : [
            "Adventure",
            "Comedy"
          ],
          "title" : "Crystal Fairy & the Magical Cactus and 2012",
          "@version" : "1",
          "year" : 2013
        },
        "sort" : [
          2013
        ]
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "72378",
        "_score" : null,
        "_source" : {
          "id" : "72378",
          "genre" : [
            "Action",
            "Drama",
            "Sci-Fi",
            "Thriller"
          ],
          "title" : "2012",
          "@version" : "1",
          "year" : 2009
        },
        "sort" : [
          2009
        ]
      }
    ]
  },
  "profile" : {
    "shards" : [
      {
        "id" : "[q3LSyHNoTSeAj6pqYovXjg][movies][0]",
        "searches" : [
          {
            "query" : [
              {
                "type" : "TermQuery",
                "description" : "title:2012",
                "time_in_nanos" : 46388,
                "breakdown" : {
                  "set_min_competitive_score_count" : 0,
                  "match_count" : 0,
                  "shallow_advance_count" : 0,
                  "set_min_competitive_score" : 0,
                  "next_doc" : 1112,
                  "match" : 0,
                  "next_doc_count" : 2,
                  "score_count" : 0,
                  "compute_max_score_count" : 0,
                  "compute_max_score" : 0,
                  "advance" : 954,
                  "advance_count" : 1,
                  "score" : 0,
                  "build_scorer_count" : 4,
                  "create_weight" : 7640,
                  "shallow_advance" : 0,
                  "create_weight_count" : 1,
                  "build_scorer" : 36674
                }
              }
            ],
            "rewrite_time" : 9815,
            "collector" : [
              {
                "name" : "CancellableCollector",
                "reason" : "search_cancelled",
                "time_in_nanos" : 151352,
                "children" : [
                  {
                    "name" : "SimpleFieldCollector",
                    "reason" : "search_top_hits",
                    "time_in_nanos" : 141275
                  }
                ]
              }
            ]
          }
        ],
        "aggregations" : [ ]
      }
    ]
  }
}

1.2. 欄位查詢:查詢title裡有2012的記錄

GET movies/_search?q=2012&df=title
{
  "profile": "true"
}

結果

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 11.303033,
    "hits" : [
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "72378",
        "_score" : 11.303033,
        "_source" : {
          "id" : "72378",
          "genre" : [
            "Action",
            "Drama",
            "Sci-Fi",
            "Thriller"
          ],
          "title" : "2012",
          "@version" : "1",
          "year" : 2009
        }
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "105254",
        "_score" : 5.2497,
        "_source" : {
          "id" : "105254",
          "genre" : [
            "Adventure",
            "Comedy"
          ],
          "title" : "Crystal Fairy & the Magical Cactus and 2012",
          "@version" : "1",
          "year" : 2013
        }
      }
    ]
  },
  "profile" : {
    "shards" : [
      {
        "id" : "[q3LSyHNoTSeAj6pqYovXjg][movies][0]",
        "searches" : [
          {
            "query" : [
              {
                "type" : "TermQuery",
                "description" : "title:2012",
                "time_in_nanos" : 86723,
                "breakdown" : {
                  "set_min_competitive_score_count" : 0,
                  "match_count" : 0,
                  "shallow_advance_count" : 0,
                  "set_min_competitive_score" : 0,
                  "next_doc" : 1996,
                  "match" : 0,
                  "next_doc_count" : 2,
                  "score_count" : 2,
                  "compute_max_score_count" : 0,
                  "compute_max_score" : 0,
                  "advance" : 1666,
                  "advance_count" : 1,
                  "score" : 4088,
                  "build_scorer_count" : 4,
                  "create_weight" : 59362,
                  "shallow_advance" : 0,
                  "create_weight_count" : 1,
                  "build_scorer" : 19601
                }
              }
            ],
            "rewrite_time" : 15506,
            "collector" : [
              {
                "name" : "CancellableCollector",
                "reason" : "search_cancelled",
                "time_in_nanos" : 23385,
                "children" : [
                  {
                    "name" : "SimpleTopScoreDocCollector",
                    "reason" : "search_top_hits",
                    "time_in_nanos" : 14883
                  }
                ]
              }
            ]
          }
        ],
        "aggregations" : [ ]
      }
    ]
  }
}

1.3. 泛查詢:查詢2012

GET movies/_search?q=2012
{
  "profile": "true"
}

泛查詢,不指定欄位,則會查詢所有欄位:

{
  "took" : 26,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 219,
      "relation" : "eq"
    },
    "max_score" : 11.303033,
    "hits" : [
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "72378",
        "_score" : 11.303033,
        "_source" : {
          "id" : "72378",
          "genre" : [
            "Action",
            "Drama",
            "Sci-Fi",
            "Thriller"
          ],
          "title" : "2012",
          "@version" : "1",
          "year" : 2009
        }
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "2012",
        "_score" : 8.778942,
        "_source" : {
          "id" : "2012",
          "genre" : [
            "Adventure",
            "Comedy",
            "Sci-Fi",
            "Western"
          ],
          "title" : "Back to the Future Part III",
          "@version" : "1",
          "year" : 1990
        }
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "105254",
        "_score" : 5.2497,
        "_source" : {
          "id" : "105254",
          "genre" : [
            "Adventure",
            "Comedy"
          ],
          "title" : "Crystal Fairy & the Magical Cactus and 2012",
          "@version" : "1",
          "year" : 2013
        }
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "89745",
        "_score" : 1.0,
        "_source" : {
          "id" : "89745",
          "genre" : [
            "Action",
            "Adventure",
            "Sci-Fi",
            "IMAX"
          ],
          "title" : "Avengers, The",
          "@version" : "1",
          "year" : 2012
        }
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "91483",
        "_score" : 1.0,
        "_source" : {
          "id" : "91483",
          "genre" : [
            "Action",
            "Crime",
            "Film-Noir"
          ],
          "title" : "Bullet to the Head",
          "@version" : "1",
          "year" : 2012
        }
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "91485",
        "_score" : 1.0,
        "_source" : {
          "id" : "91485",
          "genre" : [
            "Action",
            "Adventure"
          ],
          "title" : "Expendables 2, The",
          "@version" : "1",
          "year" : 2012
        }
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "91500",
        "_score" : 1.0,
        "_source" : {
          "id" : "91500",
          "genre" : [
            "Action",
            "Adventure",
            "Drama",
            "Sci-Fi",
            "Thriller"
          ],
          "title" : "The Hunger Games",
          "@version" : "1",
          "year" : 2012
        }
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "91529",
        "_score" : 1.0,
        "_source" : {
          "id" : "91529",
          "genre" : [
            "Action",
            "Adventure",
            "Crime",
            "IMAX"
          ],
          "title" : "Dark Knight Rises, The",
          "@version" : "1",
          "year" : 2012
        }
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "91535",
        "_score" : 1.0,
        "_source" : {
          "id" : "91535",
          "genre" : [
            "Action",
            "Adventure",
            "Drama",
            "Thriller",
            "IMAX"
          ],
          "title" : "Bourne Legacy, The",
          "@version" : "1",
          "year" : 2012
        }
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "91842",
        "_score" : 1.0,
        "_source" : {
          "id" : "91842",
          "genre" : [
            "Action",
            "Crime",
            "Drama",
            "Thriller"
          ],
          "title" : "Contraband",
          "@version" : "1",
          "year" : 2012
        }
      }
    ]
  },
  "profile" : {
    "shards" : [
      {
        "id" : "[q3LSyHNoTSeAj6pqYovXjg][movies][0]",
        "searches" : [
          {
            "query" : [
              {
                "type" : "DisjunctionMaxQuery",
                "description" : "(title.keyword:2012 | id.keyword:2012 | year:[2012 TO 2012] | genre:2012 | @version:2012 | @version.keyword:2012 | id:2012 | genre.keyword:2012 | title:2012)",
                "time_in_nanos" : 7531487,
                "breakdown" : {
                  "set_min_competitive_score_count" : 0,
                  "match_count" : 0,
                  "shallow_advance_count" : 0,
                  "set_min_competitive_score" : 0,
                  "next_doc" : 246625,
                  "match" : 0,
                  "next_doc_count" : 219,
                  "score_count" : 219,
                  "compute_max_score_count" : 0,
                  "compute_max_score" : 0,
                  "advance" : 112721,
                  "advance_count" : 3,
                  "score" : 281143,
                  "build_scorer_count" : 6,
                  "create_weight" : 4705554,
                  "shallow_advance" : 0,
                  "create_weight_count" : 1,
                  "build_scorer" : 2184996
                },
                "children" : [
                  {
                    "type" : "TermQuery",
                    "description" : "title.keyword:2012",
                    "time_in_nanos" : 93180,
                    "breakdown" : {
                      "set_min_competitive_score_count" : 0,
                      "match_count" : 0,
                      "shallow_advance_count" : 3,
                      "set_min_competitive_score" : 0,
                      "next_doc" : 0,
                      "match" : 0,
                      "next_doc_count" : 0,
                      "score_count" : 1,
                      "compute_max_score_count" : 4,
                      "compute_max_score" : 4627,
                      "advance" : 628,
                      "advance_count" : 2,
                      "score" : 567,
                      "build_scorer_count" : 4,
                      "create_weight" : 76543,
                      "shallow_advance" : 460,
                      "create_weight_count" : 1,
                      "build_scorer" : 10340
                    }
                  },
                  {
                    "type" : "TermQuery",
                    "description" : "id.keyword:2012",
                    "time_in_nanos" : 4544650,
                    "breakdown" : {
                      "set_min_competitive_score_count" : 0,
                      "match_count" : 0,
                      "shallow_advance_count" : 3,
                      "set_min_competitive_score" : 0,
                      "next_doc" : 0,
                      "match" : 0,
                      "next_doc_count" : 0,
                      "score_count" : 1,
                      "compute_max_score_count" : 4,
                      "compute_max_score" : 17021,
                      "advance" : 2250,
                      "advance_count" : 2,
                      "score" : 3324,
                      "build_scorer_count" : 4,
                      "create_weight" : 4484368,
                      "shallow_advance" : 11479,
                      "create_weight_count" : 1,
                      "build_scorer" : 26193
                    }
                  },
                  {
                    "type" : "PointRangeQuery",
                    "description" : "year:[2012 TO 2012]",
                    "time_in_nanos" : 178315,
                    "breakdown" : {
                      "set_min_competitive_score_count" : 0,
                      "match_count" : 0,
                      "shallow_advance_count" : 6,
                      "set_min_competitive_score" : 0,
                      "next_doc" : 0,
                      "match" : 0,
                      "next_doc_count" : 0,
                      "score_count" : 216,
                      "compute_max_score_count" : 9,
                      "compute_max_score" : 16956,
                      "advance" : 21989,
                      "advance_count" : 219,
                      "score" : 13542,
                      "build_scorer_count" : 6,
                      "create_weight" : 464,
                      "shallow_advance" : 19199,
                      "create_weight_count" : 1,
                      "build_scorer" : 105708
                    }
                  },
                  {
                    "type" : "TermQuery",
                    "description" : "genre:2012",
                    "time_in_nanos" : 7058,
                    "breakdown" : {
                      "set_min_competitive_score_count" : 0,
                      "match_count" : 0,
                      "shallow_advance_count" : 0,
                      "set_min_competitive_score" : 0,
                      "next_doc" : 0,
                      "match" : 0,
                      "next_doc_count" : 0,
                      "score_count" : 0,
                      "compute_max_score_count" : 0,
                      "compute_max_score" : 0,
                      "advance" : 0,
                      "advance_count" : 0,
                      "score" : 0,
                      "build_scorer_count" : 3,
                      "create_weight" : 6183,
                      "shallow_advance" : 0,
                      "create_weight_count" : 1,
                      "build_scorer" : 871
                    }
                  },
                  {
                    "type" : "TermQuery",
                    "description" : "@version:2012",
                    "time_in_nanos" : 6264,
                    "breakdown" : {
                      "set_min_competitive_score_count" : 0,
                      "match_count" : 0,
                      "shallow_advance_count" : 0,
                      "set_min_competitive_score" : 0,
                      "next_doc" : 0,
                      "match" : 0,
                      "next_doc_count" : 0,
                      "score_count" : 0,
                      "compute_max_score_count" : 0,
                      "compute_max_score" : 0,
                      "advance" : 0,
                      "advance_count" : 0,
                      "score" : 0,
                      "build_scorer_count" : 3,
                      "create_weight" : 5736,
                      "shallow_advance" : 0,
                      "create_weight_count" : 1,
                      "build_scorer" : 524
                    }
                  },
                  {
                    "type" : "TermQuery",
                    "description" : "@version.keyword:2012",
                    "time_in_nanos" : 4650,
                    "breakdown" : {
                      "set_min_competitive_score_count" : 0,
                      "match_count" : 0,
                      "shallow_advance_count" : 0,
                      "set_min_competitive_score" : 0,
                      "next_doc" : 0,
                      "match" : 0,
                      "next_doc_count" : 0,
                      "score_count" : 0,
                      "compute_max_score_count" : 0,
                      "compute_max_score" : 0,
                      "advance" : 0,
                      "advance_count" : 0,
                      "score" : 0,
                      "build_scorer_count" : 3,
                      "create_weight" : 4132,
                      "shallow_advance" : 0,
                      "create_weight_count" : 1,
                      "build_scorer" : 514
                    }
                  },
                  {
                    "type" : "TermQuery",
                    "description" : "id:2012",
                    "time_in_nanos" : 35330,
                    "breakdown" : {
                      "set_min_competitive_score_count" : 0,
                      "match_count" : 0,
                      "shallow_advance_count" : 3,
                      "set_min_competitive_score" : 0,
                      "next_doc" : 0,
                      "match" : 0,
                      "next_doc_count" : 0,
                      "score_count" : 1,
                      "compute_max_score_count" : 4,
                      "compute_max_score" : 3526,
                      "advance" : 665,
                      "advance_count" : 2,
                      "score" : 1631,
                      "build_scorer_count" : 4,
                      "create_weight" : 20909,
                      "shallow_advance" : 463,
                      "create_weight_count" : 1,
                      "build_scorer" : 8121
                    }
                  },
                  {
                    "type" : "TermQuery",
                    "description" : "genre.keyword:2012",
                    "time_in_nanos" : 9156,
                    "breakdown" : {
                      "set_min_competitive_score_count" : 0,
                      "match_count" : 0,
                      "shallow_advance_count" : 0,
                      "set_min_competitive_score" : 0,
                      "next_doc" : 0,
                      "match" : 0,
                      "next_doc_count" : 0,
                      "score_count" : 0,
                      "compute_max_score_count" : 0,
                      "compute_max_score" : 0,
                      "advance" : 0,
                      "advance_count" : 0,
                      "score" : 0,
                      "build_scorer_count" : 3,
                      "create_weight" : 8585,
                      "shallow_advance" : 0,
                      "create_weight_count" : 1,
                      "build_scorer" : 567
                    }
                  },
                  {
                    "type" : "TermQuery",
                    "description" : "title:2012",
                    "time_in_nanos" : 45032,
                    "breakdown" : {
                      "set_min_competitive_score_count" : 0,
                      "match_count" : 0,
                      "shallow_advance_count" : 3,
                      "set_min_competitive_score" : 0,
                      "next_doc" : 0,
                      "match" : 0,
                      "next_doc_count" : 0,
                      "score_count" : 2,
                      "compute_max_score_count" : 5,
                      "compute_max_score" : 2049,
                      "advance" : 987,
                      "advance_count" : 3,
                      "score" : 1579,
                      "build_scorer_count" : 4,
                      "create_weight" : 28629,
                      "shallow_advance" : 290,
                      "create_weight_count" : 1,
                      "build_scorer" : 11480
                    }
                  }
                ]
              }
            ],
            "rewrite_time" : 25190,
            "collector" : [
              {
                "name" : "CancellableCollector",
                "reason" : "search_cancelled",
                "time_in_nanos" : 338677,
                "children" : [
                  {
                    "name" : "SimpleTopScoreDocCollector",
                    "reason" : "search_top_hits",
                    "time_in_nanos" : 311751
                  }
                ]
              }
            ]
          }
        ],
        "aggregations" : [ ]
      }
    ]
  }
}

1.4. 直接指定欄位查詢

GET movies/_search?q=title:2012
{
  "profile": "true"
}

結果:

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 11.303033,
    "hits" : [
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "72378",
        "_score" : 11.303033,
        "_source" : {
          "id" : "72378",
          "genre" : [
            "Action",
            "Drama",
            "Sci-Fi",
            "Thriller"
          ],
          "title" : "2012",
          "@version" : "1",
          "year" : 2009
        }
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "105254",
        "_score" : 5.2497,
        "_source" : {
          "id" : "105254",
          "genre" : [
            "Adventure",
            "Comedy"
          ],
          "title" : "Crystal Fairy & the Magical Cactus and 2012",
          "@version" : "1",
          "year" : 2013
        }
      }
    ]
  },
  "profile" : {
    "shards" : [
      {
        "id" : "[q3LSyHNoTSeAj6pqYovXjg][movies][0]",
        "searches" : [
          {
            "query" : [
              {
                "type" : "TermQuery",
                "description" : "title:2012",
                "time_in_nanos" : 83066,
                "breakdown" : {
                  "set_min_competitive_score_count" : 0,
                  "match_count" : 0,
                  "shallow_advance_count" : 0,
                  "set_min_competitive_score" : 0,
                  "next_doc" : 14360,
                  "match" : 0,
                  "next_doc_count" : 2,
                  "score_count" : 2,
                  "compute_max_score_count" : 0,
                  "compute_max_score" : 0,
                  "advance" : 2226,
                  "advance_count" : 1,
                  "score" : 3328,
                  "build_scorer_count" : 4,
                  "create_weight" : 33040,
                  "shallow_advance" : 0,
                  "create_weight_count" : 1,
                  "build_scorer" : 30102
                }
              }
            ],
            "rewrite_time" : 1577,
            "collector" : [
              {
                "name" : "CancellableCollector",
                "reason" : "search_cancelled",
                "time_in_nanos" : 14514,
                "children" : [
                  {
                    "name" : "SimpleTopScoreDocCollector",
                    "reason" : "search_top_hits",
                    "time_in_nanos" : 6528
                  }
                ]
              }
            ]
          }
        ],
        "aggregations" : [ ]
      }
    ]
  }
}

二、Term vs Phrase

2.0 Term vs Phrase區別

2.0.1 Term: Beautiful Mind ==> Beautiful OR Mind

查詢含義: 有 beautiful 或者 有 mind ,兩者有一個即可;

2.0.2 Phrase: "Beautiful Mind" ==> Beautiful AND Mind

查詢含義: 有beautiful 也要有 mind, 都要有,順序也要對;

2.1 Phrase查詢-使用引號""

GET movies/_search?q=title:"Beautiful Mind"

Phrase包含的都要有,順序也不能錯,結果只匹配到一個:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 13.68748,
    "hits" : [
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "4995",
        "_score" : 13.68748,
        "_source" : {
          "id" : "4995",
          "genre" : [
            "Drama",
            "Romance"
          ],
          "title" : "Beautiful Mind, A",
          "@version" : "1",
          "year" : 2001
        }
      }
    ]
  }
}

2.2 泛查詢-不用引號""

GET movies/_search?q=title:(Beautiful Mind)

包含兩個中的一個即可:查到20個:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 20,
      "relation" : "eq"
    },
    "max_score" : 13.687479,
    "hits" : [
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "4995",
        "_score" : 13.687479,
        "_source" : {
          "id" : "4995",
          "genre" : [
            "Drama",
            "Romance"
          ],
          "title" : "Beautiful Mind, A",
          "@version" : "1",
          "year" : 2001
        }
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "3912",
        "_score" : 8.723258,
        "_source" : {
          "id" : "3912",
          "genre" : [
            "Comedy",
            "Drama"
          ],
          "title" : "Beautiful",
          "@version" : "1",
          "year" : 2000
        }
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "47404",
        "_score" : 8.576847,
        "_source" : {
          "id" : "47404",
          "genre" : [
            "Adventure",
            "Animation",
            "Comedy",
            "Fantasy",
            "Romance",
            "Sci-Fi"
          ],
          "title" : "Mind Game",
          "@version" : "1",
          "year" : 2004
        }
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "1046",
        "_score" : 7.317063,
        "_source" : {
          "id" : "1046",
          "genre" : [
            "Drama",
            "Romance"
          ],
          "title" : "Beautiful Thing",
          "@version" : "1",
          "year" : 1996
        }
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "94",
        "_score" : 7.317063,
        "_source" : {
          "id" : "94",
          "genre" : [
            "Comedy",
            "Drama",
            "Romance"
          ],
          "title" : "Beautiful Girls",
          "@version" : "1",
          "year" : 1996
        }
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "4242",
        "_score" : 7.317063,
        "_source" : {
          "id" : "4242",
          "genre" : [
            "Comedy",
            "Crime",
            "Drama",
            "Thriller"
          ],
          "title" : "Beautiful Creatures",
          "@version" : "1",
          "year" : 2000
        }
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "4372",
        "_score" : 7.317063,
        "_source" : {
          "id" : "4372",
          "genre" : [
            "Drama",
            "Romance"
          ],
          "title" : "Crazy/Beautiful",
          "@version" : "1",
          "year" : 2001
        }
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "3302",
        "_score" : 7.317063,
        "_source" : {
          "id" : "3302",
          "genre" : [
            "Comedy"
          ],
          "title" : "Beautiful People",
          "@version" : "1",
          "year" : 1999
        }
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "90353",
        "_score" : 7.317063,
        "_source" : {
          "id" : "90353",
          "genre" : [
            "Drama"
          ],
          "title" : "Beautiful Boy",
          "@version" : "1",
          "year" : 2010
        }
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "100487",
        "_score" : 7.317063,
        "_source" : {
          "id" : "100487",
          "genre" : [
            "Drama",
            "Fantasy",
            "Romance"
          ],
          "title" : "Beautiful Creatures",
          "@version" : "1",
          "year" : 2013
        }
      }
    ]
  }
}

2.3 分組與引號:TermQuery與PhraseQuery

  • title:(Beautiful Mind) --> TermQuery
  • title:"Beautiful Mind" --> PhraseQuery

2.4 布林操作

AND / OR / NOT 或者 &&/||/!

  • 必須大寫;
  • title:(matrix NOT reloaded)

2.5 分組表示:+、- 表示

  • + 表示 must
  • - 表示 must_not
  • title:(+matrix -reloaded)

2.6 有beautiful有mind

GET movies/_search?q=title:(Beautiful AND Mind)

結果只有 "美麗心靈"

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 13.687479,
    "hits" : [
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "4995",
        "_score" : 13.687479,
        "_source" : {
          "id" : "4995",
          "genre" : [
            "Drama",
            "Romance"
          ],
          "title" : "Beautiful Mind, A",
          "@version" : "1",
          "year" : 2001
        }
      }
    ]
  }
}

2.7 有beautiful無mind

GET movies/_search?q=title:(Beautiful NOT Mind)

結果中不會有包含mind的, 只有Beautiful, 因而"美麗心靈"也不會出現:

{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 15,
      "relation" : "eq"
    },
    "max_score" : 8.723258,
    "hits" : [
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "3912",
        "_score" : 8.723258,
        "_source" : {
          "id" : "3912",
          "genre" : [
            "Comedy",
            "Drama"
          ],
          "title" : "Beautiful",
          "@version" : "1",
          "year" : 2000
        }
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "1046",
        "_score" : 7.317063,
        "_source" : {
          "id" : "1046",
          "genre" : [
            "Drama",
            "Romance"
          ],
          "title" : "Beautiful Thing",
          "@version" : "1",
          "year" : 1996
        }
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "94",
        "_score" : 7.317063,
        "_source" : {
          "id" : "94",
          "genre" : [
            "Comedy",
            "Drama",
            "Romance"
          ],
          "title" : "Beautiful Girls",
          "@version" : "1",
          "year" : 1996
        }
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "4242",
        "_score" : 7.317063,
        "_source" : {
          "id" : "4242",
          "genre" : [
            "Comedy",
            "Crime",
            "Drama",
            "Thriller"
          ],
          "title" : "Beautiful Creatures",
          "@version" : "1",
          "year" : 2000
        }
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "4372",
        "_score" : 7.317063,
        "_source" : {
          "id" : "4372",
          "genre" : [
            "Drama",
            "Romance"
          ],
          "title" : "Crazy/Beautiful",
          "@version" : "1",
          "year" : 2001
        }
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "3302",
        "_score" : 7.317063,
        "_source" : {
          "id" : "3302",
          "genre" : [
            "Comedy"
          ],
          "title" : "Beautiful People",
          "@version" : "1",
          "year" : 1999
        }
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "90353",
        "_score" : 7.317063,
        "_source" : {
          "id" : "90353",
          "genre" : [
            "Drama"
          ],
          "title" : "Beautiful Boy",
          "@version" : "1",
          "year" : 2010
        }
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "100487",
        "_score" : 7.317063,
        "_source" : {
          "id" : "100487",
          "genre" : [
            "Drama",
            "Fantasy",
            "Romance"
          ],
          "title" : "Beautiful Creatures",
          "@version" : "1",
          "year" : 2013
        }
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "114126",
        "_score" : 7.317063,
        "_source" : {
          "id" : "114126",
          "genre" : [
            "Documentary"
          ],
          "title" : "Beautiful Losers",
          "@version" : "1",
          "year" : 2008
        }
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "2324",
        "_score" : 6.3012905,
        "_source" : {
          "id" : "2324",
          "genre" : [
            "Comedy",
            "Drama",
            "Romance",
            "War"
          ],
          "title" : "Life Is Beautiful",
          "@version" : "1",
          "year" : 0
        }
      }
    ]
  }
}

2.8 可能有beautiful,但必須有mind

GET movies/_search?q=title:(Beautiful %2BMind)
  • +(%2B): must
  • -(%2D): must_not
  • !(%21): NOT

必須有mind, Beautiful可有可無:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 5,
      "relation" : "eq"
    },
    "max_score" : 13.687479,
    "hits" : [
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "4995",
        "_score" : 13.687479,
        "_source" : {
          "id" : "4995",
          "genre" : [
            "Drama",
            "Romance"
          ],
          "title" : "Beautiful Mind, A",
          "@version" : "1",
          "year" : 2001
        }
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "47404",
        "_score" : 8.576847,
        "_source" : {
          "id" : "47404",
          "genre" : [
            "Adventure",
            "Animation",
            "Comedy",
            "Fantasy",
            "Romance",
            "Sci-Fi"
          ],
          "title" : "Mind Game",
          "@version" : "1",
          "year" : 2004
        }
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "6003",
        "_score" : 5.7810974,
        "_source" : {
          "id" : "6003",
          "genre" : [
            "Comedy",
            "Crime",
            "Drama",
            "Thriller"
          ],
          "title" : "Confessions of a Dangerous Mind",
          "@version" : "1",
          "year" : 2002
        }
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "144606",
        "_score" : 5.7810974,
        "_source" : {
          "id" : "144606",
          "genre" : [
            "Comedy",
            "Crime",
            "Drama",
            "Romance",
            "Thriller"
          ],
          "title" : "Confessions of a Dangerous Mind",
          "@version" : "1",
          "year" : 2002
        }
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "7361",
        "_score" : 5.2145147,
        "_source" : {
          "id" : "7361",
          "genre" : [
            "Drama",
            "Romance",
            "Sci-Fi"
          ],
          "title" : "Eternal Sunshine of the Spotless Mind",
          "@version" : "1",
          "year" : 2004
        }
      }
    ]
  }
}

2.9 範圍查詢:[ ] 閉區間 { }開區間

  • year:{2019 TO 2018}

下面會查到所有 2017年的(2016和2018不會包含)

GET movies/_search?q=year:({2016 TO 2018})
  • year:[* TO 2018] --> 2018年以前(包括2018)

下列會查到201620172018三個年份的:

GET movies/_search?q=year:([2016 TO 2018])

2.10 算術符號: >, >=, <, <=

  • year:>2010

2010年以後

下列是查詢2017年以後的(不包括2017)

GET movies/_search?q=year:(>2017)

  • year:(>2010 AND <=2018)

大於2010且小於等於2018

GET movies/_search?q=year:(>2010 AND <=2018)

2.11 查詢1980年以後的電影

GET movies/_search?q=year:>=1999&sort=year:desc

查詢所有1999年以後的電影,按照year倒序

2.12 萬用字元查詢(不建議:低效、耗記憶體,特別是前置)

  • ? 一個字元 --> title:mi?d-->會匹配mind miid
  • * 0或多個字元 --> title:be* --》會匹配beautiful

2.13 模糊查詢與近似查詢

  • title:beautifl~1 --》 匹配"beautiful": 下面都會匹配到beautiful (~1)表示容錯一個字元:
POST movies/_search?q=title:beautifal~1
POST movies/_search?q=title:beautifl~1
  • title:"lord rings"~2 -->可查到"Lord of the Rings..." 也就是中間可以有2個多餘詞!
POST movies/_search?q=title:"lord rings"~2
POST movies/_search?q=title:"lord rings"~3

上面的可以查詢到:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : 4.700435,
    "hits" : [
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "2116",
        "_score" : 4.700435,
        "_source" : {
          "id" : "2116",
          "genre" : [
            "Adventure",
            "Animation",
            "Children",
            "Fantasy"
          ],
          "title" : "Lord of the Rings, The",
          "@version" : "1",
          "year" : 1978
        }
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "5952",
        "_score" : 3.2970178,
        "_source" : {
          "id" : "5952",
          "genre" : [
            "Adventure",
            "Fantasy"
          ],
          "title" : "Lord of the Rings: The Two Towers, The",
          "@version" : "1",
          "year" : 2002
        }
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "4993",
        "_score" : 2.7496965,
        "_source" : {
          "id" : "4993",
          "genre" : [
            "Adventure",
            "Fantasy"
          ],
          "title" : "Lord of the Rings: The Fellowship of the Ring, The",
          "@version" : "1",
          "year" : 2001
        }
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "7153",
        "_score" : 2.7496965,
        "_source" : {
          "id" : "7153",
          "genre" : [
            "Action",
            "Adventure",
            "Drama",
            "Fantasy"
          ],
          "title" : "Lord of the Rings: The Return of the King, The",
          "@version" : "1",
          "year" : 2003
        }
      }
    ]
  }
}

相關文章