【问题标题】:Elasticsearch bash script not working but if I copy and paste in the terminal it worksElasticsearch bash脚本不起作用,但如果我在终端中复制并粘贴它就可以了
【发布时间】:2015-12-15 07:16:57
【问题描述】:

我创建了一个 bash 脚本来测试我的索引,但是这个 bash 脚本给出的结果与我直接在终端中复制 CURL 命令不同。我检索到的结果与使用 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

【问题讨论】:

  • 你已经发布了很多 curl 命令。你能解释一下哪一个导致不同的输出,有什么区别吗?也许只选择一个 curl 请求作为示例。

标签: bash shell elasticsearch terminal sh


【解决方案1】:

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

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

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

【讨论】:

  • 这是真的,谢谢。除此之外,为什么我的sinonym过滤器不起作用?你有什么想法吗?
  • 是的,您不需要在创建索引时使用"products" : { .... } 包围您的设置和映射。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-10
  • 1970-01-01
  • 1970-01-01
  • 2021-01-20
  • 2019-09-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多