day01

1. index3条doc

PUT books/_doc/1
{
  "title":"Effective Java",
  "author":"Joshua Bloch",
  "release_date":"2001-06-01",
  "amazon_rating":4.7,
  "best_seller":true,
  "prices": {
    "usd":9.95,
    "gbp":7.95,
    "eur":8.95
  }
}

PUT books/_doc/2
{
  "title":"Core Java Volume I - Fundamentals",
  "author":"Cay S. Horstmann",
  "release_date":"2018-08-27",
  "amazon_rating":4.8,
  "best_seller":true,
  "prices": {
    "usd":19.95,
    "gbp":17.95,
    "eur":18.95
  }
}

PUT books/_doc/3
{
  "title":"Java: A Beginner’s Guide",
  "author":"Herbert Schildt",
  "release_date":"2018-11-20",
  "amazon_rating":4.2,
  "best_seller":true,
  "prices": {
    "usd":19.99,
    "gbp":19.99,
    "eur":19.99
  }
}

2. 查询记录数/按照docid获取/查询多个_id

2.1 count记录数

# 1. count
GET books/_count

{
"count" : 3,
"_shards" : {

"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0

}
}

2.2 用doc id获取

GET books/_doc/2

{
"_index" : "books",
"_type" : "_doc",
"_id" : "2",
"_version" : 1,
"_seq_no" : 1,
"_primary_term" : 1,
"found" : true,
"_source" : {
"title" : "Core Java Volume I - Fundamentals",
"author" : "Cay S. Horstmann",
"release_date" : "2018-08-27",
"amazon_rating" : 4.8,
"best_seller" : true,
"prices" : {

 "usd" : 19.95,
 "gbp" : 17.95,
 "eur" : 18.95

}
}
}

2.3 获取多个id

GET books/_search
{
  "query": {
    "ids": {
      "values": [1,3]
    }
  }
}

{
"took" : 1,
"timed_out" : false,
"_shards" : {

"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0

},
"hits" : {

"total" : {
  "value" : 2,
  "relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
  {
    "_index" : "books",
    "_type" : "_doc",
    "_id" : "1",
    "_score" : 1.0,
    "_source" : {
      "title" : "Effective Java",
      "author" : "Joshua Bloch",
      "release_date" : "2001-06-01",
      "amazon_rating" : 4.7,
      "best_seller" : true,
      "prices" : {
        "usd" : 9.95,
        "gbp" : 7.95,
        "eur" : 8.95
      }
    }
  },
  {
    "_index" : "books",
    "_type" : "_doc",
    "_id" : "3",
    "_score" : 1.0,
    "_source" : {
      "title" : "Java: A Beginner’s Guide",
      "author" : "Herbert Schildt",
      "release_date" : "2018-11-20",
      "amazon_rating" : 4.2,
      "best_seller" : true,
      "prices" : {
        "usd" : 19.99,
        "gbp" : 19.99,
        "eur" : 19.99
      }
    }
  }
]

}
}

2.4 查询所有

# 4. get all docs
GET books/_search

{
"took" : 2,
"timed_out" : false,
"_shards" : {

"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0

},
"hits" : {

"total" : {
  "value" : 3,
  "relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
  {
    "_index" : "books",
    "_type" : "_doc",
    "_id" : "1",
    "_score" : 1.0,
    "_source" : {
      "title" : "Effective Java",
      "author" : "Joshua Bloch",
      "release_date" : "2001-06-01",
      "amazon_rating" : 4.7,
      "best_seller" : true,
      "prices" : {
        "usd" : 9.95,
        "gbp" : 7.95,
        "eur" : 8.95
      }
    }
  },
  {
    "_index" : "books",
    "_type" : "_doc",
    "_id" : "2",
    "_score" : 1.0,
    "_source" : {
      "title" : "Core Java Volume I - Fundamentals",
      "author" : "Cay S. Horstmann",
      "release_date" : "2018-08-27",
      "amazon_rating" : 4.8,
      "best_seller" : true,
      "prices" : {
        "usd" : 19.95,
        "gbp" : 17.95,
        "eur" : 18.95
      }
    }
  },
  {
    "_index" : "books",
    "_type" : "_doc",
    "_id" : "3",
    "_score" : 1.0,
    "_source" : {
      "title" : "Java: A Beginner’s Guide",
      "author" : "Herbert Schildt",
      "release_date" : "2018-11-20",
      "amazon_rating" : 4.2,
      "best_seller" : true,
      "prices" : {
        "usd" : 19.99,
        "gbp" : 19.99,
        "eur" : 19.99
      }
    }
  }
]

}
}

