本章主要介绍DQL的搜索试验。
查询数据时除了使用DQL查询,还可以使用搜索试验。使用搜索试验时,搜索语句贴近自然语言,只需要输入搜索语句或者关键词,就能查询到想要的数据。
如上图所示,用户想要查看OrderInfo表的部分数据。那怎样能让业务人员无需了解数据库表结构,只要在界面上输入自然查询语句就可以随意查询数据呢?往下看。
步骤一:
打开元数据编辑设计器,在菜单栏中点击文件 - 新建。
创建词典文件。
在菜单栏中点击工具 - 导入元数据,选择元数据文件和字典文件。
确定后,生成词典,如下:
词典的常规属性介绍,比如表词、字段词、维词、比较词、排序词、动词、聚合词、连词、宏词、无效词、量词等,参考词典文件设计小节。
步骤二:
菜单栏中点击工具 - 搜索实验
输入搜索语句,点击【搜索】按钮:
搜索结果中出现一个或多个结果。选中任意的搜索结果,点击【查看明细】,查看对应的DQL语句。
点击【执行DQL】,查看结果。
默认最多显示100条数据。可修改数值100,并再次点击【搜索】,使新的浏览数据量生效。
下面以示例文件demo.glxc为例,详细介绍下多种搜索语句。
Ø 表词
表词的定义,参考词典文件设计小节的表词
搜索语句:雇员
DQL:
select Employee.EmpID as EmpID,Employee.Name as Name,Employee.Title as Title,Employee.BirthDate as BirthDate,Employee.HireDate as HireDate,Employee.Boss as Boss,Employee.HomeCity as HomeCity,year(now())-year(Employee.BirthDate) as Employee_age from Employee
浏览数据:
Ø 字段词
字段词的定义,参考词典文件设计小节的字段词
例1:
搜索语句:雇员姓名
DQL:
select Employee.Name as 姓名 from Employee
浏览数据:
例2:
搜索语句:年龄大于40岁的雇员
DQL:
select Employee.EmpID as EmpID,Employee.Name as Name,Employee.Title as Title,Employee.BirthDate as BirthDate,Employee.HireDate as HireDate,Employee.Boss as Boss,Employee.HomeCity as HomeCity,year(now())-year(Employee.BirthDate) as Employee_age from Employee where (year(now())-year(Employee.BirthDate)>40)
浏览数据:
需要说明的是,例子中对年龄的计算只是一种简化算法,如果需要精确的年龄,则需要比较日期。
例3:
搜索语句:订单ID签单销售
DQL:
select Orders.OrderID as 订单ID,Orders.EmployeeID.Name as 签单销售 from Orders
浏览数据:
Ø 维词
维词以及常数词的定义,参考词典文件设计小节的维词
例1:
搜索语句:今年Sanchuan订单金额
DQL:
select Orders.ShipDate#Year as ShipDate,Orders.CustomerID as CustomerID,Orders.Amount as 订单金额 from Orders where (Orders.ShipDate#Year==year(now()))&&(Orders.CustomerID==\"ALFKI\")
浏览数据:
例2:
搜索语句:北方地区的雇员姓名
DQL:
select Employee.HomeCity#Area as HomeCity,Employee.Name as 姓名 from Employee where ([1,3,5].contain(Employee.HomeCity#Area))
浏览数据:
Ø 比较词
比较词的定义,参考词典文件设计小节的比较词
例1:
搜索语句:订单金额大于1000
DQL:
select Orders.OrderID as OrderID,Orders.CustomerID as CustomerID,Orders.EmployeeID as EmployeeID,Orders.ReceiveDate as ReceiveDate,Orders.ShipDate as ShipDate,Orders.Amount as Amount,Orders.EmployeeID.Name as Orders_EmployeeID_Name from Orders where (Orders.Amount>1000)
浏览数据:
例2:
搜索语句:发货日期早于2011-02-12
DQL:
select Orders.OrderID as OrderID,Orders.CustomerID as CustomerID,Orders.EmployeeID as EmployeeID,Orders.ReceiveDate as ReceiveDate,Orders.ShipDate as ShipDate,Orders.Amount as Amount,Orders.EmployeeID.Name as Orders_EmployeeID_Name from Orders where (Orders.ShipDate<date(2011,02,12) )
浏览数据:
Ø 排序词
排序词的定义,参考词典文件设计小节的排序词
例1:
搜索语句:订单金额降序
DQL:
select Orders.Amount as 订单金额 from Orders order by 订单金额 desc
浏览数据:
例2:
搜索语句:订单金额前5
DQL:
select Orders.Amount as 订单金额 from Orders order by 订单金额 desc limit 5
浏览数据:
Ø 动词
动词的定义,参考词典文件设计小节的动词
搜索语句:签单于 "Wang Wei"
搜索时,系统会去字段簇里找与"Wang Wei"数据类型匹配的宏字段。
DQL:
select Orders.OrderID as OrderID,Orders.CustomerID as CustomerID,Orders.EmployeeID as EmployeeID,Orders.ReceiveDate as ReceiveDate,Orders.ShipDate as ShipDate,Orders.Amount as Amount,Orders.EmployeeID.Name as Orders_EmployeeID_Name from Orders where (Orders.EmployeeID.Name==\"Wang Wei\")
浏览数据:
Ø 聚合词
聚合词的定义,参考词典文件设计小节的聚合词
例1:
搜索语句:订单金额总计
DQL:
select Orders.sum(Orders.Amount) as 订单金额总计 from Orders
浏览数据:
例2:
搜索语句:客户订单金额总计
DQL:
select Orders.sum(Orders.Amount) as 订单金额总计 on Customer as 客户 from Orders by Orders.CustomerID
浏览数据:
Ø 连词
连词的定义,参考词典文件设计小节的连词
搜索语句:订单金额小于200或者订单金额大于10000
DQL:
select Orders.OrderID as OrderID,Orders.CustomerID as CustomerID,Orders.EmployeeID as EmployeeID,Orders.ReceiveDate as ReceiveDate,Orders.ShipDate as ShipDate,Orders.Amount as Amount,Orders.EmployeeID.Name as Orders_EmployeeID_Name from Orders where ((Orders.Amount<200) || (Orders.Amount>10000))
浏览数据:
Ø 宏词
宏词的定义,参考词典文件设计小节的宏词
搜索语句:最年轻的员工
DQL:
select top 1 Employee.EmpID as EmpID,Employee.Name as Name,Employee.Title as Title,Employee.BirthDate as BirthDate,Employee.HireDate as HireDate,Employee.Boss as Boss,Employee.HomeCity as HomeCity,year(now())-year(Employee.BirthDate) as Employee_age from Employee order by year(now())-year(Employee.BirthDate) asc
浏览数据:
Ø 量词
量词的定义,参考词典文件设计小节的量岗纲
例1:
搜索语句:金额大于1000元
DQL:
select ReturnedPmt.CustomerID as CustomerID,ReturnedPmt.ID as ID,ReturnedPmt.SellerID as SellerID,ReturnedPmt.Date as Date,ReturnedPmt.Amount as Amount,ReturnedPmt.VIPCustomer_End as VIPCustomer_End from ReturnedPmt where (ReturnedPmt.Amount>1000)
浏览数据:
搜索语句:金额大于1千元
DQL:
select ReturnedPmt.CustomerID as CustomerID,ReturnedPmt.ID as ID,ReturnedPmt.SellerID as SellerID,ReturnedPmt.Date as Date,ReturnedPmt.Amount as Amount,ReturnedPmt.VIPCustomer_End as VIPCustomer_End from ReturnedPmt where (ReturnedPmt.Amount>1*1000.0)
浏览数据:同上
例2:
搜索语句:40岁以上姓名
DQL:
select year(now())-year(Employee.BirthDate) as Employee_age,Employee.Name as 姓名 from Employee where (year(now())-year(Employee.BirthDate)>40)
浏览数据:
Ø 无效词
无效词的定义,参考词典文件设计小节的无效词
搜索语句:40岁以上的姓名
DQL:
select year(now())-year(Employee.BirthDate) as Employee_age,Employee.Name as 姓名 from Employee where (year(now())-year(Employee.BirthDate)>40)
浏览数据:同上