使用 PSP 进行文本搜索


简介

本文档描述如何使用 PSP 构建文本搜索示例应用程序。我们假定读者了解如何使用 interMedia Text。要开发和部署 PL/SQL Server Pages,您需要 8.1.6 版本或更新的 Oracle 服务器,以及 PL/SQL web 网关。

PSP 是什么?

PL/SQL Server Pages (PSP) 是 Oracle 用于服务器端 web 应用程序开发的 PL/SQL 脚本编制解决方案。它使 PL/SQL 用户能够通过将 PL/SQL 脚本嵌入到 HTML 页中,从而开发具有动态内容的 web 页,这些脚本在 Web 客户请求页面时被执行。

PSP 页是嵌入 PL/SQL 脚本的 HTML 页,与 HTML 内容相分离。应用程序逻辑(嵌入的 PL/SQL 脚本)与布局逻辑 (HTML) 的这种分离使 PSP 页的开发和维护变得容易。

构建文本搜索 PSP

用 PL/SQL 编写基于 web 的应用程序的常用方法是使用 htp 程序包来处理 HTML 输出的构造。以下的代码段表示如何显示 PL/SQL 的文本搜索查询结果。
   ...
for c1 in (select id, title, descr, ctxsys.score(1) scr
from docs
where ctxsys.contains(content,v_query,1) > 0
order by ctxsys.score(1) desc) 
loop

htp.p('<table  cellpadding=0 cellspacing=0 border=0>');
htp.p('<tr>');
htp.p('<td><b>Precision score</b>: ');
htp.p('<font color="#ff0000">'||c1.scr||'%</font>, <b>');
htp.p('</td>');
htp.p('<td>'||c1.id||'</td>');
htp.p('<td>'||c1.title||'</td>');
htp.p('<td>'||c1.descr||'</td>');
htp.p('</tr></table>');

end loop;
   ...
在以上的示例代码中,开发人员必须使用程序变量作为输出字符串来编写 HTML 输出代码。

在 PSP 中,目的是将逻辑与布局相分离。它是 HTML(页面的静态部分)与 PL/SQL(动态内容部分)的混合体。我们可以在 <% 和 %> 中包含任何 PL/SQL 语句。

有关 PSP 语法的更多信息,请参阅 Oracle8i Application 开发人员指南。

让我们创建一个简单的表。在 sqlplus 中……

create table docs
(id number primary key,
text varchar2(2000));

让我们插入一些值
insert into docs (id,text)
values(1,'This is a technical overview of the improvements found in interMedia Text, version 8.1.6. ');

insert into docs(id,text)
values(2,'This is intended for an audience familiar
with version 8.1.5. If you are new to interMedia Text, please start with Oracle8i interMedia Text 8.1.5 - Technical Overview   ');

insert into docs(id,text)
values(3,'The improvements in structured document support include indexing and searching attribute text, nested within for sophisticated

queries, doctype-limited tag detection, dynamic add section for existing indexes, and a new AUTO sectioner which requires no
section pre-definition   ');

insert into docs(id,text)
values(4,'Multi-lingual databases can now store documents of different languages in a single column, thanks to the new MULTI lexer');

insert into docs(id,text)
values(5,'Other interesting features include limited parallel indexing, more flexible stem and fuzzy operators, and in-memory document
services   ');
现在我们创建一个文本索引
create index docs_index on docs(text) indextype is ctxsys.context;

PSP 的文件必须具有扩展名 .psp(在此例中为 sample.psp)。


<%@ plsql parameter="query" default="null" %>

<html>
<head>
<title>PSP Sample</title>
</head>
<body>
<%


If query is null Then

-- This part of the script allows a person
-- to enter data on an HTML form.
%>
<center>
<FORM METHOD=POST ACTION="sample">
<P>Search for <INPUT TYPE=TExt SIZE=50 MAXLENGTH=50 NAME="query">
<INPUT TYPE=SUBMIT VALUE="Search">
</FORM>

<% Else

%>

<h2> PSP Sample</h2>
<p>
<%!
color varchar2(6) := 'ffffff';
%>
<center>
<FORM METHOD=POST ACTION="sample">
<P>Search for 
<INPUT TYPE=TExt SIZE=50 MAXLENGTH=30 NAME="query" VALUE=<%= query %>>
<INPUT TYPE=SUBMIT VALUE="Search">
</FORM>

<p>
<table border="1" cellpadding="4" cellspacing="0">
<tr bgcolor="#6699CC">
<th>Doc ID</th>

<th>Text</th>
</tr>
<%
for c in (select id, text
from docs where contains(text,query) >0 
) loop
       %>
<tr bgcolor="#<%= color %>">
<td> <%= c.id %>   </td>
<td> <%= c.text %> </td>
</tr>
<%
if (color = 'ffffff') then 
color := 'eeeeee';
else
color := 'ffffff';
end if;


end loop;

 %>
</table></center>
<% End if;%>
<hr>

</body>
</html>

安装

要在服务器中安装 PSP,我们需要将其作为存储过程加载到数据库中。每个 .psp 文件对应一个存储过程。使用 loadpsp 实用程序在一个步骤中编译和加载这些页面。

loadpsp -replace -user oalonso/oalonso sample.psp
在正确编译 PSP 后,可以通过在 web 浏览器中检索 URL 来运行它。例如:
http://mymachine.name/sample
还可以指定不同的端口号和 DAD。例如,如果使用 WebDB 监听器:
http://oalonso-sun.us.oracle.com:8080/oalonso2/sample

最后更新时间:8 月 29 日星期二,美国太平洋时区 08:58:50