search pass two

This commit is contained in:
Matthew Scragg 2014-11-11 17:06:28 -06:00
parent 312c61ef61
commit 13d4be8937
7 changed files with 63 additions and 11 deletions

View file

@ -10,13 +10,28 @@ if [ -d "/vagrant" ]; then
fi
echo "Provisioning..."
if ! type "add-apt-repository" > /dev/null; then
sudo apt-get update
sudo apt-get install -y software-properties-common python-software-properties
fi
# Elastic Search
wget -qO - http://packages.elasticsearch.org/GPG-KEY-elasticsearch | sudo apt-key add -
echo 'deb http://packages.elasticsearch.org/elasticsearch/1.4/debian stable main' | sudo tee /etc/apt/sources.list.d/elastic.list
sudo add-apt-repository -y ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install -y python build-essential pkg-config git \
python-pip python-virtualenv python-dev zlib1g-dev \
libffi-dev libyaml-dev libssl-dev nodejs
libffi-dev libyaml-dev libssl-dev nodejs openjdk-7-jre-headless elasticsearch
# Create swap file because ES eats up RAM and 14.04 doesn't have swap by default
sudo fallocate -l 1G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
# lxml deps
# libxml2-dev libxslt1-dev
@ -45,7 +60,6 @@ sudo npm install -g bower
cd /home/vagrant
virtualenv .venv
source .venv/bin/activate
@ -64,4 +78,6 @@ sudo mv /tmp/realms-wiki /usr/local/bin
sudo chmod +x /usr/local/bin/realms-wiki
sudo service elasticsearch start
realms-wiki start

View file

@ -3,11 +3,13 @@ from realms.modules.search.models import Search
@Wiki.after('write_page')
def wiki_write_page(name, content, **kwargs):
def wiki_write_page(name, content, message=None, username=None, email=None, **kwargs):
body = dict(name=name,
content=content)
body.update(kwargs)
return Search.index('wiki', 'page', body=body)
content=content,
message=message,
email=email,
username=username)
return Search.index('wiki', 'page', id_=name, body=body)
@Wiki.after('rename_page')

View file

@ -10,7 +10,16 @@ class Search(HookMixin):
@classmethod
def wiki(cls, query):
return elastic.search(index='wiki', body={"query": {"match_all": {}}})
if not query:
return []
res = elastic.search(index='wiki', body={"query": {
"multi_match": {
"query": query,
"fields": ["name^3", "content"]
}}})
return [hit["_source"] for hit in res['hits']['hits']]
@classmethod
def users(cls, query):

View file

@ -1,10 +1,10 @@
from flask import abort, g, render_template, request, redirect, Blueprint, flash, url_for, current_app
from .models import Search
blueprint = Blueprint('search', __name__)
@blueprint.route('/_search')
def search():
results = Search.wiki(request.args.get('q'))
print results
return render_template('search/search.html', results=results)

View file

@ -51,6 +51,10 @@
border-radius: 0;
}
.navbar .form-control {
max-height: 33px;
}
.checkbox-cell {
width: 4em;
padding: 0.3em;

View file

@ -49,7 +49,15 @@
<li><a href="{{ url_for('wiki.history', name=name) }}"><i class="fa fa-clock-o"></i> History</a></li>
{% endif %}
</ul>
<ul class="nav navbar-nav navbar-right">
<li>
<form class="navbar-form" role="search" action="{{ url_for('search.search') }}">
<div class="form-group">
<input name="q" type="text" class="form-control" placeholder="Search">
</div>
</form>
</li>
{% if current_user.is_authenticated() %}
<li class="dropdown user-avatar">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">

View file

@ -1,4 +1,17 @@
{% extends 'layout.html' %}
{% block body %}
{% if results %}
<div class="list-group">
{% for r in results %}
<a href="{{ url_for('wiki.page', name=r['name']) }}" class="list-group-item">
<h4 class="list-group-item-heading">{{ r['name'] }}</h4>
<p class="list-group-item-text">
{{ r['content'][:100] }}
</p>
</a>
{% endfor %}
</div>
{% else %}
No Results Found
{% endif %}
{% endblock %}