我创建了代码(在 test.sh
shell 脚本中),它试图测试索引、搜索和删除数据到名为 my_test_cluster
的 Elasticsearch 集群中。 :
megacorp
的索引中.注意:我相信这是代码失败的地方。
结果:我没有看到我尝试索引的两条记录,但我确实看到了 elasticsearch 实例(名称=Leonus)附带的默认记录。
结果:我没有看到 user=Jane 的记录。
我的代码如下:
echo ""
echo "------------------------------------------------------"
echo "PUT Employees into megacorp index."
echo "------------------------------------------------------"
curl -XPUT 'http://localhost:9200/megacorp/employee/1' -d '{
"first_name" : "Jane",
"last_name" : "Doe",
"age" : 25,
"about" : "I love to go rock climbing and write music.",
"interests": [ "sports", "music" ]
}'
curl -XPUT 'http://localhost:9200/megacorp/employee/2' -d '{
"first_name" : "John",
"last_name" : "Smith",
"age" : 30,
"about" : "I love to go rock climbing and cooking.",
"interests": [ "sports", "cooking" ]
}'
echo ""
echo ""
echo "------------------------------------------------------"
echo "Search all records"
echo "------------------------------------------------------"
curl -i -XGET 'http://localhost:9200/'
echo ""
echo ""
echo "------------------------------------------------------"
echo "Search specifically for user = Jane"
echo "------------------------------------------------------"
curl -XGET 'http://localhost:9200/megacorp/employee/_search?q=user:Jane'
echo ""
echo ""
echo "------------------------------------------------------"
echo "Delete employee record ID = 1'"
echo "------------------------------------------------------"
curl -XDELETE 'http://localhost:9200/megacorp/employee/1'
echo ""
echo ""
echo "------------------------------------------------------"
echo "Search all"
echo "------------------------------------------------------"
curl -i -XGET 'http://localhost:9200/'
echo ""
echo ""
当我运行脚本
. test.sh
,我得到以下结果...------------------------------------------------------
PUT Employees into megacorp index.
------------------------------------------------------
{"_index":"megacorp","_type":"employee","_id":"1","_version":1,"_shards":{"total":2,"successful":1,"failed":0},"created":true}{"_index":"megacorp","_type":"employee","_id":"2","_version":5,"_shards":{"total":2,"successful":1,"failed":0},"created":false}
------------------------------------------------------
Search all records
------------------------------------------------------
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Content-Length: 317
{
"name" : "Leonus",
"cluster_name" : "my_test_cluster",
"version" : {
"number" : "2.1.0",
"build_hash" : "72cd1f1a3eee09505e036106146dc1949dc5dc87",
"build_timestamp" : "2015-11-18T22:40:03Z",
"build_snapshot" : false,
"lucene_version" : "5.3.1"
},
"tagline" : "You Know, for Search"
}
------------------------------------------------------
Search specifically for user = Jane
------------------------------------------------------
{"took":7,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}
------------------------------------------------------
Delete employee record ID = 1'
------------------------------------------------------
{"found":true,"_index":"megacorp","_type":"employee","_id":"1","_version":2,"_shards":{"total":2,"successful":1,"failed":0}}
------------------------------------------------------
Search all
------------------------------------------------------
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Content-Length: 317
{
"name" : "Leonus",
"cluster_name" : "my_test_cluster",
"version" : {
"number" : "2.1.0",
"build_hash" : "72cd1f1a3eee09505e036106146dc1949dc5dc87",
"build_timestamp" : "2015-11-18T22:40:03Z",
"build_snapshot" : false,
"lucene_version" : "5.3.1"
},
"tagline" : "You Know, for Search"
}
非常感谢对我做错的任何帮助。 - 谢谢
最佳答案
您搜索所有记录的查询应该是其中之一(即您需要查询 /_search
endpoint 而不是根 /
,它只是告诉您有关 ES 安装的一些详细信息):
curl -i -XGET 'http://localhost:9200/megacorp/employee/_search'
curl -i -XGET 'http://localhost:9200/megacorp/_search'
curl -i -XGET 'http://localhost:9200/_search'
您将在索引它们后看到您的两条记录。如果没有,您可能需要调用
_refresh
在索引之后和搜索之前:curl -XPOST 'http://localhost:9200/megacorp/_refresh'
为了搜索 Jane,您需要使用正确的字段(
first_name
而不是 user
),即curl -XGET 'http://localhost:9200/megacorp/employee/_search?q=first_name:jane'
您也可以在不指定字段的情况下进行搜索(搜索将在 special field called
_all
上完成):curl -XGET 'http://localhost:9200/megacorp/employee/_search?q=jane'
更新
我在这里回答你的评论,因为还有更多空间:)
http://localhost:9200/
时看到的内容Elasticsearch 基本上是在说“hello world”。 “Leonus”只是您节点的名称。如果您重新启动节点,您将看到另一个名称 (more info)。 sleep 1
)或 2)将刷新间隔设置为 -1(more info)关于curl - 您如何正确索引 elasticsearch 中的数据,找到它,然后删除它,所有这些都使用 CURL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34053254/