实践006-elasticsearch查询之1-URI Search查询

[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
        }
      }
    ]
  }
}

你可能感兴趣的:(elasticsearch)