繁体   English   中英

Elasticsearch bash脚本不起作用,但是如果我将其复制并粘贴到终端中,它将起作用

[英]Elasticsearch bash script not working but if I copy and paste in the terminal it works

我已经创建了一个bash脚本来测试索引,但是,与在终端中直接复制CURL命令相比,此bash脚本提供的结果有所不同。 与使用sh [file-name]启动代码相比,我得到的结果有所不同?会发生什么?

#!/bin/sh

alias curl="curl -s"


echo "Delete index"
curl -X DELETE "localhost:9200/products?pretty"
echo

echo "Create index"
curl -X POST "localhost:9200/products?pretty" -d '

{
  "products" : {
    "settings" : {
      "index" : {
        "analysis" : {
          "filter" : {
            "my_synonym" : {
              "ignore_case" : "true",
              "expand" : "true",
              "type" : "synonym",
              "synonyms" : [ "pote, foundation"]            }
          },
          "analyzer" : {
            "folding_analyzer" : {
              "filter" : [ "standard", "lowercase", "asciifolding", "my_synonym" ],
              "tokenizer" : "standard"
            }
          }
        },
        "number_of_shards" : "1",
        "number_of_replicas" : "0"
      }
    },
    "mappings" : {
      "product" : {
        "dynamic" : "false",
        "properties" : {
          "brand_name" : {
            "type" : "string",
            "index_options" : "offsets",
            "analyzer" : "folding_analyzer"
          },
          "product_name" : {
            "type" : "string",
            "index_options" : "offsets",
            "analyzer" : "folding_analyzer"
          }
        }
      }
    }
  }
}

'
echo

echo "Info index"
curl -XGET 'localhost:9200/products/_settings,_mappings?pretty'
echo

echo "Test doc:"
curl -X POST "localhost:9200/products/product/1?pretty" -d '{
    "product_name": "Foundation Brush",
    "brand_name": "Bobbi Brown"
}'
echo

echo "Test doc:"
curl -X POST "localhost:9200/products/product/2?pretty" -d '{
    "product_name": "Foundation Primer",
    "brand_name": "Laura Mercier"
}'
echo

echo "Test doc:"
curl -X POST "localhost:9200/products/product/3?pretty" -d '{
    "product_name": "Lock-It Tattoo Foundation",
    "brand_name": "Kat Von D"
}'
echo

echo "Test doc:"
curl -X POST "localhost:9200/products/product/4?pretty" -d '{
    "product_name": "Diorskin Airflash Spray Foundation",
    "brand_name": "Dior"
}'
echo

echo "Test doc:"
curl -X POST "localhost:9200/products/product/5?pretty" -d '{
    "product_name": "Diorskin Airflash Spray Lancôme",
    "brand_name": "Dior"
}'
echo


echo "Info index"
curl -XGET 'localhost:9200/products/_settings,_mappings?pretty'
echo

echo "Search all"
curl -X GET "localhost:9200/products/_search?pretty" -d '{
    "query": {
      "match_all": {}
    }
  }'
echo

elasticsearch中的索引文档无法立即用于搜索。 它们仅在刷新操作发生后才出现在搜索中。 默认情况下,此操作每1秒自动发生一次。 因此,当您一一粘贴命令时,它会在获取设置和映射时发生。

当您运行bash脚本时,根本没有时间进行刷新。 因此,您需要在最后一个index命令之后添加显式刷新自己:

curl -XPOST 'http://localhost:9200/products/_refresh?pretty'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM