[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 ...
随机推荐
- tcp nonblock connection rst
客户端(>5w)异步connect连接到server端,server端listen backlog设置为1024,发现存在部分客户端建立连接后,收到服务端的rst包. 先看下tcp监听套接字维护 ...
- eclipse和jdk的版本问题,比如printf()出错
1.右键项目选中properties 2.Java Builder Path >>>Libraries>>>选中JRE Syste Library[jre7]然后r ...
- python面向对象笔记
一.封装(属性/私有方法/公有方法/静态方法/构造函数...) # 定义一个类 class Animal: # 私有成员(用_开头的约定为私有成员 - 注:仅仅是君子协定) _age = 0 # 构造 ...
- emSecure Use Digital Signatures to protect your products
emSecure Use Digital Signatures to protect your products emSecure is an RSA based software solution ...
- ITxlab倡议启动“互联网X大脑”计划
导语:"互联网X大脑"计划由ITxlab(互联网X实验室)联合科学院相关机构.基于7年以来取得的研究成果,倡议建立的互联网与脑科学前沿研究平台,吸引不同领域专家进行科学研究和成果交 ...
- poj 1184
经典的宽搜题目,感觉最好的办法应该是双向广搜. 不过用简单的启发式搜索可以飘过. #include <iostream> #include <cstdio> #include ...
- SpringMVC默认欢迎页面的问题
使用SpringMVC很长时间,一直有个问题没有搞定,就是web.xml中默认欢迎页面转向控制器的问题. 由于答应朋友明天要交个网站,他们对默认页面有这样的要求,并且最好也别用js等等的跳转:所以今天 ...
- android:碎片的生命周期
和活动一样,碎片也有自己的生命周期,并且它和活动的生命周期实在是太像了,我相 信你很快就能学会,下面我们马上就来看一下. 4.3.1 碎片的状态和回调 还记得每个活动在其生命周期内可能会有哪几种 ...
- LeetCode OJ 238. Product of Array Except Self 解题报告
题目链接:https://leetcode.com/problems/product-of-array-except-self/ 238. Product of Array Except Se ...
- iOS画折线图
代码例子效果: 下载地址:http://download.csdn.net/detail/qqmcy/6983187 LineChartViewDemo.h #import <UIKit/UI ...