简体   繁体   中英

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

I have creating a bash script to test my index, however this bash script it gives different results than if I copy directly the CURL command in my terminal . I retrieve different result than if I launch my code with sh [file-name] What can be happening?

#!/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

Indexed documents in elasticsearch are not immediately available for searching. They only show up in search after refresh operation takes place. By default this operation occurs automatically every 1sec. So when you paste commands one by one it occurs while you are getting settings and mappings.

When you run bash script there is simply no time for refresh to take place. So, you need to add explicit refresh yourself after the last index command:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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