弹性搜索,是否可以在搜索结果集中获取特定文档的索引?

时间:2013-05-29 21:19:51

标签: elasticsearch

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

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

2 个答案:

答案 0 :(得分: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());      
}

答案 1 :(得分:0)

我遇到了非常相似的挑战。 我们必须弄清楚某些列表中的剪辑位置是什么。

它假设我们按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 !!!!
 */