[USACO Question Library] 3.3.1 RIDING The FENCES Riding Fair Raiders

2023-01-23   ES  

1, cluster management

1. View cluster health status

curl -X GET "localhost:9200/_cat/health?v"

2. View cluster health status

curl -X GET "localhost:9200/_cluster/health?pretty"

3. View nodes in the cluster

curl -X GET "localhost:9200/_cat/nodes?v"

4. List all the current indexes

curl -X GET "localhost:9200/_cat/indices?v"

5. Create index

curl -X PUT "localhost:9200/customer?pretty"

6. Delete the index

curl -X DELETE "localhost:9200/customer?pretty"

7. Set data type

curl -X PUT "localhost:9200/allcountry?pretty" -H "Content-Type:application/json" -d '
{
    "settings": {
        "index.number_of_replicas": 0
    },
    "mappings": {
        "country_info": {
            "properties": {
                "geonameid": {
                    "type": "long"
                },
                "name": {
                    "type": "text"
                },
                "latitude": {
                    "type": "double"
                },
                "longitude": {
                    "type": "double"
                },
                "population": {
                    "type": "long"
                }
            }
        }
    }
}'

2, data management

1, add document

curl -X PUT "localhost:9200/customer/customer_info/1?pretty" -H 'Content-Type: application/json' -d'
{
  "name": "John Doe"
}
'

2, query document

curl -X GET "localhost:9200/customer/customer_info/1?pretty"

3, replace the document

curl -X PUT "localhost:9200/customer/customer_info/1?pretty" -H "Content-Type:application/json" -d '
{
   "name":"Milton"
}
'

4, update the document

curl -X POST "localhost:9200/customer/customer_info/1/_update?pretty" -H 'Content-Type: application/json' -d'
{
  "doc": { "name": "Apple" }
}
'

5. Update Document — Add new fields

curl -X POST "localhost:9200/customer/customer_info/1/_update?pretty" -H 'Content-Type: application/json' -d'
{
  "doc": { "name": "Tom", "age": 20 }
}
'

6. Update Document — Use Scripts script update

curl -X POST "localhost:9200/customer/customer_info/1/_update?pretty" -H 'Content-Type: application/json' -d'
{
  "script" : "ctx._source.age += 5"
}
'

Note: CTX represents the updated object itself

7. Delete the document

curl -X DELETE "localhost:9200/customer/customer_info/2?pretty"

3, batch processing

1. Add document in batches

curl -X POST "localhost:9200/customer/customer_info/_bulk?pretty" -H 'Content-Type: application/json' -d'
{"index":{"_id":"11"}}
{"name": "Milton" }
{"index":{"_id":"22"}}
{"name": "Cherish" }
'

2, batch update and delete document

curl -X POST "localhost:9200/customer/customer_info/_bulk?pretty" -H 'Content-Type: application/json' -d'
{"update":{"_id":"11"}}
{"doc": { "name": "Milton Love Cherish" } }
{"delete":{"_id":"22"}}
'

3. Create/update index in batches

curl -X POST "localhost:9200/_bulk?pretty" -H 'Content-Type: application/json' -d'
{"index":{"_index":"teacher","_type":"teacher_info","_id":1}}
{"name":"Miltom"}
{ "index" : { "_index" : "teacher", "_type" : "teacher_info", "_id" : "2" } }
{ "name" : "Cherish" }
{ "index" : { "_index" : "teacher", "_type" : "teacher_info", "_id" : "3" } }
{ "name" : "Evan" }
'

4. Create in batches

curl -X POST "localhost:9200/_bulk?pretty" -H 'Content-Type: application/json' -d'
{ "create" : { "_index" : "teacher", "_type" : "teacher_info", "_id" : "4" } }
{ "name" : "yangp" }
{ "create" : { "_index" : "teacher", "_type" : "teacher_info", "_id" : "5" } }
{ "name" : "yangf" }
'

Note: both and create can add documents. When using Index, if the record exists, it will be updated. If it does not exist, it will increase; but when using Create, if the record exists, it will fail!

5. Update update in batches

curl -X POST "localhost:9200/teacher/teacher_info/_bulk?pretty" -H "Content-Type: application/json" -d '
{ "update" : { "_id" : "4" } }
{"doc":{ "name" : "yangp_update" }}
{ "update" : { "_id" : "5" } }
{"doc":{ "name" : "yangf_update" }}
'

6. Delete delete in batches

