0

I’m sure this is somehow a duplicate. But every solution I found didn’t help me out. To make things simple I post very minimal examples of my data.

What I want to achieve is to add a mapping for nested form objects and to define an analyzer for sorting the text properties of these objects. I also found examples with _doc keys. But I don’t understand why I should use them.

{
  "settings": {
    "analysis": {
      "analyzer": {
        "asciifolding": {
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "asciifolding"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": { 
      "article": {
        "properties": {
          "form": {
            "type": "nested",
            "properties": {
              "text": {
                "type": "text",
                "analyzer": "asciifolding",
                "fields": {
                  "sort": {
                    "type": "icu_collation_keyword",
                    "index": false,
                    "language": "it",
                    "country": "IT",
                    "variant": "@collation=standard"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

article_1.json

{
  "article": {
    "form": [
      {
        "text": "foo",
        "location": "somewhere"
      },
      {
        "text": "bar",
        "location": "somewhere else"
      }
    ]
  }
}

test_index.json

{
  "settings": {
    "index.mapping.single_type": true,
    "analysis": {
      "analyzer": {
        "asciifolding": {
          "tokenizer": "standard",
          "filter": ["standard", "lowercase", "asciifolding"]
        }
      }
    }
  },
  "mappings": {
    "article": {
      "properties": {
        "form": {
          "type": "nested",
          "properties": {
            "text": {
              "type": "text",
              "analyzer": "asciifolding",
              "fields": {
                "sort": {answer
                  "type": "icu_collation_keyword",
                  "index": false,
                  "language": "it",
                  "country": "IT",
                  "variant": "@collation=standard"
                }
              }
            }
          }
        }
      }
    }
  }
}

test_query.json

{
  "query" : {
    "nested" : {
      "path": "article.form",
      "query": {
        "match": { "form.text": "foo" }
      }
    }
  }
}
$ curl -X PUT -H 'Content-Type: application/json' -T article_1.json 'http://localhost:9200/test/article/article_1'
$ curl -X PUT -H 'Content-Type: application/json' -T test_index.json 'http://localhost:9200/test/article/'
$ curl -X GET -H 'Content-Type: application/json' -T test_query.json 'http://localhost:9200/test/article/_search'

Result

{"error":{"root_cause":[{"type":"query_shard_exception","reason":"failed to create query: [nested] nested object under path [article.form] is not of nested type","index_uuid":"AOi0L14-Q3mOo2YJqvAnjA","index":"test"}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"test","node":"UdlgcachRGih0q4bFwQcNg","reason":{"type":"query_shard_exception","reason":"failewilld to create query: [nested] nested object under path [article.form] is not of nested type","index_uuid":"AOi0L14-Q3mOo2YJqvAnjA","index":"test","caused_by":{"type":"illegal_state_exception","reason":"[nested] nested object under path [article.form] is not of nested type"}}}]},"status":400}

EDIT

I changed my data according to the answer I got from ESCoder. It doesn’t seem to work.

The health of my index is yellow. I don’t know why.

curl 'localhost:9200/_cat/indices?v'
health status index uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   test  2bwqzUFHSdWerqcMTvekkw   1   1          2            0     15.1kb         15.1kb

The contents of the index look incorrect, too. Lots of nested 'properties' keys.

{
  "test" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "article" : {
          "properties" : {
            "form" : {
              "properties" : {
                "location" : {
                  "type" : "text",
                  "fields" : {
                    "keyword" : {
                      "type" : "keyword",
                      "ignore_above" : 256
                    }
                  }
                },
                "text" : {
                  "type" : "text",
                  "fields" : {
                    "keyword" : {
                      "type" : "keyword",
                      "ignore_above" : 256
                    }
                  }
                }
              }
            }
          }
        },
        "mappings" : {
          "properties" : {
            "properties" : {
              "properties" : {
                "article" : {
                  "properties" : {
                    "properties" : {
                      "properties" : {
                        "form" : {
                          "properties" : {
                            "properties" : {
...

EDIT2

Index definition

{
  "settings": {
    "analysis": {
      "analyzer": {
        "asciifolding": {
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "asciifolding"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": { 
      "article": {
        "properties": {
          "form": {
            "type": "nested",
            "properties": {
              "text": {
                "type": "text",
                "analyzer": "asciifolding",
                "fields": {
                  "sort": {
                    "type": "icu_collation_keyword",
                    "index": false,
                    "language": "it",
                    "country": "IT",
                    "variant": "@collation=standard"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

Query

{
  "query": {
    "nested": {
      "path": "article.form",
      "query": {
        "match": {
          "article.form.text": "foo"
        }
      }
    }
  }
}

To delete articles

curl -X POST -H 'Content-Type: application/json' "http://localhost:9200/test/_delete_by_query" -d'
{
     "query":{
          "match_all":{}
      }
}'

To delete the index

curl -X DELETE 'http://localhost:9200/test'

I don’t know how to force a re-index, yet.

Contents of the index

$ curl -X GET 'http://localhost:9200/test?pretty'

{
  "test" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "mappings" : {
          "properties" : {
            "properties" : {
              "properties" : {
                "article" : {
                  "properties" : {
                    "properties" : {
                      "properties" : {
                        "form" : {
                          "properties" : {
                            "properties" : {
                              "properties" : {
                                "text" : {
                                  "properties" : {
                                    "type" : {
                                      "type" : "text",
                                      "fields" : {
                                        "keyword" : {
                                          "type" : "keyword",
                                          "ignore_above" : 256
                                        }
                                      }
                                    }
                                  }
                                }
                              }
                            },
                            "type" : {
                              "type" : "text",
                              "fields" : {
                                "keyword" : {
                                  "type" : "keyword",
                                  "ignore_above" : 256
                                }
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "number_of_shards" : "1",
        "provided_name" : "test",
        "creation_date" : "1618506388668",
        "number_of_replicas" : "1",
        "uuid" : "1OY-3mJYTOyaIi8cch5cLQ",
        "version" : {
          "created" : "7120099"
        }
      }
    }
  }
}

2 Answers 2

1
  1. Mapping types are removed in 7.x
  2. The [standard] token filter has been removed.
  3. Mapping for the index data is not done correctly ( note the article and form section in mapping)

You need to modify your index mapping as

Index Mapping:

    {
  "settings": {
    "analysis": {
      "analyzer": {
        "asciifolding": {
          "tokenizer": "standard",
          "filter": [
            "standard",
            "lowercase",
            "asciifolding"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": { 
      "article": {                     // note this
        "properties": {
          "form": {
            "type": "nested",
            "properties": {
              "text": {
                "type": "text",
                "analyzer": "asciifolding",
                "fields": {
                  "sort": {
                    "type": "icu_collation_keyword",
                    "index": false,
                    "language": "it",
                    "country": "IT",
                    "variant": "@collation=standard"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

Index Data:

{
  "article": {
    "form": [
      {
        "text": "foo",
        "location": "somewhere"
      },
      {
        "text": "bar",
        "location": "somewhere else"
      }
    ]
  }
}

Search Query:

{
  "query": {
    "nested": {
      "path": "article.form",
      "query": {
        "match": {
          "article.form.text": "foo"   // note this
        }
      }
    }
  }
}

Search Result:

"hits": [
      {
        "_index": "67111821",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.6931471,
        "_source": {
          "article": {
            "form": [
              {
                "text": "foo",
                "location": "somewhere"
              },
              {
                "text": "bar",
                "location": "somewhere else"
              }
            ]
          }
        }
      }
    ]
Sign up to request clarification or add additional context in comments.

8 Comments

Thank you for your help. I changed the mapping and the search query. I still get the same result: "failed to create query: [nested] nested object under path [article.form] is not of nested type". This is one of the many variants I tried before. I don’t know what else could go wrong. How do you load each document into Elasticsearch and to which path? Maybe the problem is there. See bottom of original question.
I deleted the index and the document and created them again multiple times. Same result. I don’t know how it is possible that you get a proper result. And the contents of the index don’t look correct, too. I will edit my question with more information.
@MarcusHusar which version of ES are you using? Are you following this : Delete index --> Create index with new index mapping --> Index data --> Search query ?
@MarcusHusar can you please share the exact index mapping that now you are using ?
I will add the new data to my question as EDIT2.
|
0

First a big thank you to @ESCoder.

Here is a partial answer to my problem. I put the index into the wrong path. Maybe because I wasn’t allowed to overwrite an already existent index at some time. I learned later how to delete an index. And I removed the definition of the sort field because it doesn’t work either, and I get another nice error message.

wrong: /test/article

curl -X PUT -H 'Content-Type: application/json' -T test_index.json 'http://localhost:9200/test/article'

correct: /test

curl -X PUT -H 'Content-Type: application/json' -T test_index.json 'http://localhost:9200/test'

New problem

The nested object definition in the index is recognized. But now the database thinks my data has actually no nested objects.

Test data

{
  "article": {
    "form": [
      {
        "text": "foo",
        "location": "somewhere"
      },
      {
        "text": "bar",
        "location": "somewhere else"
      }
    ]
  }
}

Error message

{"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"cannot change object mapping from nested to non-nested"}],"type":"illegal_argument_exception","reason":"cannot change object mapping from nested to non-nested"},"status":400}

EDIT

Now I know. what went wrong. I have to put my test article into /test/_doc/anything

wrong

url -X PUT -H 'Content-Type: application/json' -T test_article.json 'http://localhost:9200/test/article/1'

correct

url -X PUT -H 'Content-Type: application/json' -T test_article.json 'http://localhost:9200/test/_doc/1'

Now a very minimal index works.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.