dongyi1748 2017-09-04 10:49
浏览 24
已采纳

ElasticSearch中的0次点击来自PHP的curl,所有命中都来自命令行

I run the following PHP:

function curl_delete($url)
{
    $ch = curl_init();
    curl_setopt_array($ch, 
    [
        CURLOPT_CUSTOMREQUEST => 'DELETE',
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true
    ]);
    $response = curl_exec($ch);
    curl_close($ch);
    return $response;
}

function curl_put($url, $data)
{
    $ch = curl_init();
    curl_setopt_array($ch, 
    [
        CURLOPT_CUSTOMREQUEST => 'PUT',
        CURLOPT_URL => $url,
        CURLOPT_HTTPHEADER =>
        [
            'Content-Type: application/json',
            'Content-Length: ' . strlen($data),
        ],
        CURLOPT_POSTFIELDS => $data,
        CURLOPT_RETURNTRANSFER => true
    ]);
    $response = curl_exec($ch);
    curl_close($ch);
    return $response;
}

function curl_post($url, $data)
{
    $ch = curl_init();
    curl_setopt_array($ch, 
    [
        CURLOPT_POST => true,
        CURLOPT_URL => $url,
        CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
        CURLOPT_POSTFIELDS => $data,
        CURLOPT_RETURNTRANSFER => true
    ]);
    $response = curl_exec($ch);
    curl_close($ch);
    return $response;
}

echo curl_delete('localhost:9200/my_index?pretty');
echo curl_put('localhost:9200/my_index?pretty', '{"settings": {"number_of_shards": 1}}');
echo curl_post('localhost:9200/my_index/my_type/_bulk?pretty', '
{ "index": { "_id": 1 }}
{ "title": "The quick brown fox" }
{ "index": { "_id": 2 }}
{ "title": "The quick brown fox jumps over the lazy dog" }
{ "index": { "_id": 3 }}
{ "title": "The quick brown fox jumps over the quick dog" }
{ "index": { "_id": 4 }}
{ "title": "Brown fox brown dog" }
');
echo curl_post('localhost:9200/my_index/my_type/_refresh?pretty', '{}');
echo curl_post('localhost:9200/my_index/my_type/_search?pretty', '{}');

and I get the following output with no hits:

{
  "acknowledged" : true
}
{
  "acknowledged" : true,
  "shards_acknowledged" : true
}
{
  "took" : 92,
  "errors" : false,
  "items" : [
    {
      "index" : {
        "_index" : "my_index",
        "_type" : "my_type",
        "_id" : "1",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "created" : true,
        "status" : 201
      }
    },
    {
      "index" : {
        "_index" : "my_index",
        "_type" : "my_type",
        "_id" : "2",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "created" : true,
        "status" : 201
      }
    },
    {
      "index" : {
        "_index" : "my_index",
        "_type" : "my_type",
        "_id" : "3",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "created" : true,
        "status" : 201
      }
    },
    {
      "index" : {
        "_index" : "my_index",
        "_type" : "my_type",
        "_id" : "4",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "created" : true,
        "status" : 201
      }
    }
  ]
}
{
  "_index" : "my_index",
  "_type" : "my_type",
  "_id" : "_refresh",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "created" : true
}
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "failed" : 0
  },
  "hits" : {
    "total" : 0,
    "max_score" : null,
    "hits" : [ ]
  }
}

But if afterwards I run the same empty query from the command line:

"d:\Program Files\curl\bin\curl.exe" -XPOST localhost:9200/my_index/my_type/_search?pretty" -H "Content-Type: application/json" -d"{}"

I get all the hits as I'm supposed to:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "failed" : 0
  },
  "hits" : {
    "total" : 4,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "my_index",
        "_type" : "my_type",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "title" : "The quick brown fox"
        }
      },
      {
        "_index" : "my_index",
        "_type" : "my_type",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "title" : "The quick brown fox jumps over the lazy dog"
        }
      },
      {
        "_index" : "my_index",
        "_type" : "my_type",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "title" : "The quick brown fox jumps over the quick dog"
        }
      },
      {
        "_index" : "my_index",
        "_type" : "my_type",
        "_id" : "4",
        "_score" : 1.0,
        "_source" : {
          "title" : "Brown fox brown dog"
        }
      }
    ]
  }
}

Can someone please tell me what's wrong with my last line of PHP? Why is it getting no hits?

  • 写回答

1条回答 默认 最新

  • dongqi1245 2017-09-04 10:58
    关注

    When running the bulk query, you can see that all documents were created. However, the index is not yet refreshed, hence why you don't get any hits when running a search immediately.

    What you can do is to call refresh just before the search, like this:

    echo curl_post('localhost:9200/my_index/_refresh', '{}');
    

    Or simply add a refresh parameter to your bulk call like this:

    echo curl_post('localhost:9200/my_index/my_type/_bulk?pretty=true&refresh=true', ...
    

    And then you can issue your search query normally:

    echo curl_post('localhost:9200/my_index/my_type/_search?pretty', '{}');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图2.0 版本点聚合中Marker的位置无法实时更新,如何解决呢?
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题