簡體   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