Skip to main content
summaryrefslogtreecommitdiffstats
blob: 248239f5b582f8503cbc681111f2b580f6c46a3e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*******************************************************************************
 * Copyright (c) 2010, 2016 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.ua.tests.help.search;

import static org.junit.Assert.assertEquals;

import java.util.ArrayList;
import java.util.Arrays;

import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.help.internal.base.BaseHelpSystem;
import org.eclipse.help.internal.search.ISearchQuery;
import org.eclipse.help.internal.search.SearchHit;
import org.eclipse.help.internal.search.SearchQuery;
import org.eclipse.help.internal.search.SearchResults;
import org.junit.Test;

public class SearchRanking {

	public SearchHit[] findHits(String searchWord) {
		ISearchQuery query = new SearchQuery(searchWord, false, new ArrayList<String>(), Platform.getNL());
		SearchResults collector = new SearchResults(null, 10, Platform.getNL());
		BaseHelpSystem.getSearchManager().search(query, collector, new NullProgressMonitor());
		return collector.getSearchHits();
	}

	/**
	 * Verify that a match in a title has more weight than a match in the body
	 */
	@Test
	public void testTitleBoost1() {
		SearchHit[] hits = SearchTestUtils.getSearchHits("mjuhgt", "en");
		Arrays.sort(hits);
		assertEquals(2, hits.length);
		assertEquals("/org.eclipse.ua.tests/data/help/search/extraDir/ranking/ranktest1b.htm", getPath(hits[0]));
		assertEquals("/org.eclipse.ua.tests/data/help/search/extraDir/ranking/ranktest1a.htm", getPath(hits[1]));
	}

	@Test
	public void testTitleBoost2() {
		SearchHit[] hits = SearchTestUtils.getSearchHits("odrgtb", "en");
		Arrays.sort(hits);
		assertEquals(2, hits.length);
		assertEquals("/org.eclipse.ua.tests/data/help/search/extraDir/ranking/ranktest1a.htm", getPath(hits[0]));
		assertEquals("/org.eclipse.ua.tests/data/help/search/extraDir/ranking/ranktest1b.htm", getPath(hits[1]));
	}

	/**
	 * Verify that consecutive words raise the weight
	 */
	@Test
	public void testConsecutiveWords1() {
		SearchHit[] hits = SearchTestUtils.getSearchHits("iduhnf xaqsdab", "en");
		Arrays.sort(hits);
		assertEquals(2, hits.length);
		assertEquals("/org.eclipse.ua.tests/data/help/search/extraDir/ranking/ranktest2b.htm",
				getPath(hits[0]));
		assertEquals("/org.eclipse.ua.tests/data/help/search/extraDir/ranking/ranktest2a.htm",
				getPath(hits[1]));
	}

	@Test
	public void testConsecutiveWords2() {
		SearchHit[] hits = SearchTestUtils.getSearchHits("xaqsdab iduhnf", "en");
		Arrays.sort(hits);
		assertEquals(2, hits.length);
		assertEquals("/org.eclipse.ua.tests/data/help/search/extraDir/ranking/ranktest2a.htm",
				getPath(hits[0]));
		assertEquals("/org.eclipse.ua.tests/data/help/search/extraDir/ranking/ranktest2b.htm",
				getPath(hits[1]));
	}


	private String getPath(SearchHit hit) {
		String href = hit.getHref();
		int query = href.indexOf('?');
		return query < 0 ? href : href.substring(0, query);
	}

}

Back to the top