How enable nested mapping? - elasticsearch

using ES 6 and not being able to set my mapping correctly.
I have this doc:
{
"_index": "entries_1",
"_type": "elasticsearch-record",
"_id": "3684",
"_score": 5.355921,
"_source": {
"title": "My Title",
"result": {
"autor": [
"fernando-fernandes"
]
}
}
}
And my mapping:
{
"craft-entries_1": {
"mappings": {
"elasticsearch-record": {
"properties": {
"result": {
"type": "nested",
"enabled": false
}
}
}
}
}
}
And I can't query results.autor with this:
{
"query": {
"bool" : {
"must" : [
{ "term": { "result.autor": "fernando-fernandes" } }
]
}
}
}
I've tried PUT this, but seems has no effect on mapping at all, even after I query again my mapping still appears as enabled:false, maybe I should mapping as object?
{
"properties": {
"result.autor": {
"type": "nested",
"enabled": true
}
}
}
What i'm missing?

Your source document is not designed properly according to your mapping, it should be like this:
{
"title": "My Title",
"result": [
{
"autor": "fernando-fernandes"
}
]
}
Since result is nested, it should be modeled as an array with elements inside.
So, delete your index, recreate it with your following mapping (you need to remove enabled: false as that's only for object types)
PUT craft-entries_1
{
"mappings": {
"elasticsearch-record": {
"properties": {
"result": {
"type": "nested"
}
}
}
}
}
Finally, index your documents as I showed above, and then your query will work.

Related

Matching the stored values and queries in Elastic Search

I have a field called that is inside a nested field "name" that is a "Keyword" in elastic search.
Name field contains 2 values.
Jagannathan Rajagopalan
Rajagopalan.
If I query "Rajagopalan", I should get only the item #2.
If I query the complete Jagannathan Rajagopalan, I should get #1.
How do I achieve it?
You need to use the term query which is used for exact search. Added a working example according to your use-case.
Index mapping
{
"mappings": {
"properties": {
"name": {
"type": "nested",
"properties": {
"keyword": {
"type": "keyword"
}
}
}
}
}
}
Index sample docs
{
"name" : {
"keyword" : "Jagannathan Rajagopalan"
}
}
And another doc
{
"name" : {
"keyword" : "Jagannathan"
}
}
And search query
{
"query": {
"nested": {
"path": "name",
"query": {
"bool": {
"must": [
{
"match": {
"name.keyword": "Jagannathan Rajagopalan"
}
}
]
}
}
}
}
}
Search result
"hits": [
{
"_index": "key",
"_type": "_doc",
"_id": "2",
"_score": 0.6931471,
"_source": {
"name": {
"keyword": "Jagannathan Rajagopalan"
}
}
}
]

search array of strings by partially match in elasticsearch

I got fields like that:
names: ["Red:123", "Blue:45", "Green:56"]
it's mapping is
"names": {
"type": "keyword"
},
how could I search like this
{
"query": {
"match": {
"names": "red"
}
}
}
to get all the documents where red is in element of names array?
Now it works only with
{
"query": {
"match": {
"names": "red:123"
}
}
}
You can add multi fields OR just change the type to text, to achieve your required result
Index Mapping using multi fields
{
"mappings": {
"properties": {
"names": {
"type": "text",
"fields": {
"raw": {
"type": "keyword"
}
}
}
}
}
}
Adding a working example with index data, mapping, search query, and search result
Index Mapping:
{
"mappings":{
"properties":{
"names":{
"type":"text"
}
}
}
}
Index Data:
{
"names": [
"Red:123",
"Blue:45",
"Green:56"
]
}
Search Query:
{
"query": {
"match": {
"names": "red"
}
}
}
Search Result:
"hits": [
{
"_index": "64665127",
"_type": "_doc",
"_id": "1",
"_score": 0.2876821,
"_source": {
"names": [
"Red:123",
"Blue:45",
"Green:56"
]
}
}
]

In Elasticsearch, how to move data from one field into another field

I have an index with mappings that look like this:
"mappings": {
"default": {
"_all": {
"enabled": false
},
"properties": {
"Foo": {
"properties": {
"Bar": {
"type": "keyword"
}
}
}
}
}
I am trying to change the mapping to introduce a sub-field of Bar, called Code, whilst migrating the string currently in Bar into Bar.Code. Here is the new mapping:
"mappings": {
"default": {
"_all": {
"enabled": false
},
"properties": {
"Foo": {
"properties": {
"Bar": {
"properties": {
"Code": {
"type": "keyword"
}
}
}
}
}
}
}
In order to do this, I think I need to do a _reindex and specify a pipeline. Is that correct? If so, how does my pipeline access the original data?
I have tried variations on the following code, but without success:
PUT _ingest/pipeline/transformFooBar
{
"processors": [
{
"set": {
"field": "Bar.Code",
"value": "{{_source.Bar}}"
}
}
]
}
POST _reindex
{
"source": {
"index": "foo_v1"
},
"dest": {
"index": "foo_v2",
"pipeline": "transformFooBar"
}
}
Ah, I almost had the syntax right. The _source is not required:
// Create a pipeline with a SET processor
PUT _ingest/pipeline/transformFooBar
{
"processors": [
{
"set": {
"field": "Bar.Code",
"value": "{{Bar}}"
}
}
]
}
// Reindex using the above pipeline
POST _reindex
{
"source": {
"index": "foo_v1"
},
"dest": {
"index": "foo_v2",
"pipeline": "transformFooBar"
}
}

highlight does not properly work with exact phrase match search in elasticsearch

I am having problem with highlighting the exact phrase search. when the highlight comes back it highlights all the terms regardless of the exact search. see the example:
Put /test2
PUT /test2/all/1
{
"name" : "climate behaviour change"
}
PUT /test2/all/3
{
"name" : "climate behaviour change it is climate change"
}
and my mapping:
{
"test2": {
"mappings": {
"all": {
"properties": {
"name": {
"type": "string"
}
}
}
}
}
}
and when I run this query:
GET /test2/_search
{
"query": {
"query_string": {
"fields": ["name"],
"query": "\"climate change\""
}
}
,"highlight": {
"fields": {
"name": {"number_of_fragments": 0}
}
}
}
the result is
"hits": [
{
"_index": "test2",
"_type": "all",
"_id": "3",
"_score": 0.23013961,
"_source": {
"name": "climate behaviour change it is climate change"
},
"highlight": {
"name": [
"<em>climate</em> behaviour <em>change</em> it is <em>climate</em> <em>change</em>"
]
}
}
]
since I searched for exact climate change I would expect to have highlight for just the second part of the result, so climate behaviour change should not get highlighted. Does anyone know why this is like that?
or any other way I can get what I want?
By Default query_string uses OR operator see here
Rather use AND operator.
GET /test2/_search
{
"query": {
"query_string": {
"fields": ["name"],
"query": "\"climate change\"",
"default_operator": AND
}
}
,"highlight": {
"fields": {
"name": {"number_of_fragments": 0}
}
}
}

Unable to find a field mapper for field in nested query using field_value_factor

Here's the mapping:
PUT books-index
{
"mappings": {
"books": {
"properties": {
"tags": {
"type": "nested",
"fields": {
"name": {
"type": "string"
},
"weight": {
"type": "float"
}
}
}
}
}
}
}
Then doing a nested query using a field_value_factor fails with an error
GET books-index/books/_search
{
"query": {
"nested": {
"path": "tags",
"score_mode": "sum",
"query": {
"function_score": {
"query": {
"match": {
"tags.name": "world"
}
},
"field_value_factor": {
"field": "weight"
}
}
}
}
}
}
The error: "nested: ElasticsearchException[Unable to find a field mapper for field [weight]]"
Interestingly, if there's one book in the index with tags - there's no error and the query works well.
Why is this happening? how can I prevent the error when there are no books with tags in the index?
Any ideas?
Thank you!
P.S. There's also an issue on github for this: https://github.com/elastic/elasticsearch/issues/12871
it looks like your mapping is incorrect.
After PUTing the mapping you provided, try executing GET books-index/_mapping, It will show these results:
"books-index": {
"mappings": {
"books": {
"properties": {
"tags": {
"type": "nested"
}
}
}
}
}
It's missing name and weight! The problem with the mapping is that you used either you used fields instead of properties, or you forget to put in a second properties key.
I modified your mapping to reflect that you were looking for a nested name and type within tags, as it looks like that is what your query wants.
PUT books-index
{
"mappings": {
"books": {
"properties": {
"tags": {
"type": "nested",
"properties": { // <--- HERE!
"name": {
"type": "string"
},
"weight": {
"type": "float"
}
}
}
}
}
}
}

Resources