2.5 match匹配字段

# 5. Search a Book Written By a Specific Author
GET books/_search
{
  "query": {
    "match": {
      "author": "Joshua"
    }
  }
}

{
"took" : 4,
"timed_out" : false,
"_shards" : {

"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0

},
"hits" : {

"total" : {
  "value" : 1,
  "relation" : "eq"
},
"max_score" : 1.0417082,
"hits" : [
  {
    "_index" : "books",
    "_type" : "_doc",
    "_id" : "1",
    "_score" : 1.0417082,
    "_source" : {
      "title" : "Effective Java",
      "author" : "Joshua Bloch",
      "release_date" : "2001-06-01",
      "amazon_rating" : 4.7,
      "best_seller" : true,
      "prices" : {
        "usd" : 9.95,
        "gbp" : 7.95,
        "eur" : 8.95
      }
    }
  }
]

}
}

  • match:会先分词, 再一个一个分词匹配
  • match_phrase: 不会分词, 作为一个完整匹配
  • term: 不分词

2.6 match+operator(and/or)

# 6. match + and/or
GET books/_search
{
  "query": {
    "match": {
      "title": {
        "query": "Effective Java",
        "operator": "and"
      }
    }
  }
}

{
"took" : 5,
"timed_out" : false,
"_shards" : {

"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0

},
"hits" : {

"total" : {
  "value" : 1,
  "relation" : "eq"
},
"max_score" : 1.36891,
"hits" : [
  {
    "_index" : "books",
    "_type" : "_doc",
    "_id" : "1",
    "_score" : 1.36891,
    "_source" : {
      "title" : "Effective Java",
      "author" : "Joshua Bloch",
      "release_date" : "2001-06-01",
      "amazon_rating" : 4.7,
      "best_seller" : true,
      "prices" : {
        "usd" : 9.95,
        "gbp" : 7.95,
        "eur" : 8.95
      }
    }
  }
]

}
}

GET books/_search
{
  "query": {
    "match": {
      "title": {
        "query": "Effective Java",
        "operator": "or"
      }
    }
  }
}

3条都返回:

{
"took" : 3,
"timed_out" : false,
"_shards" : {

"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0

},
"hits" : {

"total" : {
  "value" : 3,
  "relation" : "eq"
},
"max_score" : 1.36891,
"hits" : [
  {
    "_index" : "books",
    "_type" : "_doc",
    "_id" : "1",
    "_score" : 1.36891,
    "_source" : {
      "title" : "Effective Java",
      "author" : "Joshua Bloch",
      "release_date" : "2001-06-01",
      "amazon_rating" : 4.7,
      "best_seller" : true,
      "prices" : {
        "usd" : 9.95,
        "gbp" : 7.95,
        "eur" : 8.95
      }
    }
  },
  {
    "_index" : "books",
    "_type" : "_doc",
    "_id" : "3",
    "_score" : 0.12874341,
    "_source" : {
      "title" : "Java: A Beginner’s Guide",
      "author" : "Herbert Schildt",
      "release_date" : "2018-11-20",
      "amazon_rating" : 4.2,
      "best_seller" : true,
      "prices" : {
        "usd" : 19.99,
        "gbp" : 19.99,
        "eur" : 19.99
      }
    }
  },
  {
    "_index" : "books",
    "_type" : "_doc",
    "_id" : "2",
    "_score" : 0.11623955,
    "_source" : {
      "title" : "Core Java Volume I - Fundamentals",
      "author" : "Cay S. Horstmann",
      "release_date" : "2018-08-27",
      "amazon_rating" : 4.8,
      "best_seller" : true,
      "prices" : {
        "usd" : 19.95,
        "gbp" : 17.95,
        "eur" : 18.95
      }
    }
  }
]

}
}


丰木
322 声望19 粉丝

遇见超乎想象的自己!