Skip to content

Commit

Permalink
add site GET parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
cweiske committed Feb 5, 2016
1 parent 8dc6e4a commit f1a5d01
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1 +1,2 @@
/data/config.php
README.html
12 changes: 11 additions & 1 deletion README.rst
@@ -1,8 +1,18 @@
Features
========
- Crawler and indexer with the ability to run many in parallel
- Shows and highlights text that contains search words
- Boolean search queries:

- ``foo bar`` searches for ``foo AND bar``
- ``foo OR bar``
- ``title:foo`` searches for ``foo`` only in the page title
- Facets for tag, domain, language and type
- Site search

- Query: ``foo bar site:www.example.org/dir/``
- Query: ``foo bar site:example.org/dir/``
- or use the ``site`` GET parameter:
``/?q=foo&site=example.org/dir``

Dependencies
============
Expand Down
3 changes: 3 additions & 0 deletions data/templates/search.htm
Expand Up @@ -14,6 +14,9 @@
<form class="navbar-form pull-left">
<input type="text" name="q" placeholder="Search"
value="{{query}}" class="input-xxlarge"/>
{% if siteParam %}
<input type="hidden" name="site" value="{{site}}"/>
{% endif %}
<button type="submit" class="btn">Find</button>
</form>
</div>
Expand Down
12 changes: 9 additions & 3 deletions data/templates/search/list.htm
@@ -1,10 +1,16 @@
{% if hitcount == 0 %}
<p>
Sorry, no results for "<tt>{{query}}</tt>".
No results
for "<strong><tt>{{cleanQuery}}</tt></strong>"
{% if site %}
on <strong><tt>{{site}}</tt></strong>
&#160;&#160;&#160;|&#160;&#160;&#160;
<a href="{{urlNoSite}}">Show all results</a>
{% endif %}
</p>
{% else %}
<p class="resultinfo">
{{hitcount}} results
<p class="resultinfo">
{{hitcount}}
{% if hitcount == 1 %}result{% else %}results{%endif%}
for "<strong><tt>{{cleanQuery}}</tt></strong>"
{% if site %}
Expand Down
27 changes: 24 additions & 3 deletions www/index.php
Expand Up @@ -17,9 +17,23 @@
$page = (int)$_GET['page'] - 1;
}
$perPage = 10;//$GLOBALS['phinde']['perPage'];

$site = null;
$siteParam = false;
$baseLink = '?q=' . urlencode($query);

if (preg_match('#site:([^ ]*)#', $query, $matches)) {
$site = $matches[1];
$cleanQuery = trim(str_replace('site:' . $site, '', $query));
$site = Helper::noSchema($site);
} else if (isset($_GET['site']) && trim(isset($_GET['site'])) != '') {
$site = trim($_GET['site']);
$siteParam = true;
$cleanQuery = $query;
$baseLink .= '&site=' . urlencode($site);
} else {
$cleanQuery = $query;
}

$filters = array();
if (isset($_GET['filter'])) {
$allowedFilter = array('domain', 'language', 'tags', 'term');
Expand Down Expand Up @@ -54,7 +68,6 @@ function buildLink($baseLink, $filters, $addFilterType, $addFilterValue)
return $baseLink;
}

$site = null;
if (preg_match('#site:([^ ]*)#', $query, $matches)) {
$site = $matches[1];
$cleanQuery = trim(str_replace('site:' . $site, '', $query));
Expand All @@ -77,7 +90,8 @@ function buildLink($baseLink, $filters, $addFilterType, $addFilterValue)

foreach ($res->hits->hits as &$hit) {
$doc = $hit->_source;
if ($doc->title == '') {
if (!isset($doc->title) || $doc->title == '') {
$doc->title = '(no title)';
$doc->htmlTitle = '(no title)';
}
if (isset($hit->highlight->title[0])) {
Expand All @@ -104,6 +118,12 @@ function buildLink($baseLink, $filters, $addFilterType, $addFilterValue)
}
}

if ($site !== null) {
$urlNoSite = buildLink('?q=' . urlencode($cleanQuery), $filters, null, null);
} else {
$urlNoSite = null;
}

render(
'search',
array(
Expand All @@ -112,6 +132,7 @@ function buildLink($baseLink, $filters, $addFilterType, $addFilterValue)
'cleanQuery' => $cleanQuery,
'urlNoSite' => $urlNoSite,
'site' => $site,
'siteParam' => $siteParam,
'hitcount' => $res->hits->total,
'hits' => $res->hits->hits,
'aggregations' => $res->aggregations,
Expand Down

0 comments on commit f1a5d01

Please sign in to comment.