This is the 14th day of my participation in The August More Text Challenge.

ES Operation (Middle)

Index mappings

Index participle concept

Index: Default true, false, this field will not be indexed

Create mappings along with the index creation
# PUT/name} {index (custom) # PUT http://192.168.123.64:9200/index_jacquesh {" the mappings ": {" properties" : {" username ": {" type" : "Text ", "index": true}," realName ": {"type": "keyword", "index": false}}}} # return {"acknowledged": True, "shards_trees ": true, "index": "index_mapping"} # Index created successfullyCopy the code
“Mappings” is used with setting
# PUT http://192.168.123.64:9200/ {index name} {" the mappings ": {" properties" : {" username ": {" type" : "text", "index" : true }, "realname": { "type": "keyword", "index": false } } }, "settings": { "index": { "number_of_shards": "2", "number_of_replicas": "0"}}} ## Return {"acknowledged": true, "shards_acknowledged": true, "index": "Index_mapping_and_setting"} ##Copy the code
Check the word segmentation effect
# # the GET / {index name} / _analyze GET http://192.168.123.64:9200/index_mapping {" field ":" username ", "text" : {"tokens": "start_offset": 0, "end_offset": 2, "type": {"tokens": 0, "end_offset": 2, "type": "<ALPHANUM>", "position": 0 }, { "token": "name", "start_offset": 3, "end_offset": 7, "type": "<ALPHANUM>", "position": 1 }, { "token": "is", "start_offset": 8, "end_offset": 10, "type": "<ALPHANUM>", "position": 2 }, { "token": "Jacquesh ", "start_offset": 11, "end_offset": 19, "type": "<ALPHANUM>", "position": 3}} ##Copy the code
Modifying an Index Mapping
# POST http://192.168.123.64:9200/ {index name} / _mapping {" properties ": {" username" : {" type ": {"root_cause": [{"root_cause": [{"type": "illegal_argument_exception", "reason": "mapper [username] cannot be changed from type [text] to [long]" } ], "type": "illegal_argument_exception", "reason": "mapper [username] cannot be changed from type [text] to [long]" }, "status": {"properties": {"sex": {"type": "integer"}}} # Return {"acknowledged": True} ### New addition succeededCopy the code
Primary data types
  • text, keyword, string
  • long, integer, short, byte
  • double, float
  • boolean
  • date
  • object
  • Arrays cannot be mixed; they are of the same type
string
  • Text: The contents of the text class that need to be segmented and inverted, such asName of commodity.Goods details.The introduction, use the text
  • Keyword: matches the search directly without being categorized or inverted, for exampleThe order status.Qq users.WeChat ID.Mobile phone no.And so on, these exact matches, without word segmentation

Basic operation of the document

Add document data

# POST /{index name}/_doc/{index ID} # POST /{index name}/_doc/{index ID} # If not, a string is automatically generated. < Suggestions and consistent data id >) # # POST to http://192.168.123.64:9200/index_jacquesh/_doc/1001 {" id ": 1001," name ":" jacuqesh ", "desc" : "Jacuqesh is very good, Jacuqesh is very good!" , "create_date" : "2020-09-04"} # # returns {" _index ":" index_jacquesh ", "_type" : "_doc", "_id" : "1001", "_version" : 1, "result": "created", "_shards": { "total": 1, "successful": 1, "failed": 0 }, "_seq_no": 0, "_primary_term": 2} # # to create success Next to another new data # # 2 for POST to http://192.168.123.64:9200/index_jacquesh/_doc/1002 {" id ": 1002," name ": "Jack ", "desc":" Jack is very nice, Jack is very nice! , "create_date" : "2020-09-05"} # # POST to http://192.168.123.64:9200/index_jacquesh/_doc/1003 {" id ": 1003," name ": "Huang ", "desc":" Huang is Daniel Wu of Guangdong Province! , "create_date": "2020-09-15" }Copy the code

View the index information: You can see that the mappings have been created automatically

  • If the index does not have manual mappings, the attribute type will be automatically set based on the document type when inserting document data. This is the dynamic mapping of ES, which helps us establish the configuration information of data structure in index index library.
  • “Fields” : {” type “:” keyword “} Sets multiple indexing modes for a field, using the text type for full-text search, as well as the keyword type for aggregation and sorting
  • Ignore_above: 256 Sets the maximum length of the field index and storage. If the value exceeds this value, the value is ignored

Delete the document

# DELETE / {index name} / _doc /} {index ID # # # DELETE http://192.168.123.64:9200/index_jacquesh/_doc/1001 returns {" _index ": "index_jacquesh", "_type": "_doc", "_id": "1001", "_version": 2, "result": "deleted", "_shards": { "total": 1, "successful" : 1, "failed" : 0}, "_seq_no" : 1, "_primary_term" : 2} # # deleted successfullyCopy the code
  • Note: Document deletions are not immediate deletions, documents remain on disk, and as indexes grow, those that have been marked for deletions will be cleaned up and removed from disk.

    Id =1001

Modify the document

  • Local:

    # POST / {index name} / _doc/index of {ID} / _update # POST to http://192.168.123.64:9200/index_jacquesh/_doc/1002/_update {" doc ": {" name" : "Huangkunhuang # # returns {}}" _index ":" index_jacquesh ", "_type" : "_doc", "_id" : "1002", "_version" : 2, "result" : "Updated", "_shards" : {" total ": 1," successful ": 1," failed ": 0}," _seq_no ": 1," _primary_term ": 2} # # modified successfullyCopy the code
  • Full replacement:

    # PUT / {index name} / _doc/index of {ID} {" ID ": 1002," name ":" huang ", "desc" : "huang is written code too good!" , "create_date" : "2020-12-24"} # # returns {" _index ":" index_jacquesh ", "_type" : "_doc", "_id" : "1002", "_version" : 3, "result": "updated", "_shards": { "total": 1, "successful": 1, "failed": 0 }, "_seq_no": 2, "_primary_term": 2} ## Update succeededCopy the code
  • Note: Version changes after each modification

Query the document

Conventional query

/ / the first # GET / {index name} / _doc /} {index ID # http://192.168.123.64:9200/index_jacquesh/_doc/1002 returns {" _index ": "index_jacquesh", "_type": "_doc", "_id": "1002", "_version": 3, "_seq_no": 2, "_primary_term": 2, "found": True, "_source" : {" id ": 1002," name ":" huang ", "desc" : "huang is written code too good!" , "create_date": "2020-12-24"}} / / the second # GET / {index name} / _doc _search # GET http://192.168.123.64:9200/index_jacquesh/_doc/_search {" query" : {" match ": {" _id" : 1002}}} returns {" took ": 136," timed_out ": false," _shards ": {" total" : 5, "successful" : 5, "skipped" : 0, "failed" : "hits" : {0}, "total" : {" value ": 1, the" base ":" eq "}, "max_score" : 1.0, "hits" : [{" _index ":" index_jacquesh ", "_type" : "_doc", "_id" : "1002", "_score" : 1.0, "_source" : {" id ": 1002," name ": "Huang", "desc" : "huang is written code too good!", "create_date" : "2020-12-24"}}}}]Copy the code

\