Skip to content

Commit 59db973

Browse files
検索調整
1 parent c42ebf9 commit 59db973

File tree

1 file changed

+44
-49
lines changed

1 file changed

+44
-49
lines changed

Frontend/related-viewer.php

Lines changed: 44 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -41,48 +41,52 @@
4141

4242
// $suggestions = [];
4343

44-
// "<title> <parent.title>" で検索
44+
// "<title> <parent.title> <tag1> <tag2> <tag3> ..."で検索
4545
// ただし, parent は rootではない
46-
$titleQuery = NotBlankText([$currentContent->title, basename($currentContent->path)]) .
46+
$query = NotBlankText([$currentContent->title, basename($currentContent->path)]) .
4747
(($parent === false || $parent->IsRoot()) ? '' : ' ' . NotBlankText([$parent->title, basename($parent->path)]) );
4848

49-
$titleSuggestions = SearchEngine\Searcher::Search($titleQuery);
50-
// Debug::Log($titleSuggestions);
51-
// score 0.5 未満 は除外
52-
foreach($titleSuggestions as $i => $suggestion){
53-
if($suggestion['score'] < 0.5){
54-
unset($titleSuggestions[$i]);
55-
}
56-
}
57-
// Debug::Log($workSuggestions);
58-
// $suggestions = array_merge($suggestions, $workSuggestions);
59-
60-
// "<tag1> <tag2> <tag3> ..." で検索
61-
$tagQuery = '';
6249
foreach($currentContent->tags as $tag){
6350
if(!in_array($tag, array('noindex', 'noindex-latest', '編集中', 'editing'))){
64-
$tagQuery .= $tag . ' ';
51+
$query .= ' ' . $tag;
6552
}
6653
}
67-
$tagSuggestions = SearchEngine\Searcher::Search($tagQuery);
6854

69-
// 全タグをAND検索したとき, score 0.3 未満のものは除外
70-
foreach($tagSuggestions as $i => $suggestion){
71-
if($suggestion['score'] < 0.3){
72-
unset($tagSuggestions[$i]);
55+
$suggestions = SearchEngine\Searcher::Search($query);
56+
57+
$terms = explode(' ', $query);
58+
$termCount = count($terms);
59+
for($i = $termCount - 1; $i >= 0; $i--){
60+
$terms[$i] = trim($terms[$i]);
61+
if(empty($terms[$i])){
62+
array_splice($terms, $i, 1);
7363
}
7464
}
75-
// $suggestions = array_merge($suggestions, $workSuggestions);
76-
77-
// // 重複を除外
78-
// $uniqueKeys = [];
79-
// foreach($suggestions as $i => $suggestion){
80-
// if(array_key_exists($suggestion['id'], $uniqueKeys)){
81-
// unset($suggestions[$i]);
82-
// continue;
83-
// }
84-
// $uniqueKeys[$suggestion['id']] = true;
85-
// }
65+
// Debug::Log(count($terms));
66+
// Debug::Log(0.7 / count($terms));
67+
// Debug::Log(count($terms));
68+
// Debug::Log(0.5 / (1+log10(count($terms))));
69+
// Debug::Log($suggestions);
70+
71+
// フィルタ例:
72+
// 0.3以上のもの:
73+
// 問題点:
74+
// termが増えるごとに, 限定されていき, ヒットしずらくなる
75+
//
76+
// 各termごと, 平均して7割以上類似するもの:
77+
// 0.7 / count($terms)
78+
// 問題点:
79+
// termが増えるごとに, scoreが0に近づき, ヒットしやすくなる
80+
//
81+
// 考えは, 上のままでtermsの数が増えるごとにスコアの下降を抑える
82+
// 0.5 / (1+log(count($terms)))
83+
//
84+
foreach($suggestions as $i => $suggestion){
85+
if($suggestion['score'] < (0.5 / (1+log(count($terms))))){
86+
unset($suggestions[$i]);
87+
}
88+
}
89+
8690
$childPathList = [];
8791
if($parent !== false){
8892
$childCount = $parent->ChildCount();
@@ -93,8 +97,8 @@
9397
}
9498
}
9599
// Debug::Log($childPathList);
96-
$titleSuggestions = SelectDifferentDirectoryContents($titleSuggestions, $currentContent->path, $childPathList);
97-
$tagSuggestions = SelectDifferentDirectoryContents($tagSuggestions, $currentContent->path, $childPathList);
100+
// $titleSuggestions = SelectDifferentDirectoryContents($titleSuggestions, $currentContent->path, $childPathList);
101+
$suggestions = SelectDifferentDirectoryContents($suggestions, $currentContent->path, $childPathList);
98102

99103
// End 関連コンテンツの検索 =================================================
100104

@@ -125,26 +129,17 @@
125129

126130
$body = '';
127131

128-
if(count($titleSuggestions) > 0){
129-
$body .= '<h3>タイトル「' . trim($titleQuery) . '」に関連する</h3>';
130-
$body .= CreateSuggestedContentList($titleSuggestions);
131-
}
132-
133-
if(count($tagSuggestions) > 0){
134-
$body .= '<h3>タグ「' . trim($tagQuery) . '」に関連する</h3>';
135-
$body .= CreateSuggestedContentList($tagSuggestions);
132+
if(count($suggestions) > 0){
133+
// $body .= '<h3>「' . trim($query) . '」に関連する</h3>';
134+
$body .= CreateSuggestedContentList($suggestions);
136135
}
137136

138137

139-
if(count($titleSuggestions) + count($tagSuggestions) > 0){
140-
$vars['contentSummary'] = '<p>「コンテンツ: <a href="' . CreateContentHREF($currentContent->path) . '">' .
141-
NotBlankText([$currentContent->title, basename($currentContent->path)]) .
142-
'</a>」と関連するコンテンツが, 別階層で<em>' . (count($titleSuggestions) + count($tagSuggestions)) .'件</em>見つかりました.</p>';
138+
if(count($suggestions) > 0){
139+
$vars['contentSummary'] = '<p><em>「' . trim($query) . '」</em>に関連するコンテンツが, 別階層で<em>' . count($suggestions) .'件</em>見つかりました.</p>';
143140
}
144141
else{
145-
$vars['contentSummary'] = '<p>「コンテンツ: <a href="' . CreateContentHREF($currentContent->path) . '">' .
146-
NotBlankText([$currentContent->title, basename($currentContent->path)]) .
147-
'</a>」と関連するコンテンツが, 別階層で見つかりませんでした.</p>';
142+
$vars['contentSummary'] = '<p><em>「' . trim($query) . '」</em>に関連するコンテンツが, 別階層で見つかりませんでした.</p>';
148143
}
149144

150145
$vars['contentBody'] = $body;

0 commit comments

Comments
 (0)