你如何使用CURL正确地索引elasticsearch中的数据,找到它然后删除它?

时间:2015-12-02 21:22:47

标签: curl indexing elasticsearch

我已创建代码(在test.sh shell脚本中),尝试测试索引,搜索和删除数据到名为my_test_cluster的弹性搜索群集中:

  1. 输入两个类型" person" (简和约翰)进入一个名为megacorp的索引。 注意:我认为这是代码失败的地方。

  2. 我搜索所有记录,但我尝试索引的记录都没有显示出来。 结果:我没有看到我试图索引的两条记录,但我确实看到了elasticsearch(name = Leonus)实例附带的默认记录确实显示。

  3. 我专门搜索user = Jane,但没有任何内容。 结果:我没有看到user = Jane的记录。

  4. 我尝试通过ID = 1

  5. 删除user = Jane记录
  6. 我再次搜索所有记录,只看到user = Leonus的默认记录

  7. 我的代码如下:

    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"
    }
    

    非常感谢任何有关我做错事的帮助。 - 谢谢

1 个答案:

答案 0 :(得分:1)

您搜索所有记录的查询应该是其中之一(即您需要查询/_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'

<强>更新

我在这里回答你的评论,因为还有更多的空间:)

  1. 没有默认用户&#34; Leonus&#34;。在查询http://localhost:9200/时你看到的是Elasticsearch基本上说&#34;你好世界&#34;。 &#34; Leonus&#34;只是您节点的名称。如果您重新启动节点,则会看到另一个名称(more info)。

  2. 默认情况下每秒都会进行一次刷新,因此如果您在脚本编制索引后立即进行搜索,则只需要调用它。但是如果您索引文档并在一秒钟之后搜索它,则不需要刷新它。如果您不想明确刷新,则有两种选择:1)使脚本暂停一秒钟(例如sleep 1)或2)将刷新间隔设置为-1({{3 }})