String qstr = "title:\"" + title + "\" OR content:\"" + content + "\"";これは、検索の条件を指定する「クエリー」なのです。GAEのデータベースはSQLではありませんが、独自のクエリー言語(というほど大げさではありませんが)に基づいて検索条件を設定するようになっています。――では、Searchで使えるクエリーというのはどのようなものか。その基本を少し整理しましょう。
フィールド名1: "検索テキスト1" OR フィールド名2: "検索テキスト2"このように記述されていることがわかります。ORは「2つのいずれかが合致すればOK」という設定です。逆に「2つとも合致してないとダメ」という場合はANDを指定します。これらは条件だけでなく、複数のテキストを指定するのにも使えます。例えば、
title: ("Hello" OR "Hi") AND "BYE"こんな具合に設定すれば、titleフィールドにHelloかHiのどちらか、およびBYEの両方が含まれているものを探します。※リストが表示されない場合
AddBlockなどの広告ブロックツールがONになっているとリストなどが表示されない場合があります。これらのツールをOFFにしてみてください。
※MyGaeAppServletの修正(doPostは置き換える)
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException {
response.setContentType("text/html");
request.setCharacterEncoding("utf8");
response.setCharacterEncoding("utf8");
session = request.getSession();
session.setAttribute("message", null);
String param = request.getParameter("mode");
String title = request.getParameter("title");
String content = request.getParameter("content");
if ("create".equals(param)){
this.createDocument(title, content);
}
response.sendRedirect("/mygaeapp");
}
public void findDocument(String title,String content){
String qstr = "title:\"" + title + "\" OR content:\"" + content + "\"";
Index index = SearchServiceFactory.getSearchService()
.getIndex(IndexSpec.newBuilder()
.setName("mydataIndex"));
try {
Query query = Query.newBuilder()
.setOptions(QueryOptions
.newBuilder()
.setLimit(10)
.build()).build(qstr); // ★
Results<ScoredDocument> results = index.search(query);
String result = "<table width=\"700\">";
for (ScoredDocument document : results) {
String id = document.getId();
String title_str = document.getOnlyField("title")
.getText();
String content_str = document.getOnlyField("content")
.getText();
Date date = document.getOnlyField("datetime")
.getDate();
result += "<tr><td>" + id + "</td><td>" + title_str + "</td><td>" +
content_str + "</td><td>" + date + "</td></tr>";
}
result += "</table>";
session.setAttribute("result",result);
session.setAttribute("message", "検索 " + qstr + " を実行しました。");
} catch (SearchException e) {
session.setAttribute("message", "検索に失敗しました。");
}
}
| << 前へ |