在Shell脚本中通过curl进行弹性搜索以添加设置和映射时出错

时间:2019-03-01 02:28:07

标签: shell elasticsearch curl

我正在尝试通过docker中的shell脚本初始化设置和映射。

#!/bin/sh

until $(curl --output /dev/null --silent --head --fail http://elasticsearch:9200); do
    printf '.'
    sleep 5
done

for i in {30..0}; do
    if curl elasticsearch:9200; then
        curl -XPUT 'elasticsearch:9200/x_product/' -H 'Content-Type: application/json'  -d '{
                "settings": {
                   "number_of_shards": 1,
                   "analysis": {
                        "filter": {
                            "ngram_filter": {
                                "type": "nGram",
                                "min_gram": 2,
                                "max_gram": 3
                            }
                        },
                        "analyzer": {
                            "ngram_analyzer": {
                            "type": "custom",
                            "tokenizer": "my_tokenizer",
                            "filter":  ["lowercase", "ngram_filter"]
                            }
                        }
                    }
                },
                "mappings" : {
                    "product": {
                        "name": {
                            "type": "string",
                            "include_in_all": true,
                            "term_vector": "yes",
                            "index_analyzer": "ngram_analyzer",
                            "search_analyzer": "standard"
                        },
                        "description_value": {
                            "type": "string",
                            "include_in_all": true,
                            "term_vector": "yes",
                            "index_analyzer": "ngram_analyzer",
                            "search_analyzer": "standard"
                        },
                        "barcode_value": {
                            "type": "string",
                            "include_in_all": true,
                            "term_vector": "yes",
                            "index_analyzer": "ngram_analyzer",
                            "search_analyzer": "standard"
                        },
                        "searchword_content": {
                            "type": "string",
                            "include_in_all": true,
                            "term_vector": "yes",
                            "index_analyzer": "ngram_analyzer",
                            "search_analyzer": "standard"
                        },
                        "discount": {
                            "type": "integer"
                        },
                        "datetime": {
                            "type": "date",
                            "format": "epoch_millis"
                        }
                    }
                }
            }';
            break;
    fi
    sleep 2
done

但是这种语法给我错误:

  

{“错误”:{“ root_cause”:[{“ type”:“ parse_exception”,“ reason”:“ Failed to   解析内容以映射“}],” type“:” parse_exception“,” reason“:”失败   将内容解析为   map“,” caused_by“:{” type“:” json_parse_exception“,” reason“:”意外   字符('}'(代码125)):期望双引号开头   名称\ n [来源:   org.elasticsearch.transport.netty4.ByteBufStreamInput@770de4a9;线:   19,列:22]“}},”状态“:400}

我的语法出了什么问题?

已更新: 1.更新我的语法更改,从“ lowercase到” lowercase“也从” type“更改为” type“,但是出现了另一个错误 2.在分析器上删除多余的逗号

1 个答案:

答案 0 :(得分:0)

除了语法问题之外,用于弹性搜索的json字段中也存在问题,因此我更改了一些字段和值,并且也将设置和映射分开以使其更易于阅读。

 curl -XPUT 'elasticsearch:9200/x_product/_settings' -H 'Content-Type: application/json'  -d '{
            "analysis": {
                "filter": {
                    "ngram_filter": {
                        "type": "nGram",
                        "min_gram": 2,
                        "max_gram": 3
                    }
                },
                "analyzer": {
                    "ngram_analyzer": {
                    "type": "custom",
                    "tokenizer": "standard",
                    "filter":  ["lowercase", "ngram_filter"]
                    }
                }
            }
        }';
        curl -XPUT 'elasticsearch:9200/x_product/_mapping/product' -H 'Content-Type: application/json'  -d '{
            "properties": {
                "name": {
                    "type": "text",
                    "term_vector": "yes",
                    "analyzer": "ngram_analyzer",
                    "search_analyzer": "standard"
                },
                "description_value": {
                    "type": "text",
                    "term_vector": "yes",
                    "analyzer": "ngram_analyzer",
                    "search_analyzer": "standard"
                },
                "barcode_value": {
                    "type": "text",
                    "term_vector": "yes",
                    "analyzer": "ngram_analyzer",
                    "search_analyzer": "standard"
                },
                "searchword_content": {
                    "type": "text",
                    "term_vector": "yes",
                    "analyzer": "ngram_analyzer",
                    "search_analyzer": "standard"
                },
                "discount": {
                    "type": "integer"
                },
                "datetime": {
                    "type": "date",
                    "format": "epoch_millis"
                }
            }
        }';