簡體   English   中英

使用映射創建Elasticsearch索引

[英]Elasticsearch index creation with mapping

我正在努力完成索引創建的簡單任務,目標是使用分析器和字段映射創建索引。 當我使用分析器創建索引時,我可以通過analyze api調用與分析器通信,但是當我添加映射信息時,創建索引調用失敗,並且“找不到字段[$ field]]的Analyzer [analyzer1]]”,I創建了一個腳本來顯示問題:

    #!/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節點中。 有關構造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"
                }
            }
        }
    }
}';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM