So basically i am trying to add an analyzer to my Elasticsearch backend in Haystack and so far i have had no luck.
I am trying to query something like this 3/09.0TBOBR.C1
and i get lots of errors. I was told analyzers were the solution to my problem so i tried using the keyword
analyzer
i tried like this:
class ConfigurableElasticBackend(ElasticsearchSearchBackend):
DEFAULT_ANALYZER = "keyword"
and i also tried like this in my settings file:
ELASTICSEARCH_DEFAULT_ANALYZER = "keyword"
I did REBUILD INDEX after changing
My error message is:
Failed to query Elasticsearch using '(3/09.0TBOBR.C1)':
Non-OK response returned (400):
u'SearchPhaseExecutionException[Failed to execute phase [query], all shards failed;
shardFailures {[oh93DYn1QTyVWmAnDMyKdQ][haystack][4]:
SearchParseException[[haystack][4]: from[-1],size[-1]:
Parse Failure [Failed to parse source [{
"sort": [{
"score": {"order": "asc", "ignore_unmapped": true, "missing": "_last"}
}],
"query": {"filtered": {"filter": {"fquery": {"query": {"query_string": {"query": "django_ct:(caselaw.process)"}}, "_cache": true}}, "query": {"query_string": {"query": "(3/09.0TBOBR.C1)", "default_operator": "AND", "default_field": "text", "auto_generate_phrase_queries": true, "analyze_wildcard": true}}}
}, "from": 0, "size": 20}]]];
nested:
QueryParsingException[[haystack]
Failed to parse query [(3/09.0TBOBR.C1)]];
nested:
ParseException[Cannot parse \'(3/09.0TBOBR.C1)\':
Lexical error at line 1, column 17.
Encountered: <EOF> after : "/09.0TBOBR.C1)"];
nested:
TokenMgrError[Lexical error at line 1, column 17.
Encountered: <EOF> after : "/09.0TBOBR.C1)"]; }{[oh93DYn1QTyVWmAnDMyKdQ][haystack][0]:
SearchParseException[[haystack][0]: from[-1],size[-1]:
Parse Failure [Failed to parse source [{"sort": [{"score": {"order": "asc", "ignore_unmapped": true, "missing": "_last"}}], "query": {"filtered": {"filter": {"fquery": {"query": {"query_string": {"query": "django_ct:(caselaw.process)"}}, "_cache": true}}, "query": {"query_string": {"query": "(3/09.0TBOBR.C1)", "default_operator": "AND", "default_field": "text", "auto_generate_phrase_queries": true, "analyze_wildcard": true}}}}, "from": 0, "size": 20}]]];
nested:
QueryParsingException[[haystack] Failed to parse query [(3/09.0TBOBR.C1)]];
nested:
ParseException[Cannot parse \'(3/09.0TBOBR.C1)\':
Lexical error at line 1, column 17. Encountered: <EOF> after : "/09.0TBOBR.C1)"];
nested:
TokenMgrError[Lexical error at line 1, column 17. Encountered: <EOF> after : "/09.0TBOBR.C1)"]; }{[oh93DYn1QTyVWmAnDMyKdQ][haystack][1]: SearchParseException[[haystack][1]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"sort": [{"score": {"order": "asc", "ignore_unmapped": true, "missing": "_last"}}], "query": {"filtered": {"filter": {"fquery": {"query": {"query_string": {"query": "django_ct:(caselaw.process)"}}, "_cache": true}}, "query": {"query_string": {"query": "(3/09.0TBOBR.C1)", "default_operator": "AND", "default_field": "text", "auto_generate_phrase_queries": true, "analyze_wildcard": true}}}}, "from": 0, "size": 20}]]]; nested: QueryParsingException[[haystack] Failed to parse query [(3/09.0TBOBR.C1)]]; nested: ParseException[Cannot parse \'(3/09.0TBOBR.C1)\': Lexical error at line 1, column 17. Encountered: <EOF> after : "/09.0TBOBR.C1)"]; nested: TokenMgrError[Lexical error at line 1, column 17. Encountered: <EOF> after : "/09.0TBOBR.C1)"]; }{[oh93DYn1QTyVWmAnDMyKdQ][haystack][2]: SearchParseException[[haystack][2]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"sort": [{"score": {"order": "asc", "ignore_unmapped": true, "missing": "_last"}}], "query": {"filtered": {"filter": {"fquery": {"query": {"query_string": {"query": "django_ct:(caselaw.process)"}}, "_cache": true}}, "query": {"query_string": {"query": "(3/09.0TBOBR.C1)", "default_operator": "AND", "default_field": "text", "auto_generate_phrase_queries": true, "analyze_wildcard": true}}}}, "from": 0, "size": 20}]]]; nested: QueryParsingException[[haystack] Failed to parse query [(3/09.0TBOBR.C1)]]; nested: ParseException[Cannot parse \'(3/09.0TBOBR.C1)\': Lexical error at line 1, column 17. Encountered: <EOF> after : "/09.0TBOBR.C1)"]; nested: TokenMgrError[Lexical error at line 1, column 17. Encountered: <EOF> after : "/09.0TBOBR.C1)"]; }{[oh93DYn1QTyVWmAnDMyKdQ][haystack][3]: SearchParseException[[haystack][3]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"sort": [{"score": {"order": "asc", "ignore_unmapped": true, "missing": "_last"}}], "query": {"filtered": {"filter": {"fquery": {"query": {"query_string": {"query": "django_ct:(caselaw.process)"}}, "_cache": true}}, "query": {"query_string": {"query": "(3/09.0TBOBR.C1)", "default_operator": "AND", "default_field": "text", "auto_generate_phrase_queries": true, "analyze_wildcard": true}}}}, "from": 0, "size": 20}]]]; nested: QueryParsingException[[haystack] Failed to parse query [(3/09.0TBOBR.C1)]]; nested: ParseException[Cannot parse \'(3/09.0TBOBR.C1)\': Lexical error at line 1, column 17. Encountered: <EOF> after : "/09.0TBOBR.C1)"]; nested: TokenMgrError[Lexical error at line 1, column 17. Encountered: <EOF> after : "/09.0TBOBR.C1)"]; }]'
What am i doing wrong? thanks
The problem on the query side with haystack is that it uses the catch-all field flagged by document=True
in your search index configuration. This field is most probably not analyzed with the keyword analyzer. But you better check that by looking at the output of elasticsearch (this lists the analyzers):
http://localhost:9200/<index-name>/?pretty=true
In the stack trace you can see:
"default_field": "text"
You might think about having two different search views if you still want to support free text user searches with "normal" input. For your special search of inputs like 3/09.0TBOBR.C1
you might want to use the .filter()
or .narrow()
methods of Haystack's SearchQuerySet
where you can specify the field to search (and that would be the field you use the keyword
analyzer on, explicitly). For regular text fields you probably do not want to use keyword (== no) analysis.