gpt4 book ai didi

使用映射创建 Elasticsearch 索引

转载 作者:行者123 更新时间:2023-11-29 02:44:21 26 4
gpt4 key购买 nike

我正在努力完成创建索引的简单任务,目标是创建一个带有分析器和字段映射的索引。当我使用分析器创建索引时,我可以通过分析 api 调用与分析器通信,但是当我添加映射信息时,创建索引调用失败并显示“找不到字段 [$field]] 的分析器 [analyzer1]”,我创建了一个脚本来显示问题:

    #!/bin/bash

INDEX_NAME="test1"

echo "delete index just to be sure"
curl -XDELETE "http://localhost:9200/$INDEX_NAME/"; echo

echo "create new index"
curl -X PUT "http://localhost:9200/$INDEX_NAME/" -d '{
"index":{
"analysis":{
"analyzer":{
"analyzer1":{
"type":"custom",
"tokenizer":"standard",
"filter":[ "standard", "lowercase", "stop", "kstem", "ngram" ]
}
},
"filter":{
"ngram":{
"type":"ngram",
"min_gram":2,
"max_gram":15
}
}
}
}
}'; echo

echo "analyze something with our shiny new analyzer"
curl -XGET "localhost:9200/$INDEX_NAME/_analyze?analyzer=analyzer1&pretty=true" -d 'abcd'

echo "remove the created index"
curl -XDELETE "http://localhost:9200/$INDEX_NAME/"; echo

echo "create new index again with mapping"
curl -X PUT "http://localhost:9200/$INDEX_NAME/" -d '{
"index":{
"analysis":{
"analyzer":{
"analyzer1":{
"type":"custom",
"tokenizer":"standard",
"filter":[ "standard", "lowercase", "stop", "kstem", "ngram" ]
}
},
"filter":{
"ngram":{
"type":"ngram",
"min_gram":2,
"max_gram":15
}
}
}
},
"mappings": {
"product": {
"properties": {
"title": {
"type": "string",
"search_analyzer" : "analyzer1",
"index_analyzer" : "analyzer1"
}
}
}
}
}'; echo

最佳答案

我认为您的问题是 analysis 设置需要嵌套在 JSON 中的 settings 节点中,而不是 index 节点中因为你有它。请引用Elasticsearch Create Index API有关构建 JSON 的详细信息。

因此,您的创建索引调用应如下所示:

curl -X PUT "http://localhost:9200/$INDEX_NAME/" -d '{
"settings":{
"analysis":{
"analyzer":{
"analyzer1":{
"type":"custom",
"tokenizer":"standard",
"filter":[ "standard", "lowercase", "stop", "kstem", "ngram" ]
}
},
"filter":{
"ngram":{
"type":"ngram",
"min_gram":2,
"max_gram":15
}
}
}
},
"mappings": {
"product": {
"properties": {
"title": {
"type": "string",
"search_analyzer" : "analyzer1",
"index_analyzer" : "analyzer1"
}
}
}
}
}';

关于使用映射创建 Elasticsearch 索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21876857/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com