簡體   English   中英

elasticsearch php搜索存在

[英]elasticsearch php search exists

一個人怎么可能以下請求

GET /giata_index/giata_type/_search/exists
{
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "status": 2
                    }
                },
                {
                    "term": {
                        "ids": "26744"
                    }
                }
            ]
        }
    }
}

與ElasticSearch的PHP庫一起使用?

我已經玩過了exist端點,但是事實證明,那只能檢查一個特定的uid是否存在。 所以我想我需要進行搜索。 但是我在搜索端點的白名單中找不到可以簡單檢查是否存在的參數。

我想避免獲取整個文檔而只問它是否存在的原因是因為我有成千上萬的導入,並且ES中的文檔數量也一樣多,所以我希望它投入很少的工作。盡可能。

注意:我也研究了可以通過HTTP請求實現的head請求(僅檢索文檔的標頭-200或404)。 但這可能僅適用於通過HTTP的請求。

如果情況變得更糟,我可以通過php進行卷發,而只需通過HTTP即可。 但是我寧願不這樣做。

似乎確實不存在端點優先搜索,但我認為您使用了一種簡單的替代方法:

使用一個空的“字段”數組。 並計算您的查詢結果。 如果== 0:否。 如果> 0:是

GET /giata_index/giata_type/_search
{
    "fields": [],
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "status": 2
                    }
                },
                {
                    "term": {
                        "ids": "26744"
                    }
                }
            ]
        }
    }
}

另一種替代方法是使用_count: https ://www.elastic.co/guide/en/elasticsearch/reference/1.6/search-count.html

使用最新的2.x版本應該可以。 代碼示例可能是這樣的:

$clientBuilder = Elasticsearch\ClientBuilder::create();
// Additional client options, hosts, etc.
$client = $clientBuilder->build();

$index = 'your_index';
$type = 'your_type';

$params = [
  'index' => $index,
  'type'  => $type,
  'body'  => [
    'query' => [
      'bool' => [
        'must' => [
          [
            'term' => [
              "status" => 2
            ]
          ],
          [
            'term' => [
              'ids' => "26744"
            ]
          ]
        ]
      ]
    ]
  ];

try {
  $client->searchExists($params);
} catch (Exception $e) {
  // Not found. You might want to return FALSE if wrapped in a function.
  // return FALSE;
}
// Found.

值得注意的是,如果搜索未包裝在try / catch塊中,則會中斷執行並引發異常(如果未找到狀態碼4xx)。 同樣,它不能在以后的模式中有效使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM