Basic command statement

  • When updating with PUT, all attributes must be copied; otherwise, they will be empty
  • With POST/index name/type name/document ID / _UPDATE, you only need to fill in the attributes to be modified, which is more flexible
method url describe
PUT Localhost :9200/ index name/type name/document ID Create document (specify ID)
POST Localhost :9200/ index name/type name Create document (random document ID)
POST Localhost :9200/ index name/type name/document ID /_update Modify the document
DELETE Localhost :9200/ index name/type name/document ID Delete the document
GET Localhost :9200/ index name/type name/document ID Query documents by ID
POST Localhost :9200/ index name/type name /_search Query all data
// Create an empty index
PUT /test2
{
  "mappings": {
    "properties": {
      "name": {"type": "text"},
      "age": { "type": "integer"},
      "birthday": {"type": "date"}}}}// Create an index test1 type type1 id=1
PUT /test1/type1/1
{
  // Insert data into index
  "name": "xxxxx"."age": 18
}
// Get index information
GET test1
Copy the code

The query

A simple query

GET test1/type1/_search
{
  // Query the parameter body
  "query": {"match": { // The result of the query is fuzzy matching
      "name": "Meet" // Query conditions}}}Copy the code

Results:

{
  "took" : 901."timed_out" : false."_shards" : {
    "total" : 1."successful" : 1."skipped" : 0."failed" : 0
  },
  "hits" : {// Query results are placed in hits
    "total" : {
      "value" : 3."relation" : "eq"
    },
    "max_score" : 0.26706278."hits": [{"_index" : "test1"."_type" : "type1"."_id" : "2"."_score" : 0.26706278.// The greater the match, the greater the value
        "_source" : {
          "name" : "Meet _line"."age" : 20}}, {"_index" : "test1"."_type" : "type1"."_id" : "3"."_score" : 0.26706278."_source" : {
          "name" : "Meet _line3"."age" : 20}}, {"_index" : "test1"."_type" : "type1"."_id" : "1"."_score" : 0.26706278."_source" : {
          "name" : "Meet _line1"."age" : 20}}]}}Copy the code

Results the filter

Fields can be filtered by adding _source to the original query

GET test1/type1/_search
{
  "query": {"match": {
      "name": "Meet"}},"_source" : ["name"]}Copy the code

Results:

"hits": [{"_index" : "test1"."_type" : "type1"."_id" : "2"."_score" : 0.26706278."_source" : {
          "name" : "Meet _line"
          // age does not exist}}Copy the code

Results the sorting

Sort by sort

GET test1/type1/_search
{
  "query": {"match": {
      "name": "Meet"}},"_source" : ["name"]."sort":[
    {
      "age": {"order": "desc" //" asC "ascending, descending desc}}}]Copy the code
"hits": [{"_index" : "test1"."_type" : "type1"."_id" : "3"."_score" : null."_source" : {
          "name" : "Meet _line3"
        },
        "sort" : [
          21] {},"_index" : "test1"."_type" : "type1"."_id" : "2"."_score" : null."_source" : {
          "name" : "Meet _line2"
        },
        "sort" : [
          20] {},"_index" : "test1"."_type" : "type1"."_id" : "1"."_score" : null."_source" : {
          "name" : "Meet _line1"
        },
        "sort" : [
          19]]}}Copy the code

Paging query

GET test1/type1/_search
{
  "query": {"match": {
      "name": "Meet"}},"_source" : ["name"]."sort":[
    {
      "age": {"order": "desc"}}]."from":0.// Start at 0, subscript 0
  "size":2  // Display 2 entries per page
}
Copy the code

Many conditions query | Bool queries

All the conditions must be met

Query name contains met and age 19

GET test1/type1/_search
{
  "query": {"bool": {
      "must": [{"match": {
            "name": "Meet"}}, {"match": {
            "age": 19}}]}}}Copy the code

Should (or)

Select * from person where age 20 or 19

GET test1/type1/_search
{
  "query": {"bool": {
      "should": [{"match": {
            "age": "20"}}, {"match": {
            "age": 19}}]}}}Copy the code

Msut_not is equivalent to not

The query is neither 19 nor 20 years old

GET test1/type1/_search
{
  "query": {"bool": {
      "must_not": [{"match": {
            "age": "20"}}, {"match": {
            "age": 19}}]}}}Copy the code

Filter filter

Select age from [10,20] and name from people you met

GET test1/type1/_search
{
  "query": {"bool": {
      "must": [{"match": {
            "name": "Meet"}}]."filter": [{"range": {
            "age": {
              "gte": 10.//gt> gte>=
              "lte": 20}}}]}}Copy the code

Multi-match query tags

Query for people whose tags contain male or technology

GET test1/type1/_search
{
  "query": {"match": {
      "tags": "Man technique" // Space indicates or}}Copy the code

Precise query term

Query by inverted index

The original data Inverted index
The blog id The label The label The blog id
1 windows windows 1, 2, 3
2 windows linux 3, 4
3 windows,linux
4 liunx
  • Term direct and accurate matching
  • Match uses a word splitter (analyze documents first, then find)
  • Text: will be split by the word divider
  • The keyword is treated as a whole and is not split by the segmentation
GET test1/type1/_search
{
  "query": {"term": {
      "name": "Meet _line1"}}}Copy the code

Highlighting the query

GET test1/type1/_search
{
  "query": {"match": {
      "name": "Meet"}},// Select the name field to highlight
  "highlight" : {
    "fields": {"name": {}}}}Copy the code
"hits": [{"_index" : "test1"."_type" : "type1"."_id" : "3"."_score" : 0.17402273."_source" : {
          "name" : "Meet _line3"."age" : 21."tags" : [
            "Straight"."Geek"."Shy"]},"highlight" : {
          "name" : [
          // The highlighted part is wrapped in the EM tag
            " encounter  see _line3"]}},Copy the code

We can also customize the highlight tag

GET test1/type1/_search
{
  "query": {"match": {
      "name": "Meet"}},"highlight" : {
    "pre_tags": "<p color='red'>"."post_tags": "</p>"."fields": {"name": {}}}}Copy the code
"highlight" : {
          "name" : [
            

see

_line3"
]}}Copy the code