2

I am using ElasticSearch in a website where i index data from MongoDB.

def postToEs(self):
    """
    put data to the elasticsearch
    """
    es = Elasticsearch()
    cursor = self.getMongoData()
    for document in cursor:
        esdoc={}
        esdoc["category"] = document.get("category")
        esdoc["description"] = document.get("description")
        esdoc["title"] = document.get("title")
        esdoc["link"] = document.get("link")
        esdoc["state"] = document.get("state")
        esdoc_id = esdoc["link"]
        es.index(
            index = 'news',
            doc_type = 'allnews',
            id = esdoc_id,
            body = json.dumps(esdoc)
        )

This worked good. But currently I have to search in the state field for an exact match in elasticsearch. Currently if I search for entries for New York it also gives result of New Hampshire. I have found This link and saw elasticsearch documentation that I need to add mapping on the data. My question is how do I add a mapping on the data in the current scenario? or is there any better way of doing it?

Community
  • 1
  • 1
salmanwahed
  • 9,450
  • 7
  • 32
  • 55
  • 1) we can add new field to mapping . but we cannot modify mapping after indexing. we need to re index. pls provide your current mapping.. Then i ll help you – BlackPOP Apr 23 '14 at 09:24
  • current mapping is like this `{"allnews":{"properties":{"category":{"type":"string"},"description":{"type":"string"},"link:{"type":"string"},"state":{"type":"string"},"title":{"type":"string"}}}}` – salmanwahed Apr 23 '14 at 09:30
  • i need to map the `state` as `"state":{"type":"string","index" : "not_analyzed"}` – salmanwahed Apr 23 '14 at 09:34
  • if I re index how do i specify the mapping. As i am new in Elasticsearch i don't know very much about it's query syntax – salmanwahed Apr 23 '14 at 09:36

1 Answers1

6

Delete the existing index

curl -XDELETE "http://hostname:9200/index/type"

Delete the existing river config index

curl -XDELETE "http://hostname:9200/_river"

Create mapping to index

curl -XPUT "http://hostname:9200/index/type/_mapping" -d'
{
"allnews": {
    "properties": {
        "category": {
            "type": "string"
        },
        "description": {
            "type": "string"
        },
        "link": {
            "type": "string"
        },
        "state": {
            "type": "string",
            "index" : "not_analyzed"
        },
        "title": {
            "type": "string"
        }
    }
}
}'

After these steps put the river plugin config sync mongodb to elasticsearch.

HOpe it helps..!

BlackPOP
  • 5,657
  • 2
  • 33
  • 49
  • Thank you. Actually I solved it already the same way you have specified here. Is there any way of doing this from python script? – salmanwahed Apr 23 '14 at 10:36