【问题标题】:Unable to create a working index for Elasticsearch 7无法为 Elasticsearch 7 创建工作索引
【发布时间】:2021-07-10 16:31:35
【问题描述】:

我确定这在某种程度上是重复的。但是我找到的每一个解决方案都没有帮助我。为了简单起见,我发布了非常少的数据示例。

我想要实现的是为嵌套表单对象添加一个映射来定义一个分析器来对这些对象的文本属性进行排序。我还找到了带有 _doc 键的示例。但我不明白为什么要使用它们。

{
  "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'

结果

{"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}

编辑

我根据我从 ESCoder 得到的答案更改了我的数据。它似乎不起作用。

我的索引的健康状况是黄色的。我不知道为什么。

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

索引的内容看起来也不正确。许多嵌套的“属性”键。

{
  "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" : {
...

编辑2

索引定义

{
  "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": {
    "nested": {
      "path": "article.form",
      "query": {
        "match": {
          "article.form.text": "foo"
        }
      }
    }
  }
}

删除文章

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

删除索引

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

我还不知道如何强制重新索引。

索引内容

$ 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"
        }
      }
    }
  }
}

【问题讨论】:

    标签: elasticsearch indexing


    【解决方案1】:
    1. Mapping types are removed in 7.x
    2. [标准] 令牌过滤器已被删除。
    3. 索引数据的映射未正确完成(注意映射中的articleform 部分)

    你需要修改你的索引映射为

    索引映射:

        {
      "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"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
    

    索引数据:

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

    搜索查询:

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

    搜索结果:

    "hits": [
          {
            "_index": "67111821",
            "_type": "_doc",
            "_id": "1",
            "_score": 0.6931471,
            "_source": {
              "article": {
                "form": [
                  {
                    "text": "foo",
                    "location": "somewhere"
                  },
                  {
                    "text": "bar",
                    "location": "somewhere else"
                  }
                ]
              }
            }
          }
        ]
    

    【讨论】:

    • 感谢您的帮助。我更改了映射和搜索查询。我仍然得到相同的结果:“无法创建查询:路径 [article.form] 下的 [nested] 嵌套对象不是嵌套类型”。这是我之前尝试过的众多变体之一。我不知道还有什么问题。您如何将每个文档加载到 Elasticsearch 中以及加载到哪个路径?也许问题就在那里。请参阅原始问题的底部。
    • 我删除了索引和文档并多次创建它们。结果相同。我不知道你怎么可能得到正确的结果。而且索引的内容看起来也不正确。我将使用更多信息编辑我的问题。
    • @MarcusHusar 您使用的是哪个版本的 ES?您是否在关注:删除索引 --> 使用新索引映射创建索引 --> 索引数据 --> 搜索查询?
    • @MarcusHusar 您能分享一下您现在正在使用的确切索引映射吗?
    • 我会将新数据作为 EDIT2 添加到我的问题中。
    【解决方案2】:

    首先非常感谢@ESCoder。

    这是我的问题的部分答案。我将索引放入错误的路径。也许是因为有时我不允许覆盖已经存在的索引。后来我学会了如何删除索引。我删除了排序字段的定义,因为它也不起作用,我收到了另一个很好的错误消息。

    错误:/test/article

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

    正确:/test

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

    新问题

    索引中的嵌套对象定义被识别。但是现在数据库认为我的数据实际上没有嵌套对象。

    测试数据

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

    错误信息

    {"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}
    

    编辑

    现在我知道了。什么地方出了错。我必须将我的测试文章放入 /test/_doc/anything

    错误

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

    正确

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

    现在一个非常小的索引可以工作了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-19
      • 1970-01-01
      • 1970-01-01
      • 2020-05-31
      • 1970-01-01
      • 2013-09-12
      • 2020-03-20
      • 1970-01-01
      相关资源
      最近更新 更多