4

我知道我想在我的结果集中显示的特定文档的 ID,但我不知道它将在结果的哪一页上。弹性搜索是否可以告诉它返回特定文档所在的页面?

我的猜测是这是不可能的。我目前的方法是在仅加载文档 ID 后运行查询,并为查询返回一个非常大的(全部)结果集。我在这个列表中找到了 id,然后我再次运行查询,加载我想在页面上显示的所有数据。如果可以避免,我宁愿不运行两次查询。

4

2 回答 2

2

我正在使用 JAVA API,我正在获取索引、类型、id 和源,如下所示。

SearchResponse response = client.prepareSearch().execute().actionGet();
SearchHit[] documents = response.getHits().getHits();
for(SearchHit document : documents)
{
    System.out.println("document index :: " + document.getIndex());
    System.out.println("document type :: " + document.getType());
    System.out.println("document id :: " + document.getId());
    System.out.println("document source JSON :: " + document.getSourceAsString());      
}
于 2013-05-30T05:30:17.957 回答
1

我也面临过非常相似的挑战。我们必须弄清楚某个列表中的剪辑位置是什么。

它假设我们score按文档排序id。如果没有排序依据id或其他任何东西作为第二个排序规则,它就不会以这种方式工作。

这是我们解决方案的 PHP 代码概述:

$clipScore = $this->getScore($clip);
$lowestPossibleHigherScore = $this->getLowestPossibleHigherScore($clipScore);

$countHigherScore = $this->countClipsWithMinScore($lowestPossibleHigherScore);
$countSameScoreAndHigherId = $this->countClipsWithMinScore($clipScore, $clip->getId());
$countHigherScoreAndHigherId = $countHigherScore;
if ($countHigherScore > 0) {
    $countHigherScoreAndHigherId = $this->countClipsWithMinScore($lowestPossibleHigherScore, $clip->getId());
}

$position = $countHigherScore + ($countSameScoreAndHigherId - $countHigherScoreAndHigherId);

return $position;

这里有一些说明为什么这样有效;)

/**
 * Considered test cases =D
 * 
 * | Position   | Score  | ID   |
 * | 0          | 4.0    | 3    | 
 * | 1          | 3.2    | 1    | 
 * | 2          | 3.1    | 6    | 
 * | 3          | 2.5    | 5    | 
 * | 4          | 2.5    | 4    | 
 * | 5          | 2.5    | 2    | 
 * | 6          | 1.2    | 7    | 
 * 
 * findPosition(ID = 4)
 * 
 * countAllMinScore(2.501) = 3 (A) // so it's best position is 3 (4th place)
 * countAllMinScoreAndBiggerId(2.5, 4) = 2 (C) // "two" of same score and higher ID
 * countAllMinScoreAndBiggerId(2.501, 4) = 1 (D) // "one" of higher score and higher ID
 * 
 * $position (how to get "4"?) = 3 (A) + 1 (C - D) ???  YES !!!!
 * 
 * // next case
 * findPosition(ID = 5)
 *
 * countAllMinScore(2.501) = 3 (A) 
 * countAllMinScoreAndBiggerId(2.5, 5) = 1 (C) 
 * countAllMinScoreAndBiggerId(2.501, 5) = 1 (D) 
 *
 * $position (how to get "3"?) = 3 (A) + 0 (C - D) = 3 ??? YES !!!!
 * 
 * // next case
 * findPosition(ID = 2)
 *
 * countAllMinScore(2.501) = 3 (A)
 * countAllMinScoreAndBiggerId(2.5, 2) = 5 (C)
 * countAllMinScoreAndBiggerId(2.501, 2) = 3 (D)
 *
 * $position (how to get "5"?) = 3 (A) + 2 (C - D) = 5 ??? YES !!!!
 *
 * /// next case
 * findPosition(ID = 3)
 *
 * countAllMinScore(4.001) = 0 (A)
 * countAllMinScoreAndBiggerId(4.0, 3) = 0 (C)
 * countAllMinScoreAndBiggerId(4.001, 3) = 0 (D)
 *
 * $position (how to get "0"?) = 0 (A) + 0 (C - D) = 0 ??? YES !!!!
 * 
 * /// next case
 * findPosition(ID = 7)
 *
 * countAllMinScore(1.201) = 6 (A)
 * countAllMinScoreAndBiggerId(1.2, 7) = 0 (C)
 * countAllMinScoreAndBiggerId(1.201, 7) = 0 (D)
 *
 * $position (how to get "6"?) = 6 (A) + 0 (C - D) = 6 ??? YES !!!!
 *
 *
 * /// next case
 *
 * | Position   | Score  | ID   |
 * | 0          | 4.0    | 3    |
 * | 1          | 4.0    | 1    |
 * | 2          | 3.1    | 6    |
 * | 3          | 2.5    | 5    |
 * | 4          | 2.5    | 4    |
 * | 5          | 2.5    | 2    |
 * | 6          | 1.2    | 7    |
 *
 * findPosition(ID = 3)
 *
 * countAllMinScore(4.001) = 0 (A)
 * countAllMinScoreAndBiggerId(4.0, 3) = 0 (C)
 * countAllMinScoreAndBiggerId(4.001, 3) = 0 (D)
 *
 * $position (how to get "0"?) = 0 (A) + 0 (C - D) = 0 ??? YES !!!!
 *
 * /// next case
 * findPosition(ID = 1)
 *
 * countAllMinScore(4.001) = 0 (A)
 * countAllMinScoreAndBiggerId(4.0, 1) = 1 (C)
 * countAllMinScoreAndBiggerId(4.001, 1) = 0 (D)
 *
 * $position (how to get "1"?) = 0 (A) + 1 (C - D) = 1 ??? YES !!!!
 */
于 2016-08-17T20:10:48.817 回答