16👍
You can achieve that behavior by making your index’s text field an EdgeNgramField:
class PersonIndex(SearchIndex):
text = EdgeNgramField(document=True, use_template=True)
first_name = CharField(model_attr = 'first_name')
last_name = CharField(model_attr = 'last_name')
3👍
In addition to the EdgeNgramField
hint that others mentioned in this page (and of course NgramField
, if you work with Asian languages), I think it is worth to mention that in Django_haystack you can run raw queries on Solr via following command:
from haystack.query import SearchQuerySet
from haystack.inputs import Raw
SearchQuerySet().filter(text=Raw(query))
where text
is the field you want to search, and the query
can be anything based on Query Parser Syntax (version 3.6, or 4.6) of Lucene.
In this way you can easily set the query to ABC*
or ABC~
or anything else which fits to the syntax.
- [Django]-Making a Django form class with a dynamic number of fields
- [Django]-The right place to keep my signals.py file in a Django project
- [Django]-How can I disable logging while running unit tests in Python Django?
1👍
I had a similar issue while searching for non english words, for instance:
ABC
ABCD
If I want to search for keywords ABC
, I will expect the above two results. I was able to achieve the following by converting the keyword to lowercase and using startswith
:
keywords = 'ABC'
results.filter(code__startswith=keywords.lower())
- [Django]-Django: How to make a form with custom templating?
- [Django]-Django rest framework – self.context doesn't have request attribute
- [Django]-Django-admin: Add extra row with totals
1👍
I had the same problem and the only way to get the results I wanted was to modify the solr configuration file to include ngram filtering as the default tokenizer is based on white space. So use NGramTokenizer instead. I’d love to know if there was a haystack way of doing the same thing.
I’m not at my machine right now but this should do the trick.
<tokenizer class="solr.NGramTokenizerFactory" minGramSize="3" maxGramSize="15" />
- [Django]-Django syncdb and an updated model
- [Django]-Passing **kwargs to Django Form
- [Django]-How to switch Python versions in Terminal?
0👍
@riz I can’t comment yet or I would and I know it’s an old comment but in case anyone else runs past this: Make sure to manage.py update_index
Blockquote @Liarez how did you get this to work? I’m using haystack/elastic search and I wasn’t able to get it to work.
- [Django]-How to register users in Django REST framework?
- [Django]-Error in admin: __str__ returned non-string (type NoneType)
- [Django]-Check if key exists in a Python dict in Jinja2 templates