[toc]


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

Queries are indexed based on movielens data.

1. Detailed URI Search

1.1. Introduction to URI Search

 GET movies/_search?q=2012&df=title&sort=year:desc&from=0&size=10&timeout=1s
{
  "profile": "true"
}
  • q: Specifies the query statement. Use Query String Syntax
  • df(default field): The default field, if not specified, 所有字段 will be queried.
  • sort: sort
  • from/size: for pagination
  • profile: can see how the query is executed

result:

 {
  "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. Field query: query the records with 2012 in the title

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

result

 {
  "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. Pan query: query 2012

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

General query, if no field is specified, all fields will be queried:

 {
  "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. Directly specify field query

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

result:

 {
  "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" : [ ]
      }
    ]
  }
}

2. Term vs Phrase

2.0 Term vs Phrase Difference

2.0.1 Term: Beautiful Mind ==> Beautiful OR Mind

Query meaning: There is beautiful or there is mind , one of the two is enough;

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

Query meaning: if there is beautiful d336754a9875e4024e5e830197c2ade8---, it should also have mind , and it should be in the right order;

2.1 Phrase query - use quotation marks ""

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

Phrase must contain everything, and the order can't be wrong. The result only matches one:

 {
  "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 Generic query - no quotation marks ""

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

Include one of the two: 20 are found:

 {
  "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 Grouping and Quotes: TermQuery and PhraseQuery

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

2.4 Boolean Operations

AND / OR / NOT or && / || / !

  • must be capitalized;
  • title: (matrix NOT reloaded)

2.5 Group representation: +, - represent

  • + means must
  • - means must_not
  • title:(+matrix -reloaded)

2.6 There is beautiful and mind

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

The result is only "beautiful 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 but no mind

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

The result will not contain mind , only Beautiful , so "Beautiful Mind" will not appear:

 {
  "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 May be beautiful, but must have mind

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

Must have mind, Beautiful is optional:

 {
  "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 Range query: [ ] closed interval { } open interval

  • year:{2019 TO 2018}

The following will find all 2017 years (2016 and 2018 will not be included)

 GET movies/_search?q=year:({2016 TO 2018})
  • year:[* TO 2018] --> before 2018 (including 2018)

The following will find 2016 , 2017 , 2018 three years:

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

2.10 Arithmetic symbols: > , >= , < , <=

  • year:>2010

After 2010

The following is the query after 2017 (excluding 2017)

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

  • year:(>2010 AND <=2018)

Greater than 2010 and less than or equal to 2018

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

2.11 Query movies after 1980

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

Query all movies after 1999, in reverse order by year

2.12 Wildcard query (not recommended: inefficient, memory-intensive, especially front-end)

  • ? one character --> title:mi?d -->will match mind miid
  • * 0 or more characters --> title:be* --" will match beautiful etc.

2.13 Fuzzy and approximate queries

  • title:beautifl~1 --" matches "beautiful": the following will match beautiful (~1) means fault tolerance for one character:
 POST movies/_search?q=title:beautifal~1
POST movies/_search?q=title:beautifl~1
  • title:"lord rings"~2 -->You can find "Lord of the Rings..." That is, there can be 2 redundant words in the middle!
 POST movies/_search?q=title:"lord rings"~2
POST movies/_search?q=title:"lord rings"~3

The above can be queried:

 {
  "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
        }
      }
    ]
  }
}

丰木
322 声望19 粉丝

遇见超乎想象的自己!