elastic / elasticsearch-rs

Official Elasticsearch Rust Client
https://www.elastic.co/guide/en/elasticsearch/client/rust-api/current/index.html
Apache License 2.0
702 stars 72 forks source link

[DOCS] How to create a Mapping? #66

Closed DevQps closed 4 years ago

DevQps commented 4 years ago

Please describe the documentation I was browsing the documentation and was unable to find some documentation on how to create a mapping using the API. Maybe it would be nice to provide an example on the IndicesPutMapping or put_mapping functions?

Describe where the documentation should for A documented example should be added that shows how to create a mapping on an index.

russcam commented 4 years ago

Hi @DevQps

A mapping can be created either upon index creation:

let response = client
    .indices()
    .create(IndicesCreateParts::Index("test_index"))
    .body(json!({
        "mappings" : {
            "properties" : {
                "field1" : { "type" : "text" }
            }
        }
    }))
    .send()
    .await?;

Or by putting a mapping into an existing index (assuming the index does not have a mapping, or if it does, that any of the properties do not conflict with existing properties):

let response = client
    .indices()
    .put_mapping(IndicesPutMappingParts::Index(&["test_index"]))
    .body(json!({
        "properties" : {
            "field1" : { "type" : "text" }
        }
    }))
    .send()
    .await?;

The client API functions and builder structs are generated from a spec, which makes weaving examples into them a little tricky. https://github.com/elastic/elasticsearch-rs/issues/64 will generate rust examples for all the examples in the elastic.co reference documentation, so there will be a dropdown to see what a specific call looks like for the Rust client. Take a look at the match query page on master as an example. Once these doc examples are implemented, I think we could look at weaving them into docs.rs, tackling the problem more generally.

DevQps commented 4 years ago

Thanks for your response!