[IR] XPath for Search Query
XPath 1.0
XPath Containment
Distributed Query Evaluation
RE and DFA
XPath 1.0
-- 在XML中的使用
XPath 语法: http://www.w3school.com.cn/xpath/xpath_syntax.asp

XPath (红色字体) 示例:
/bib/book/year
Result: <year> 1995 </year>
<year> 1998 </year> /bib/paper/year
Result: empty
XPath: Restricted Kleene Closure 暴力查找node的感觉
//author
Result: <author> Serge Abiteboul </author>
<author> <first-name> Rick </first-name>
<last-name> Hull </last-name>
</author>
<author> Victor Vianu </author>
<author> Jeffrey D. Ullman </author> /bib//first-name
Result: <first-name> Rick </first-name>
XPath: Text Nodes 提取 (无子结构的) elem
/bib/book/author/text()
Result: Serge Abiteboul
Victor Vianu
Jeffrey D. Ullman
XPath: Text Nodes 提取 (有子结构的) elem
//author/*
Result: <first-name> Rick </first-name>
<last-name> Hull </last-name>
XPath: Attribute Nodes 提取属性的内容
/bib/book/@price
Result: “55”
XPath: Qualifiers 只要有firstname子节点的元素
/bib/book/author[firstname]
Result: <author> <first-name> Rick </first-name>
<last-name> Hull </last-name>
</author>
XPath: Qualifiers 只要条件为:有firstname子节点和address子节点,且address中下的某一level中有zip节点,以及下一level就得有city。满足如此条件的author下的lastname的内容。
/bib/book/author[firstname][address[//zip][city]]/lastname
Result: <lastname> ... </lastname>
<lastname> ... </lastname>
XPath: 子节点的属性满足指定条件
/bib/book[@price < “60”]
/bib/book[author/@age < “25”]
XPath: author下的elem只允许有txt。
/bib/book[author/text()]
XPath 轴: 轴可定义相对于当前节点的节点集
轴可定义相对于当前节点的节点集。
| 轴名称 | 结果 |
|---|---|
| ancestor | 选取当前节点的所有先辈(父、祖父等)。 |
| ancestor-or-self | 选取当前节点的所有先辈(父、祖父等)以及当前节点本身。 |
| attribute | 选取当前节点的所有属性。 |
| child | 选取当前节点的所有子元素。 |
| descendant | 选取当前节点的所有后代元素(子、孙等)。 |
| descendant-or-self | 选取当前节点的所有后代元素(子、孙等)以及当前节点本身。 |
| following | 选取文档中当前节点的结束标签之后的所有节点。 |
| namespace | 选取当前节点的所有命名空间节点。 |
| parent | 选取当前节点的父节点。 |
| preceding | 选取文档中当前节点的开始标签之前的所有节点。 |
| preceding-sibling | 选取当前节点之前的所有同级节点。 |
| self | 选取当前节点。 |
XPath Containment
Return Node: [[name]] 使用double circles表示。
任意深度子目录存在:双斜线。

表达式解释:
root下的任意未知结点person,满足孩子中有name,任意子孙中需有属性zip = "12345"的结点。
满足以上条件,则返回这个person的phone。

Equivalence, Containment
- E = E’ if they return the same result
- E ⊆ E’ if the result returned by E is a subset of that returned by E’.

返回的结果总是一致的,我们认为这是一个query。

这个比较复杂,所以我们引入一种方法来解决:
Practical Algorithm for Linear XPath *,//

关键在于找到mapping。
第一个例子:

第二个例子:

第三个例子:

为何不等价?
应该是前者包含后者!
注意1:/*// 转化为双线。
注意2://name 这里没有*,所以是>=0,这导致了两者不等价!
Branching XPath *,//
-- 难度加大,计算量大

视情况而定,此处略。
Distributed Query Evaluation

a.b*.c // any num of b, here we regard it as a Query.
a.b+.c // at least one num of b
Review 正则表达式 与 DFA 的关系
在分布式环境下的Query,如下:(如用在下图中查询这个Query)

A naïve approach takes too many communication steps
=> we have to do more work locally
A better approach needs to
1. identify all external references
2. identify targets of external references
Algorithm:
1. Given a query, we compute its automaton 计算出某一个query的对应的一个东西
2. Send it to each site 将这个东西分发给各个site
3. Start an identical process at each site 各个site分别处理这个东西
4. Compute two sets Stop(n, s) and Result(n, s) 计算出这两个结果
5. Transmits the relations to a central location and get their union 合并所有结果
Detail:
每个site都会有自己的一个《进出表》,如下:
Site 1:
| In | x1, x4 |
| Out | y1, y3 |
Site 2:
| In | y1, y3 |
| Out | z2 |
Site 3:
| In | z2 |
| Out | x4 |
相对于Query: a.b+.c
以 Site 2 为例,该site将处理query并得到如下两个table:
《搞不定表》- Stop Table
| Start | End |
| (y1, s2) | (z2, s2) |
| (y3, s2) | (z2, s2) |
这里的z2怎么得来?Site 2虽然不晓得其他site的信息,但起码知道
与自己有直接连接的node信息。比如:
与y3直接相连接的z2有可能作为s2。
《能搞定表》- Result Table
| Start | End |
| (y1, s2) | (y3, s3) |
| (y1, s3) | (y1, s3) |
| (y3, s3) | (y3, s3) |
然后,每个site都应该返回了针对该query的俩表。
接下来就是如何合并这么些个表的问题。
让我们将整个表都列出来,做一次傻瓜式的demo。
Union:
| Start | Stop | Start | Result | ||
| (x1, s1) | (y1, s2) | stop1 | (x1, s3) | x1 | result1 |
| (x4, s2) | (y3, s3) | stop2 | (x4, s2) | x3 | result2 |
| (y1, s2) | (z2, s2) | stop3 | (x4, s3) | x4 | result3 |
| (y3, s2) | (z2, s2) | stop4 | (y1, s2) | y3 | result4 |
| (z2, s2) | (x4, s2) | stop5 | (y1, s3) | y1 | result5 |
| (y3, s3) | y3 | result6 | |||
| (z2, s1) | z3 | result7 | |||
| (z2, s2) | z2 | result8 | |||
| (z2, s3) | z2 | result9 |
解说:
| root | ||
| stop1 | 在其他site能直接终止么? | |
| result4 | 还真有,可以直接结束! | 匹配到了(y1,s2) |
| stop3 | 不结束,绕一绕,其他site或许能给出新的sol | 匹配到了(y1,s2) |
| result8 | 确实有,可以直接结束! | 匹配到了(z2,s2) |
| stop5 | 不结束,绕一绕,其他site或许能给出新的sol | 匹配到了(z2,s2) |
| result2 | 还真有,可以直接结束! | 匹配到了(x4,s2) |
| stop2 | 不结束,绕一绕,其他site或许能给出新的sol | 匹配到了(x4,s2) |
| result6 | 有的啦,可以直接结束! | |
| n/a | 不结束,绕一绕,其他site或许能给出新的sol | 匹配不到了! |
Answer:
{y3, z2, x3}
Regular Expression (正则表达式)
定义:

This definition may seem circular, but 1-3 form the basis
Precedence: Parentheses have the highest precedence,
followed by *, concatenation, and then union.
RE Examples (表达形式 = 实际的集合形式)
-- 当search时希望return results in this kind of pattern.
• L(001) = {001}
• L(0+1*) = { 0, 1, 10, 100, 1000, 10000, … } // union: or; * any number of
• L(0*1*) = {1, 01, 10, 010, 0010, …} i.e. {w | w has exactly a single 1}
• L( |- |- )* = {w | w is a string of even length}
• L((0(0+1))*) = { ε, 00, 01, 0000, 0001, 0100, 0101, …}
• L((0+ε)(1+ ε)) = {ε, 0, 1, 01}
• L(1Ø) = Ø ; concatenating the empty set to any set yields the empty set.
• Rε = R // ε就像1
• R+Ø = R // 空集就像0
Exercise 1
∑ = {10, 11}, ∑* = {є, 10, 11, 1010, 1011, 1110, 1111, …}
表示集合里元素的数量各自都是任意的,然后组合在一起。
Exercise 2
L1 = {10, 1}, L2 = {011, 11}, L1L2 = {10011, 1011, 111}
哄小孩的题。
Exercise 3
Write RE for
– All strings of 0’s and 1’s (其实就是任意0与1的组合)
• (0|1)*
– All strings of 0’s and 1’s with at least 2 consecutive 0’s (需要至少有连续的0)
• (0|1)*00(0|1)*
– All strings of 0’s and 1’s beginning with 1 and not having two consecutive 0’s (不能有连续的0)
• (1+10)* // {1, 10} 其中的元素任意组合确实不会有连续的0出现,技巧哉!
More exercises
1) (0|1)*
• Answer: all strings of 0’s and 1’s ending in 011
2) 0*1*2*
• Answer: any number of 0’s followed by any number of 1’s followed by any number of 2’s
3) 00*11*22*
• Answer: strings in 0*1*2 with at least one of each symbo
Link: 百度百科
起源:
引擎
这里只简单瞧瞧 DFA (Deterministic Finite Automata),"确定有限状态自动机"。

b*a*b*a*b* 有循环,可见是个环。
Duality: RE与DFA的等价性

局限性:
Which languages CANNOT be described by any RE?
• Set of all bit strings with equal number of 0s and 1s. // 没有计数相关功能
• Set of all decimal strings that represent prime numbers. // 又不是人工智能,:p
ab*a
可见,b有环。

a(a|b)*a
可见,(a|b)有环。思考环的构造,如下:

DFA to RE: State Elimination
• Eliminates states of the automaton and replaces the edges with regular expressions that includes the behavior of the eliminated states.
• Eventually we get down to the situation with just a start and final node, and this is easy to express as a RE

We can describe this automaton as: (R | SU*T)*SU*

分析:
对于第一个node来说,有两条路径:
- R自循环
- SU*T绕一圈后再回来
然后考虑end node:也是两条路径:
- U自循环
- “SU*T绕一圈后再回来”+S
于是便构成了: (R | SU*T)*SU*
We can describe this automaton as simply R*

标准的elimination过程
Example One:
1) First convert the edges to RE’s.
注意:简化的过程中不要丢失信息。
2) 有个环,精简它。
3) 3-->1-->2的过程不能丢失,所以保留"11"。

Answer: (0+10)*11(0+1)*
Example Two:
1) First convert the edges to RE’s.
注意:简化的过程中不要丢失信息。
2) 有个环,精简它。
3) 1-->2-->3的过程不能丢失,所以保留"10*1"。

注意:这里有两个end node。
策略:关掉node 1,计算;再关掉node 2,计算。
关掉node 3:

0*
关掉node 1:

0*10*1(0|10*1)*
合并结果(或操作):
0* | 0*10*1(0|10*1)*
[IR] XPath for Search Query的更多相关文章
- elastic search query & filter & query_string
一.基本概念 1.query时,如何指定返回哪些字段 希望返回name和date字段 希望返回以location.*为前缀的字段以及date字段:不希望返回location.geolocation字段 ...
- [IR] Open Source Search Engines
From:http://blog.csdn.net/xum2008/article/details/8740063 本文档是对现有的开源的搜索引擎的一个简单介绍 1. Lucene Lucene ...
- [IR] What is XML
Concept: http://www.w3school.com.cn/xml/xml_cdata.asp Semistructured: 和普通纯文本相比,半结构化数据具有一定的结构性.OEM(Ob ...
- 本人AI知识体系导航 - AI menu
Relevant Readable Links Name Interesting topic Comment Edwin Chen 非参贝叶斯 徐亦达老板 Dirichlet Process 学习 ...
- [Python] 02 - String
字符串 string 一.基本性质 不变性 Immutability 要变就 --> list --> string 二.功能函数 功能函数 S = 'Spam" S.find( ...
- lucene 3.0.2 search 各种各样的Query类型
http://blog.sina.com.cn/s/blog_61d2047c010195mo.html lucene的这种各种各样的查询类型 1.TermQuery 最简单的Qu ...
- lucene查询索引之Query子类查询——(七)
0.文档名字:(根据名字索引查询文档)
- SharePoint 2013 Search REST API 使用示例
前言:在SharePoint2013中,提供Search REST service搜索服务,你可以在自己的客户端搜索方法或者移动应用程序中使用,该服务支持REST web request.你可以使用K ...
- Query DSL for elasticsearch Query
Query DSL Query DSL (资料来自: http://www.elasticsearch.cn/guide/reference/query-dsl/) http://elasticsea ...
随机推荐
- Android中POST请求代码
public static String getDataByPost(String APIUrl,Map<String, String> paramsHashMap){ String re ...
- 通过ZwQuerySystemInformation获取EPROCESS
google一下,发现很多都是直接通过ZwQuerySystemInformation通过11号获取进程结构SYSTEM_PROCESS_INFORMATION,对于详细的进程信息表达不够.所以想要通 ...
- iOS:用Block写一个链式编程
一.介绍 链式编程是一个比较新颖的编程方式,简单直观,用起来也比较舒服.目前比较有名的Mansory和BabyBlueTooth就是使用链式编程写的第三方框架. 二.写法 链式编程写法不同于传统方式, ...
- Android典型界面设计——FragmentTabHost+Fragment实现底部tab切换
一.问题描述 在上次博文中,我们使用RadioGroup+ViewPage+Fragmen实现了顶部滑动导航(查看文章:http://www.cnblogs.com/jerehedu/p/460759 ...
- js手机适配
代码一: <script type="text/javascript"> function mobile_device_detect(url){ var thisOS= ...
- MySQL空间索引简单使用
简述 MySQL在5.7之后的版本支持了空间索引,而且支持OpenGIS几何数据模型.国内的MySQL相关的书籍都比较老了,在这方面有详细描述的还没有见过.有一本比较新的PostgreSQL的数据介绍 ...
- javascript中return function与return function()的区别
参考https://stackoverflow.com/questions/7629891/functions-that-return-a-function-javascript 问题:唯一的区别是r ...
- Kotlin VS Java:基本语法差异(转载)
5月18号,goole宣布Kotlin成为官方支持的开发语言以来,Kotlin语言社区,公众号,qq群等全面轰炸,本文是一篇译文,来自国外的一个用户,将给大家介绍,基础语法部分Kotlin和java之 ...
- how to check the computer is 32 bit or 64bit in linux
just use cat /proc/cpuinfo in shell
- dup2替换
今天看APUE上一道题,要求不能用fcnt1来替换dup1. 刚开始的思路是dup一个,测试发现与期望的不一致就马上关闭,发现遇到无限循环,刚才想了下,才发现一旦close掉,再次dup仍然是分配最小 ...