关于elasticsearch:实践006elasticsearch查询之1URI-Search查询

35次阅读

共计 20020 个字符,预计需要花费 51 分钟才能阅读完成。

[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.1 Term vs Phrase 区别

2.1.1 Term: Beautiful Mind ==> Beautiful OR Mind

查问含意:有 beautiful 或者 有 mind,两者有一个即可;

2.1.2 Phrase: “Beautiful Mind” ==> Beautiful AND Mind

查问含意:有beautiful 也要有 mind, 都要有,程序也要对;

2.2 Term & Phrase 测试案例

2.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.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.2.3 分组与引号:TermQuery 与 PhraseQuery

  • title:(Beautiful Mind) –> TermQuery
  • title:”Beautiful Mind” –> PhraseQuery

2.2.4 布尔操作

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

  • 必须大写;
  • title:(matrix NOT reloaded)

2.2.5 分组示意:+、- 示意

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

2.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.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.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.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.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.2.11 查问 1980 年当前的电影

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

查问所有 1999 年当前的电影,依照 year 倒序

2.2.12 通配符查问(不倡议:低效、耗内存,特地是前置)

  • ? 一个字符 –> title:mi?d–> 会匹配 mind miid
  • * 0 或多个字符 –> title:be* –》会匹配 beautiful

2.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
        }
      }
    ]
  }
}

正文完
 0