Make Elasticsearch searched fields configurable

This allows fields stuch as "content" to be searchable when using Elasticsearch
Add config item ELASTICSEARCH_FIELDS, list of fields to search.
Defaults to just "name" to preserve existing behaviour
Available fields are:
- "name"
- "username"
- "content"
- "message"
This commit is contained in:
Simon Edwards 2015-07-24 13:29:24 +01:00
parent ceec6b6789
commit 8e73a32875
2 changed files with 6 additions and 3 deletions

View file

@ -99,6 +99,7 @@ SEARCH_TYPE = 'simple' # simple is not good for large wikis
# SEARCH_TYPE = 'elasticsearch' # SEARCH_TYPE = 'elasticsearch'
ELASTICSEARCH_URL = 'http://127.0.0.1:9200' ELASTICSEARCH_URL = 'http://127.0.0.1:9200'
ELASTICSEARCH_FIELDS = ["name"]
# SEARCH_TYPE = 'whoosh' # SEARCH_TYPE = 'whoosh'
WHOOSH_INDEX = '/tmp/whoosh' WHOOSH_INDEX = '/tmp/whoosh'

View file

@ -14,7 +14,8 @@ def whoosh(app):
def elasticsearch(app): def elasticsearch(app):
from flask.ext.elastic import Elastic from flask.ext.elastic import Elastic
return ElasticSearch(Elastic(app)) fields = app.config.get('ELASTICSEARCH_FIELDS')
return ElasticSearch(Elastic(app), fields)
class Search(object): class Search(object):
@ -125,8 +126,9 @@ class WhooshSearch(BaseSearch):
class ElasticSearch(BaseSearch): class ElasticSearch(BaseSearch):
def __init__(self, elastic): def __init__(self, elastic, fields):
self.elastic = elastic self.elastic = elastic
self.fields = fields
def index(self, index, doc_type, id_=None, body=None): def index(self, index, doc_type, id_=None, body=None):
return self.elastic.index(index=index, doc_type=doc_type, id=id_, body=body) return self.elastic.index(index=index, doc_type=doc_type, id=id_, body=body)
@ -144,7 +146,7 @@ class ElasticSearch(BaseSearch):
res = self.elastic.search(index='wiki', body={"query": { res = self.elastic.search(index='wiki', body={"query": {
"multi_match": { "multi_match": {
"query": query, "query": query,
"fields": ["name"] "fields": self.fields
}}}) }}})
return [hit["_source"] for hit in res['hits']['hits']] return [hit["_source"] for hit in res['hits']['hits']]