Elasticsearch use the RESTful API

basic concepts:

  Index
    index (index) is logic Elasticsearch storing logical data, it can be divided into smaller portions.
    The index can be seen as a relational database table, index structure is prepared for the fast and efficient full-text indexing, in particular, it does not store the original value.
    Elasticsearch the index can be stored in a single machine or distributed over multiple servers, each with one or more index fragments (Shard), each slice may have a plurality of copies (replica).
  Documents
    stored in Elasticsearch major entity called the document (document). With a relational database analogy, a document equivalent to a row in the database table.

    Elasticsearch and MongoDB is a document similar, can have different structures, but Elasticsearch document, the same field must have the same type.
    Documentation of a plurality of fields, each field can appear more than once in a document, such a field called multi-value field (multivalued).
    Type for each field, it may be text, numeric, date, and the like. Field types can also be complex type, a field that contains documents or other sub-array.

  Mapping
    all will first be written into the document before the index analysis, how to enter text is divided into entries, which entries will be filtered, this behavior is called mapping (mapping). General rules defined by the user.
  Document type
    in Elasticsearch, the index object can store a lot of objects of different purposes. For example, a blog application can save articles and reviews.
    Each document can have a different structure.
    Different document types can not set the same properties of different types. For example, all document types in the same index, one called the title field must have the same type.

RESTful API:

  In Elasticsearch provided a feature-rich operating RESTful API, including basic CRUD, create an index, delete the index and other operations.

  Creating unstructured Index:

    In Lucene, create the index is a need to define the field names and field types, and provides index unstructured in Elasticsearch, the structure is no need to create an index, you can write data to the index, in fact, the bottom will Elasticsearch structuring operation, this operation is transparent to the user.
    Create an empty index

      PUT / haoke

{
    "settings": {
        "index": {
            "number_of_shards": "2", #分片数
            "number_of_replicas": "0" #副本数
        }
    }
}

    Delete Index

      DELETE / haoke 

"acknowledged": true
}

  Insert data:

    The POST / index repository {} / {index type} / {id} id pass if not, automatically generated

    POST /haoke/user/1001

{
    "id": 1001,
    "name": "张三",
    "age": 20,
    "sex": "男"
}

    

       Description: unstructured index, does not need to be created in advance, directly into the data created by default index.

    Id inserted data is not specified:

      POST /haoke/user/

{
    "id": 1002,
    "name": "张三",
    "age": 20,
    "sex": "男"
}

      

   update data:

    In Elasticsearch, the document data is not modified, but the cover can be updated by the following steps:

      1. Retrieve the JSON from the old document
      2. Modify it
      delete old documents
      4. indexing new documents

      PUT /haoke/user/1001

{
    "id": 1001,
    "name": "李四",
    "age": 30,
    "sex": "女"
}

    

    Local update: Note: This is more than a logo _update

      POST /haoke/user/1001/_update

{
    "doc": {
        "age": 23
    }
}

      

   delete data:

    In Elasticsearch, delete the document data, you only need to initiate a DELETE request.

      DELETE / haoke / user / 1001 

      

      It should be noted, result representation has been deleted, version also adds, if you delete a data does not exist, will respond to 404.

    Description:
      Delete a file will not be removed immediately from the disk, but it is marked as deleted. Elasticsearch will be added after the index you more time will be cleaned to remove content in the background.
  Search data:

    According to id search data

      GET /haoke/user/6NAVEXEBVAiLr6jRjciF

      

     Search All Data (default return 10 data)

      GET /haoke/user/_search

    Keyword search elements data

      Age 20 is equal to the user query

      GET /haoke/user/_search?q=age:20

  DSL Search:

    Elasticsearch provide a rich and flexible query language called DSL query (Query DSL), which allows you to build more complex and powerful queries.
    DSL (Domain Specific Language domain-specific language) in the form of bodies JSON request occurs.

      POST /haoke/user/_search

{
     "Query" : {
         "match" : {#match only one query
             "Age": 20 is 
        } 
    } 
}

    Men older than 18 years old user query is.

      POST /haoke/user/_search

{
    "query": {
        "bool": {
            "filter": {
                "range": {
                    "age": {
                        "gt": 18
                    }
                }
            },
            "must": {
                "match": {
                    "sex": "男"
                }
            }
        }
    }
}

    research all

      POST /haoke/user/_search

{
    "query": {
        "match": {
            "name": "张三 李四"
        }
    }
}

  Highlight:

    POST /haoke/user/_search

{
    "query": {
        "match": {
            "name": "张三 李四"
        }
    },
    "highlight": {
            "fields": {
                "name": {}
        }
    }
}

    

   polymerization:

    In Elasticsearch, the supported polymerization operation, similar to the group by SQL operations.

    POST /haoke/user/_search

{
    "aggs": {
        "all_interests": {
            "terms": {
                "field": "age"
            }
        }
    }
}

    

 

Guess you like

Origin www.cnblogs.com/roadlandscape/p/12568550.html