简体   繁体   中英

Dynamic search like stackoverflow for django

Does anyone know what plugin (with Django) is best to handle dynamic searches like StackOverflow's? Something like this :

<input type="text" value="how to [django] or [python] duplicate:yes is:answer" style="width: 50%">

I just think about complex queries.

query = request.GET.get('q')

text_query = # what regex here?
tags_query = # 
is_duplicate = re.search(r'(?P<duplicate>\w+)', query)

Question.objects.filter(...)
Answer.objects.filter(...)
query = "how to [django] or [python] or [mu] duplicate:yes is:answer"

tags = re.search(r'\[(\w+)\](( or \[(\w+)\])+)?', query)
colons = re.findall(r'\w+:\w+', query)

search = query.replace(tags.group(), '')
for colon in colons:
    search = search.replace(colon, '')

print(search) # how to

Then you can process tags and colons separately for querying.

If you want to search the questions database for all of the terms in the search query you could do something like this. It will find all text that contains all the search terms.

search = search.split()

db_query = Q(text__icontains=search[0])
for term in search[1:]:
    db_query = db_query | Q(text__icontains=term)

Question.objects.filter(db_query)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM