Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create or update mapping in elasticsearch

I am new to Elasticsearch and am currently working on implementing a geo_distance filter for searching. As of now my index has the following mapping (I've removed some fields):

{ advert_index: {    mappings: {       advert_type: {          properties: {             __v: {                type: "long"             },             caption: {                type: "string"             },             category: {                type: "string"             },             **location: {             type: "long"             },**           }       }    } } 

The geo_distance field is going to be implemented on the location field, where an example instance looks like this:

"location": [                71,                60             ], 

I.e. is on geoJSON format [lon, lat].

I understand that I will have to update my index so that the location field is of type geo_point, as described in the documentation (mapping-geo-point). It seems like I have to drop the index and create a new one, but I am not able to do this.

Am I on the right track? I would greatly appreciate it if anyone could help me with how I could create a new index or update my existing one with the correct data type.

Many thanks!

like image 961
Axelfran Avatar asked Aug 24 '14 12:08

Axelfran


People also ask

Can we update mapping in Elasticsearch?

If the Elasticsearch security features are enabled, you must have the manage index privilege for the target data stream, index, or alias. [7.9] If the request targets an index or index alias, you can also update its mapping with the create , create_doc , index , or write index privilege.

What is an Elasticsearch mapping?

Advertisements. Mapping is the outline of the documents stored in an index. It defines the data type like geo_point or string and format of the fields present in the documents and rules to control the mapping of dynamically added fields.


2 Answers

Generally speaking, you can update your index mapping using the put mapping api (reference here) :

curl -XPUT 'http://localhost:9200/advert_index/_mapping/advert_type' -d ' {     "advert_type" : {         "properties" : {            //your new mapping properties          }     } } ' 

It's especially useful for adding new fields. However, in your case, you will try to change the location type, which will cause a conflict and prevent the new mapping from being used.

You could use the put mapping api to add another property containing the location as a lat/lon array, but you won't be able to update the previous location field itself.

Finally, you will have to reindex your data for your new mapping to be taken into account.

The best solution would really be to create a new index.

If your problem with creating another index is downtime, you should take a look at aliases to make things go smoothly.

like image 60
ThomasC Avatar answered Oct 08 '22 04:10

ThomasC


Please note that there is a mistake in the url provided in this answer:

For a PUT mapping request: the url should be as follows:

http://localhost:9200/name_of_index/_mappings/document_type

and NOT

http://localhost:9200/name_of_index/document_type/_mappings

like image 28
Algorini Avatar answered Oct 08 '22 04:10

Algorini