Google App Engine のデータストアには全文検索に対応した何かが無いので Python にがんばってもらった
くそが
models.py
# -*- coding: utf-8 -*- from django.utils.translation import ugettext_lazy as _ from google.appengine.ext import db class Message(db.Model): created_date = db.DateTimeProperty(auto_now_add = True) fulltext = db.TextProperty() nomaltext = db.StringProperty()
views.py
# -*- coding: utf-8 -*- from django.http import HttpResponseRedirect, HttpResponse from django.template import Context, loader # full text search from XXXXX.models import Message import re # データ100と検索用フォームを表示 def fulltextform(request): mes = Message.all().fetch(100) alllist = [] i = 1 for val in mes: alllist.append(getDataByStore(val)) cnt = str(len(mes)) c = Context({'alllist': alllist, 'allcnt' : cnt}) t = loader.get_template('fulltextform.html') return HttpResponse(t.render(c)) # 検索実行、結果表示 def fulltextfind(request): stext = request.POST['stext'] mes = Message.all() hitlist = [] for val in mes: hit = [] if hasWord(stext, val.fulltext) == True: hitlist.append(getDataByStore(val)) continue if hasWord(stext, val.nomaltext) == True: hitlist.append(getDataByStore(val)) continue cnt = len(hitlist) c = Context({'hitlist': hitlist, 'hitcnt' : cnt}) t = loader.get_template('fulltextfind.html') return HttpResponse(t.render(c)) def hasWord(search_text, text): if not text: return False if not re.search(search_text, text): return False return True def getDataByStore(data): result = [] result.append(data.key()) result.append(data.created_date.strftime('%y/Ym/%d %H:%M:%S')) result.append(data.fulltext) result.append(data.nomaltext) return result
fulltextform.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>fulltext search form</title> <style type="text/css"> ol { background-color : #ECECFF; } </style> </head> <body> <form method="post" action="./fulltextfind"> <input type="text" name="stext" value="ふるふる" size="50"> <input type="submit" value="検 索"> </form> <p> 以下{{ allcnt }}件のデータから指定した文字列を含むデータのみ抽出。<br> <br> {% for val in alllist %} <ol> <li>key_id : {{ val.0 }}</li> <li>created_date : {{ val.1 }}</li> <li>fulltext : <pre>{{ val.2}}</pre></li> <li>nomaltext : {{ val.3 }}</li> </ol> {% endfor %} </p> </body> </html>
fulltextfind.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>fulltext search find</title> <style type="text/css"> ol { background-color : #ECECFF; } .attention { color : #0000FF; </style> </head> <body> <a href="./fulltextform">フォームへ戻る</a> <p> {{ hitcnt }}件のデータがヒットしました。<br> <br> {% for val in hitlist %} <ol> <li>key_id : {{ val.0 }}</li> <li>created_date : {{ val.1 }}</li> <li>fulltext : <pre>{{ val.2}}</pre></li> <li>nomaltext : {{ val.3 }}</li> </ol> {% endfor %} </p> </body> </html>