curl -X POST "localhost:9200/teacher/teacher_info/_bulk?pretty" -H "Content-Type: application/json" -d '
{"delete":{"_id":"4"}}
{"delete":{"_id":"5"}}
'

7. Get in batches _mget

In the index Teacher, the ID of the ID is 1,2 (three ways)

curl -X GET "localhost:9200/_mget?pretty" -H "Content-Type: application/json" -d '
{
    "docs":[
        {
            "_index":"teacher",
            "_type":"teacher_info",
            "_id":"1",
	    "_source":["name"]
        },
        {
            "_index":"teacher",
            "_type":"teacher_info",
            "_id":"2",
	    "_source":["name"]
        }
        ]
}
'
curl -X GET "localhost:9200/teacher/teacher_info/_mget?pretty" -H "Content-Type: application/json" -d '
{
    "docs":[
        { "_id":"1"},
        { "_id":"2" }
        ]
}
'
curl -X GET "localhost:9200/teacher/teacher_info/_mget?pretty" -H "Content-Type: application/json" -d '
{
	"ids":["1","2"]
}
'

Four, matching deletion and update

1. Match deletion_delete_by_query

Delete named “EVAN” from the index Teacher

curl -X POST "localhost:9200/teacher/_delete_by_query?pretty" -H "Content-Type: application/json" -d '
{
    "query":{
        "match":{
            "name":"Evan"
                }
            }
}
'

2, match update_update_by_query

From the index Teacher, update Name’s document containing “Miltom”, setting its gene = “boy”, Age = 100

curl -X POST "localhost:9200/teacher/teacher_info/_update_by_query?pretty" -H "Content-Type: application/json" -d '
{
    "query":{
        "match":{
            "name":"Miltom"
                }
    },
    "script":{
        "source":"ctx._source.gener=params.gener;ctx._source.age=params.age",
        "params":{
            "gener":"Boy",
            "age":10
            }
    }
}
'

5. Data import

Import json file accounts.json to ES

curl -X POST "localhost:9200/bank/bank_info/_bulk?pretty" -H "Content-Type: application/json" --data-binary "@accounts.json"

6. Data retrieval

  • Query: Specify the query conditions, here is {“mATCH_ALL”: {}} represent the query condition to match all the records
  • from: It means starting the value from Article N match records, defaults to 0
  • size: Indicates the number of matching strips, the default 10
  • sort: Represents sorting. Here is {“Balance”: {“Order”: “Desc”}}, indicating that it is sorted by BALANCE order. Here you can also write [{“balance”: “desc”}]
  • _Source: Represents the query field. Here is the two fields of “Account_number” and “Balance”. Back to all fields by default.
curl -X GET "localhost:9200/bank/_search?pretty" -H "Content-Type: application/json" -d '
{
    "query":{"match_all":{}},
    "from":10,
    "size":5,
    "sort":[
        {"balance":"desc"}
        ],
    "_source":["account_number","balance"]
}
'

1. MATCH query: query account_number = 20 document

curl -X GET "localhost:9200/bank/_search?pretty" -H "Content-Type: application/json" -d '
{
    "query":{
        "match":{"account_number":20}
        }
}
'

2, MATCH query: query the document containing “Mill” in the adDRESS

curl -X GET "localhost:9200/bank/_search?pretty" -H "Content-Type: application/json" -d '
{
    "query":{
        "match":{"address":"mill"}
        }
}
'

3, MATCH query: Query adDRESS contains “Mill” or “Lane” documents

curl -X GET "localhost:9200/bank/_search?pretty" -H "Content-Type: application/json" -d '
{
    "query":{
        "match":{"address":"mill lan"}
        }
}
'

4. MATCH_PHRASE Query: Inquiry the Document containing the phrase “Mill Lane” in the adDRESS

curl -X GET "localhost:9200/bank/_search?pretty" -H "Content-Type: application/json" -d '
{
    "query":{
        "match_phrase":{"address":"mill lane"}
        }
}
'

Note: The difference between MATCH and MATCH_PHRASE

  • For the result of MATCH, we can see that the description of description in the result of the result can include “HE IS”, “HE” or “IS”;
  • MATCH_PHRASED results in the result of the description field in the results of the “HE IS” must be included;
  • All retrieval results have a _score field, which looks like the current DOCUMENT score under the current search conditions, and the retrieval result is also sorted according to this score from high to low.

5. BOOL and Relationship Query: Query the Document of “Mill” and “Lane” in the address

curl -X GET "localhost:9200/bank/_search?pretty" -H "Content-Type: application/json" -d '
{
    "query":{
        "bool":{
            "must":[
                {"match":{"address":"mill"}},
                {"match":{"address":"lane"}}
                ]
            }
    }
}
'

6, BOOL OR relationship query: Query the docume of “Mill” and “Lane” in the adDRESS

curl -X GET "localhost:9200/bank/_search?pretty" -H "Content-Type: application/json" -d '
{
    "query":{
        "bool":{
            "should":[
                {"match":{"address":"mill"}},
                {"match":{"address":"lane"}}
                ]
            }
    }
}
'

7, BOOL NOT Relationship Query: Inquiring about the Document of “Mill” or “Lane” in the adDRESS

curl -X GET "localhost:9200/bank/_search?pretty" -H "Content-Type: application/json" -d '
{
    "query":{
        "bool":{
            "must_not":[
                {"match":{"address":"mill"}},
                {"match":{"address":"lane"}}
                ]
            }
    }
}
'

8, BOOL combination query: query Age = 40, state! = “ID” document

curl -X GET "localhost:9200/bank/_search?pretty" -H "Content-Type: application/json" -d '
{
    "query":{
        "bool":{
            "must":[
                {"match":{"age":40}}
                ],
            "must_not":[
                {"match":{"state":"ID"}}
                ]
            }
      }
}
'

9, BOOL FILTER query: Query 20000 <= Balance <= 30000 document

curl -X GET "localhost:9200/bank/_search?pretty" -H "Content-Type: application/json" -d '
{
    "query":{
        "bool":{
            "filter":{
                "range":{
                    "balance":{
                        "gte":20000,
                        "lte":30000
                        }
                    }
                }
            }
        }
}
'

7. Search in multiple indexes

1. Search in the two indexes of BANK and Teacher at the same time

curl -X GET "localhost:9200/bank,teacher/_search?pretty" -H "Content-Type: application/json" -d '
{
    "query":{
        "match":{"age":39}
        }                     
}'

2. Search in all indexes

curl -X GET "localhost:9200/_all/_search?pretty" -H "Content-Type: application/json" -d '
{
    "query":{
        "match":{"age":39}
        },
    "size":100
}'

8. Query all files

curl -X GET "localhost:9200/website/_search?pretty" -H "Content-Type: application/json" -d '
{
    "query":{
        "match_all":{}
        }
}'

9. Query query conditions and Filter filtering conditions

1. Inquiry “Search” in “Title”, “Content” contains “Elasticsearch”, “STatus” is “Published”, “Publish_date”> 2015-01-01

curl -X GET "localhost:9200/_search?pretty" -H "Content-Type: application/json" -d '
{
    "query":{
        "bool":{
            "must”:[
                {"match":{"title":"Search"}},
                {"match":{"content":"Elasticsearch"}}
                ],
            "filter":[
                {"term":{"status":"published"}},
                {"range":{"publish_date":{"gte":"2015-01-01"}}}
                ]
            }
    }
}'

10. Search Multi_match from multiple fields

1. Search “Learn” from “Title”, Content “

curl -X GET "localhost:9200/website/_search?pretty" -H "Content-Type: application/json" -d '
{
    "query":{
        "multi_match":{
            "query":"learn",
            "fields":["title","content"]
            }
        }
}'

2. Search “Here” from “Title”, Content “

curl -X GET "localhost:9200/website/_search?pretty" -H "Content-Type: application/json" -d '
{
    "query":{
        "multi_match":{
            "query":"here",
            "fields":["title","content"]
            }
        }
}'

11, search statistics_Count

curl -X GET "localhost:9200/website/blog/_count?pretty" -H "Content-Type: application/json" -d '
{
    "query":{
        "term":{"title":"learn"}
    }
}'

source

Related Posts

leetcode problem list index python3 step by step update ~ salted fish that struggle to turn over =

[mysql] mysqldump export database table

MyBatis’s physical class and table fields corresponding to ResultMap writing

bzoj 4012: [hnoi2015] Opening the store -dynamic tree division governance

[USACO Question Library] 3.3.1 RIDING The FENCES Riding Fair Raiders

Random Posts

Blue Bridge Cup —— 2012 C ++ Group A Question 3: Pirates Comparison [Meeting]

Set up Discuz Forum

python built

c language -io operation (1) wangs7

Database in the database without loss of decomposition and whether to keep functional dependencies