diff --git a/README.md b/README.md index c367ebd..f223cb2 100644 --- a/README.md +++ b/README.md @@ -248,6 +248,12 @@ In your Realms Config, have the following options set: "SEARCH_TYPE": "elasticsearch" "ELASTICSEARCH_URL": "http://127.0.0.1:9200" +Optionally, also set the following option to configure which fields are searchable: + + "ELASTICSEARCH_FIELDS": ["name"] + +Allowable values are `"name"`, `"content"`, `"username"`, `"message"`. The default is `["name"]`. + ### Whoosh Setup Simply install Whoosh to your Python environment, e.g. diff --git a/realms/config/__init__.py b/realms/config/__init__.py index d54bfe0..fe333b4 100644 --- a/realms/config/__init__.py +++ b/realms/config/__init__.py @@ -99,6 +99,7 @@ SEARCH_TYPE = 'simple' # simple is not good for large wikis # SEARCH_TYPE = 'elasticsearch' ELASTICSEARCH_URL = 'http://127.0.0.1:9200' +ELASTICSEARCH_FIELDS = ["name"] # SEARCH_TYPE = 'whoosh' WHOOSH_INDEX = '/tmp/whoosh' diff --git a/realms/modules/search/models.py b/realms/modules/search/models.py index 8950d4d..267c5c5 100644 --- a/realms/modules/search/models.py +++ b/realms/modules/search/models.py @@ -14,7 +14,8 @@ def whoosh(app): def elasticsearch(app): from flask.ext.elastic import Elastic - return ElasticSearch(Elastic(app)) + fields = app.config.get('ELASTICSEARCH_FIELDS') + return ElasticSearch(Elastic(app), fields) class Search(object): @@ -125,8 +126,9 @@ class WhooshSearch(BaseSearch): class ElasticSearch(BaseSearch): - def __init__(self, elastic): + def __init__(self, elastic, fields): self.elastic = elastic + self.fields = fields def index(self, index, doc_type, id_=None, body=None): 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": { "multi_match": { "query": query, - "fields": ["name"] + "fields": self.fields }}}) return [hit["_source"] for hit in res['hits']['hits']]