Elasticsearch core detail

The document

Metadata

  • _index index
  • The _type document type
  • _id unique identifier (default 32 bits)
Query response

Pertty: Makes the returned JSON easier to view

GET {query url}? pretty

Specify the response field: _source

GET /haoke/user/1005? _source=id,name

Returns only the specified field: _source? source=

GET /haoke/user/1005/_source? _source=id,name
Determine whether the document exists

HEAD /{index}/{type}/{id}

The batch operation
Batch query _mget

POST /{index}/{type}/ _MGET

{
  "ids":["AXqP5R4PXV22eQAormWv","1111"]
}



Found :false indicates that the query result does not exist

Bulk add, delete and change _bulk

POST /{index}/{type}/_bulk

Insert / * * / {" create ": {" _index" : "haoke", "_type" : "user", "_id" : 2001}} {" id ": 2001," name ":" name1 ", "age" : 20, "sex" : "Male"} {" create ": {" _index" : "haoke", "_type" : "user", "_id" : 2002}} {" id ": 2002," name ":" name2 ", "age" : 20, "sex" : "Male"} / / the last line needs a carriage return/delete * * / {" delete ": {" _index" : "haoke", "_type" : "user", "_id" : 2001}} {" delete ": {" _index" : "haoke", "_type" : "user", "_id" : 2002}} / / the last line needs a carriage return
paging

Size: Number of results, default 10


From: Skips the number of results from the start. Default is 0

GET /{index}/{type}/_search? size=10&from=0

ES paging generates sorting results once per shard and then aggregates them for sorting, so too deep pages or too many requests can affect performance

mapping

Text: A field that needs to be searched (partitioned words) in full text, such as content, description


Keyword: Structured fields, such as address, status, label

Create a mapping

PUT /{index}

{
    "settings":{
        "index":{
            "number_of_shards":"2",
            "number_of_replicas":"0"
        }
    },
    "mappings":{
        "person":{
            "properties":{
                "name":{
                    "type":"text"
                },
                "age":{
                    "type":"integer"
                },
                "mail":{
                    "type":"keyword"
                },
                "hobby":{
                    "type":"text"
                }
            }
        }
    }
}
Check the map

GET /{index}/_mapping

Search test

POST /test/person/_bulk

{" create ": {" _index" : "test", "_type" : "person", "_id" : 1}} {" id ": 1," name ":" listen to the music ", "age" : 20} {" create ": {" _index" : "test", "_type" : "person", "_id" : 2}} {" id ": 2," name ":" play music ", "age" : 20}

POST /{index}/{document}/_search

{"query":{"match":{"name":" music "}}}

Structured query

TERM query: exactly match the value. Numbers, dates, Boolean values
POST/index of {} {} document / _search {" query ": {" term" : {" age ": 20}}}
Terms query: multi-conditional matching
POST/index of {} {} document / _search {" query ": {" terms" : {" age ": [20, 21]}}}
Range query: Range matching
POST/index of {} {} document / _search {" query ": {" range" : {" age ": {" gte" : 20}}}}
Exits query: The document is long enough to contain a field, equivalent to SQLIS NULLconditions
POST/index of {} {} document / _search {" query ": {" exists" : {" field ":" hobby "}}}
Match query: Standard query, Full-text query, Precision query
POST/index of {} {} document / _search {" query ": {" match" : {" name ":" music "}}}
BOOL queries: Merges multiple query conditions

Must, must, must, must, must, must, must
and


Must_not matches multiple conditions opposite, equivalent to
not


Should has at least one condition match, which is equal to
or

POST/index of {} {} document / _search {" query ": {" bool" : {" must ": {" term" : {" age ": 20}}," must_not ": {" term" : {" name ": "Listen to the music"}}, "should" : [{" term ": {" name" : "name1"}}, {" term ": {" name" : "name2"}}]}}}

Filter the query: Filter

POST/index of {} {} document / _search {" query ": {" bool" : {" filter ": {" term" : {" age ": 20}}}}}
Query versus filtering
  • A filter statement asks each document if the field value contains a particular value.
  • The query asks how well the field values of each document match a particular value.
  • A query statement calculates the correlation of each document to the query statement, gives a correlation score of _score, and sorts the matched documents according to their relevance. This scoring method works well for a full-text search that does not have fully configured results.
  • A simple document list, quick matching and memory storage is very convenient, each document only needs 1 byte. These cached filter result sets can be very efficient in combination with subsequent requests.
  • Queries not only find matching documents, but also calculate the relevance of each document, so queries are generally more time-consuming than filters, and the results are not cacheable.

Tip: When doing exact match searches, it's best to use filter statements, because filter statements cache data.