Elasticsearch Dynamic template not working as required - elasticsearch

I want to have an elasticsearch schema that has some pre defined fields including an object type field. I want to have all the fields inside that object type field to be string by default.
I have the following mappings and dynamic templates while creating the index.
PUT myindex
{
"mappings": {
"doc": {
"dynamic_templates": [
{
"default_string": {
"path_match": "myObj.*",
"match_mapping_type": "*",
"mapping": {
"type": "text"
}
}
}],
"properties": {
"dummy_field_name": { "type": "text" },
"timestamp": {
"type": "date",
"format": "epoch_second"
},
"myObj": {
"type": "object"
}
}
}
}
}
But when I submit a field inside the object with a numeric value, it is not mapping that field to string.
curl -XPOST "http://elastic-url:8080/myindex/test" -d
'{"dummy_field_name": "dymmy_value", "myObj":{ "filed_1": 123 ,
"field_2": "some value"}, "timestamp": 1522196333}'
"filed_1" is identified as a number field. But I want it to be stored as a string type.
Field types detected

Your mapping is defined for type "doc" but you are indexing to type "test", try matching them

Related

is it possible to update nested filed by uuid

I am using update_by_query to update document,and i have a nested field named "Myfield1".There is a string field named "id" in "Myfield1".In my case,the mapping something like this:
"mapping":{
"mytype":{
"properties": {
"Myfield1": {
"type": "nested",
"properties": {
"id": {
"type": "string"
},
"field2": {
"type": "long"
}
}
},
"Title": {
"type": "string"
}
}
}
}
The field "id" in nested is unique generated by uuid.Then i want to update field2 in the nested field Myfield by query with the following request.
{
"query": {
"nested":{
"path":"Myfield1",
"query":{"match":{"Myfield1.id":"uuid"}},
"inner_hits":{"from":0,"size":1}
}
},
"script":"for (field in ctx._source.Myfield1) {if(field.id=='uuid') {field.field2='new value'}}"
}
I know it work,but the efficiency of this script is too low.How can I write ”script“ to directly update the field2 in inner_hits.Or maybe there are other efficient ways.
Thanks!

How to rename a field in Elasticsearch?

I have an index in Elasticsearch with the following field mapping:
{
"version_data": {
"properties": {
"title": {
"type": "text",
"fields": {
"raw": {
"type": "keyword"
}
}
},
"updated_at": {
"type": "date"
},
"updated_by": {
"type": "keyword"
}
}
}
}
I have already created some documents in it and now want to rename version_data field with _version_data.
Is there any way in the Elasticsearch to rename a field within the mapping and in documents?
The closest thing is the alias data type.
In your mapping you could link it from the old to the new name like this:
PUT test/_mapping
{
"properties": {
"_version_data": {
"type": "alias",
"path": "version_data"
}
}
}
BTW I would generally avoid leading underscored since those normally used for internal fields like _id.

Return only top level fields in elasticsearch query?

I have a document that has nested fields. Example:
"mappings": {
"blogpost": {
"properties": {
"title": { "type": "text" },
"body": { "type": "text" },
"comments": {
"type": "nested",
"properties": {
"name": { "type": "text" },
"comment": { "type": "text" },
"age": { "type": "short" },
"stars": { "type": "short" },
"date": { "type": "date" }
}
}
}
}
}
}
Can the query be modified so that the response only contains non-nested fields?
In this example, the response would only contain body and title.
Using _source you can exclude/include fields
GET /blogpost/_search
{
"_source":{
"excludes":["comments"]
}
}
But you have to explicitly put the field names inside exclude, I'm searching for a way to exclude all nested fields without knowing their field name
You can achieve that but in a static way, which means you entered the field(s) name using excludes keyword, like:
GET your_index/_search
{
"_source": {
"excludes": "comments"
},
"query": {
"match_all" : {}
}
}
excludes can take an array of strings; not just one string.

Can I define default type for text as keyword inside an object type?

Let's say I have the following mapping:
{
"mappings": {
"_doc": {
"properties": {
"id": {"type": "keyword"},
"params": {
"type": "object",
},
}
}
}
}
params has a lot of fields with numeric and textual values. I don't want to specify all the fields of it but I would like to specify that all the text fields should be of keyword type instead of text which is the default and is analysed (which I want to avoid).
How can I do this? I'm using ElasticSearch 6.7
This is called Dynamic Templates (See https://www.elastic.co/guide/en/elasticsearch/reference/6.7/dynamic-templates.html)
For your question you can define the mapping as follows:
PUT my_index
{
"mappings": {
"_doc": {
"dynamic_templates": [
{
"strings": {
"match_mapping_type": "string",
"mapping": {
"type": "keyword"
}
}
}
]
}
}
}
This transforms all the detected string fields to keyword. If you want to apply only to a subset of fields, you can use match, unmatch, path_match or path_unmatch clauses. (See the docs for differences between them).
Adding to what #banuj mentioned:
It seems that it is possible to combine match_mapping_type and path_match to achieve what I wanted.
PUT my_index
{
"mappings": {
"_doc": {
"dynamic_templates": [
{
"keyword params": {
"mapping": {
"type": "keyword"
},
"match_mapping_type": "string",
"path_match": "params.*"
}
}
]
}
}
}

Elasticsearch get all fields of type date

I'm using olivere's elastic v.5 Go library (https://godoc.org/gopkg.in/olivere/elastic.v5) for my Elastic queries. If I have an elastic mapping like this:
"mappings": {
"boxes": {
"properties": {
"field1": {
"type": "string"
},
"field2": {
"type": "string"
},
"field3": {
"type": "date"
},
"field4": {
"type": "date"
}
}
}
And I want to get a list of all fields that have type 'date'.
I've looked into GetFieldMapping but that doesn't seem to have an option to filter the fields based on type.
I tried this:
elasticclient.GetFieldMapping().Index("someindex").Type("boxes").Field().Type("date").Pretty(true).Do(ctx)
That just gives me all the fields and their types. Is there a different syntax to do this? Thanks!

Resources