简体   繁体   English

CURL 不适用于 docker-compose、Elasticsearch 和 Windows10

[英]CURL not working well with docker-compose, Elasticsearch and Windows 10

My docker-compose.yml file is listed below.下面列出了我的 docker-compose.yml 文件。 This is what I want:这就是我要的:

  • Spin up an Elasticsearch instance through docker-compose通过 docker-compose 启动 Elasticsearch 实例
  • Run a setup file in the container of the Elasticsearch image在 Elasticsearch 镜像的容器中运行设置文件
  • The setup file should be 'copied' from my host machine (so using volumes)安装文件应该从我的主机“复制”(所以使用卷)

This is what I got so far:这是我到目前为止得到的:

version: '3'

services:
  b-elastic:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.11.1
    container_name: b-elastic
    environment:
      - discovery.type=single-node
    volumes:
      - ./dockerelastic:/usr/share/elasticsearch/data
      - ./:/project
    ports:
      - 9200:9200 

I can verify that the setupfile is present:我可以验证 setupfile 是否存在:

$ docker exec -it b-elastic vi /project/database/elasticsearch/setup.json

gives:给出:

{
    "settings": {
        "index" : {
            "max_ngram_diff": "3"
        },
        "analysis": {
            "analyzer": {
                "username_analyzer": {
                    "tokenizer": "username_tokenizer",
                    "filter": [
                        "lowercase"
                    ]
                }
            },
            "tokenizer": {
                "username_tokenizer": {
                    "type": "ngram",
                    "min_gram": "1",
                    "max_gram": "4"
                }
            }
        }
    },
    "mappings": {
        "properties": {
            "_all" : { "enabled" : false },
            "username": {
                "type": "text",
                "analyzer": "username_analyzer"
            }
        }
    }
}

Now I want to execute the setup.json on Elasticsearch:现在我想在 Elasticsearch 上执行 setup.json:

$ docker exec -it b-elastic curl -X PUT http://localhost:9200/users -H  "Content-Type: application/json" --data-binary @project/database/elasticsearch/setup.json

This is what Docker returns:这是 Docker 返回的内容:

Warning: Couldn't read data from file
Warning: "project/database/elasticsearch/setup.json", this makes an empty
Warning: POST.
{"acknowledged":true,"shards_acknowledged":true,"index":"users"}

It looks like it is acknowledged, but when I try to search for users, I get this error:看起来它已被确认,但是当我尝试搜索用户时,我收到此错误:

[match] analyzer [username_analyzer] not found

I don't get this error when copy paste the contents of setup.json in Postman and execute the request there.将 setup.json 的内容复制粘贴到 Postman 并在那里执行请求时,我没有收到此错误。

How can I execute a file through a volume in an Elasticsearch container on Windows?如何通过 Windows 上的 Elasticsearch 容器中的卷执行文件?

Edit: The accepted answer works, but note that it failed for me when executing the command with PowerShell.编辑:接受的答案有效,但请注意,在使用 PowerShell 执行命令时它失败了。 Command Prompt worked.命令提示符有效。

Looks like curl wasn't able to read your setup.json file, so it created an empty index.看起来 curl 无法读取您的 setup.json 文件,因此它创建了一个空索引。
Notice that when you are using vi, you are using the absolute path (/ in the start), but when you are using curl you didn't.请注意,当您使用 vi 时,您使用的是绝对路径(/ 在开头),但是当您使用 curl 时,您没有使用。 Try尝试

docker exec -it b-elastic curl -X PUT http://localhost:9200/users -H "Content-Type: application/json" --data-binary @/project/database/elasticsearch/setup.json

This instead and see if it works这不是,看看它是否有效

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

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