【问题标题】:Elasticsearch index creation with mapping使用映射创建 Elasticsearch 索引
【发布时间】:2014-02-19 09:54:07
【问题描述】:

我正在为创建索引的简单任务而苦苦挣扎,目标是使用分析器和字段映射创建索引。当我使用分析器创建索引时,我可以通过分析 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

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    我相信您的问题是 analysis 设置需要嵌套在 JSON 中的 settings 节点内,而不是像您拥有的那样嵌套在 index 节点内。有关构造 JSON 的详细信息,请参考 Elasticsearch Create Index API

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

    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"
                    }
                }
            }
        }
    }';
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-23
      • 2014-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-11
      • 1970-01-01
      相关资源
      最近更新 更多