-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathsuggestcomplete.php
More file actions
140 lines (136 loc) · 4.1 KB
/
suggestcomplete.php
File metadata and controls
140 lines (136 loc) · 4.1 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
require_once 'common.php';
require_once 'functions.php';
$docs = array();
$start =0;
$offset =10;
$current = 1;
$url = 'suggestcomplete.php';
$mis = array();
$suggest = false;
if (isset($_GET['query']) && trim($_GET['query']) != '') {
$query = trim($_GET['query']);
$indexes = 'simplecompletefull';
if(isset($_GET['start'])) {
$start = $_GET['start'];
$current = $start/$offset+1;
}
$stmt = $ln_sph->prepare("SELECT * FROM $indexes WHERE MATCH(:match) LIMIT $start,$offset OPTION ranker=sph04,field_weights=(title=100,content=1)");
$stmt->bindValue(':match', $query,PDO::PARAM_STR);
$stmt->execute();
$rows = $stmt->fetchAll();
$meta = $ln_sph->query("SHOW META")->fetchAll();
foreach($meta as $m) {
$meta_map[$m['Variable_name']] = $m['Value'];
}
$total_found = $meta_map['total_found'];
$total = $meta_map['total'];
$ids = array();
$tmpdocs = array();
if (count($rows)> 0) {
foreach ($rows as $v) {
$ids[] = $v['id'];
}
$q = "SELECT id,title ,content FROM docs WHERE id IN (" . implode(',', $ids) . ")";
foreach ($ln->query($q) as $row) {
$tmpdocs[$row['id']] = array('title' => $row['title'], 'content' => $row['content']);
}
foreach ($ids as $id) {
$docs[] = $tmpdocs[$id];
}
} else {
$words = array();
foreach($meta_map as $k=>$v) {
if(preg_match('/keyword\[\d+]/', $k)) {
preg_match('/\d+/', $k,$key);
$key = $key[0];
$words[$key]['keyword'] = $v;
}
if(preg_match('/docs\[\d+]/', $k)) {
preg_match('/\d+/', $k,$key);
$key = $key[0];
$words[$key]['docs'] = $v;
}
}
$suggest = MakePhaseSuggestion($words, $query, $ln_sph);
}
}
?>
<?php
$title = 'Demo autocomplete with correction suggestion';
include 'template/header.php';
?>
<div class="container">
<ul class="nav nav-pills">
<li><a href="index.php">Autocomplete on titles</a></li>
<li class="active"><a href="suggestcomplete.php">Autocomplete on
titles + suggestion</a></li>
<li><a href="qsuggest.php">Autocomplete on titles + CALL SUGGEST</a>
</li>
<li><a href="suggestcompleteexcerpts.php">Autocomplete on titles +
suggestion + excerpts</a></li>
</ul>
<header>
<h1>Demo autocomplete with correction suggestion</h1>
</header>
<div class="row">
<div class="span9">
<p>
Autocomplete is made using star on a titles index (with infixes).<br />Correction
suggest can be made inside a phrase , by trying to find a suggestion
for each wrong typed word or word with low hits.<br /> Trigrams are
used in the process and there are also sane checks ( based on length
and levenshtein algorithm). <br /> It does not guarantee the new
corrected phrase will do matching.This can be done by doing a query
with the new phrase.
</p>
<p>Start typing in the field below</p>
<div class="well form-search">
<form method="GET" action="" id="search_form">
<input type="text" class="input-large" name="query" id="suggest"
autocomplete="off" value="<?=isset($_GET['query'])?htmlentities($_GET['query']):''?>"> <input
type="submit" class="btn btn-primary" id="send" name="send"
value="Submit">
</form>
</div>
</div>
</div>
<?php if ($suggest): ?>
<div class="row">
<div class="span9">
<p>
Did you mean <i><a href="?query=<?= $suggest; ?>"><?= $suggest; ?> </a>
</i>?
</p>
</div>
</div>
<?php endif; ?>
<p class="lead">
<?php if(isset($total_found)):?>
Total found:<?=$total_found?>
<?php endif;?>
</p>
<div class="row"><div class="span" style="display: none;"></div>
<?php if (count($docs) > 0): ?>
<div class="span9"><?php include 'template/paginator.php';?></div>
<?php foreach ($docs as $doc): ?>
<div class="span9">
<div class="container">
<h3>
<?= $doc['title'] ?>
</h3>
<p>
<?= substr(strip_tags($doc['content']), 0, 500) . '...' ?>
</p>
</div>
</div>
<?php endforeach; ?>
<div class="span9"><?php include 'template/paginator.php';?></div>
<?php elseif (isset($_GET['query']) && $_GET['query'] != ''): ?>
<p class="lead">Nothing found!</p>
<?php endif; ?>
</div>
<?php
$ajax_url = 'ajax_suggest.php';
include 'template/footer.php';
?>