Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using python how can I register specific mapping definition?

In the elasticsearch-py docs I can't find an example registering a mapping, which performs what these REST API docs say:

The put mapping API allows to register specific mapping definition for a specific type.

$ curl -XPUT 'http://localhost:9200/twitter/tweet/_mapping' -d '
{
    "tweet" : {
        "properties" : {
            "message" : {"type" : "string", "store" : true }
        }
    }
}
'
like image 728
b_dev Avatar asked Jul 03 '14 03:07

b_dev


2 Answers

Doing anything with the index involves the Indices APIs. PUTing mapping is also one of many Indices API. They can be found in the Indices section under API Documentation of the Python client docs.

And you need this: put_mapping(*args, **kwargs).

like image 137
vaidik Avatar answered Dec 20 '22 19:12

vaidik


Here is a complete example :

from elasticsearch import Elasticsearch
def fMap(document_type):
    mapping={document_type:{\
             "properties":{\
             "a_field":{"type":"integer","store":"yes"},\
             "other_field": {"type":"string","index":"analyzed","store":"no"},\
             "title":{"type":"string","store":"yes","index": "analyzed","term_vector":"yes","similarity":"BM25"},\
             "content":{"type":"string","store":"yes","index":  "analyzed","term_vector": "yes","similarity":"BM25"}\
             }}}
    return mapping
def dSetting(nShards,nReplicas):
    dSetting={
    "settings":{"index":{"number_of_shards":nShards,"number_of_replicas":nReplicas}},\
    "analysis":{\
        "filter":{\
        "my_english":{"type":"english","stopwords_path":"english.txt"},\
        "english_stemmer":{"type":"stemmer","language":"english"}},\
        "analyzer":{"english":{"filter":["lowercase","my_english","english_stemmer"]}}}}
    return dSetting
def EsSetup(con,idx,docType,dset,mapping):
    con.indices.delete(index=idx,ignore=[400, 404])
    con.indices.create(index=idx,body=dset,ignore=400)
    con.indices.put_mapping(index=idx,doc_type=docType,body=mapping)

conEs=Elasticsearch([{'host':'localhost','port':9200,'timeout':180}])
dMap = fMap('docTypeName')
dSet = dSetting(8,1)
EsSetup(conEs,'idxName','docTypeName',dset,dMap)
like image 33
taufikedys Avatar answered Dec 20 '22 19:12

taufikedys