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!
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With