上一个全文搜索实现了模糊查找,这个主要实现了精确查找,就是比如你查找mapreduce is simple那么他就只查找有这个句子的文章,而不是查找有这三个单词的文章。

这个版本需要重写反向索引,因为需要查找句子,所以需要定位每个单词的在文章中的位置,所以我们的反向索引需要加上单词所在的位置,即我们希望的输出是:
MapReduce file1.txt:<1,2,3>;file2.txt:<5,3,1>;这种格式的。
其实这一步比较简单。我们在map的时候输出为
“filename+word” position这样的<key,value>
“file1.txt:MapReduce”1
经过本地的combiner将其输出为:
“filename” “word:<position>” 
"file1.txt" "MapReduce:<1,2,3>"
最后经过reduece将所有同一个文件的单词归一,输出为
"filename" "word1:<position>;word2:<position>...."
"file1.txt" "MapReduce:<1,2,3>;simple:<5,6,7>"这种格式的
PS:由于这里的读取是从文件中每次读取一行,所以这里的position只是每一行上的位置,为非该单词在全文中的位置,如果遇到一句话横跨两行,那么这个程序就无法识别了,好像需要重写那个Input了,等下一个版本再修改
 
接下来主要就是根据索引来查找
大致的思路就是
Map阶段通过需要查找的句子例如MapReduce is simple来筛选反向索引中的单词,最后经过Map后得到在被查找的句子中的单词。输出为:
"filename" "word<position>"
"file1.txt" "MapReduce<1,2,3>"
经过reduce,则会把所有相同的文件的word给放在一起。由于reduce中单词的顺序是混乱的,所以为了识别句子,我这里增加了一个类
class  Address implements Comparable<Address>{
                 public String word ;
                 public int index;
                Address(String word, int index){
                                 this.word =word;
                                 this.index =index;
                }
                 public String toString(){
                                 return word +" "+ index;
                }
                 public int compareTo(Address a){
                                 if(index <a.index) return -1;
                                 else return 1;
                }
}
 
主要的word是用于放单词,index用于放索引,通过将同一个file下的value拆分到Address中,并且按照index进行排序,那么我们就能获得例如
M 1
M 2
M 3
i    4
s   5
i    6
M   7
(M代表Mapreduce i代表is,s代表simple)
那么如何识别这里的句子呢,首先这里的index必须是相邻的,并且这相邻的单词的顺序必须是M i s。为了识别相邻的单词的顺序问题,我这里新建了一个list,用于放输入的参数,也就是我要查找的句子,
ArrayList<String> sentence= new ArrayList<String>();
                                 for (i=2;i<wordnum+2;i++){
                                                String arg=conf.get("args" +i);
                                                sentence.add(arg);
                                }
 
接下来我们建立两个游标,一个指向上一个 word position一个指向当前,如果说上一个的word和当前的word在sentence中的位置刚好是相邻的,并且两个index也是相邻的那么n++,接着这两个游标都往下一步走,继续判断,直到n等于句子中单词的长度,那就说明已经匹配到了一个完整的句子。接着n=1再继续往下走,直到遍历完
具体代码:
 
public class MyMapper extends Mapper<LongWritable, Text, Text, Text> {
 
                 public void map(LongWritable ikey, Text ivalue, Context context)
                                                 throws IOException, InterruptedException {
                                Configuration conf=context.getConfiguration();
                                ArrayList< String> contents=new ArrayList< String>();
                                 int agrsnum=Integer.parseInt(conf.get( "argsnum"));
                                 int i=0;
                                 for (i=2;i<agrsnum;i++){
                                                 String arg=conf.get("args"+i);
                                                contents.add(arg);
                                }
                                 String line=ivalue.toString();
                                 String key=line.split("         ")[0];
                                 String value=line.split("      ")[1];
                                 for(String content:contents){
                                                 if(content.compareTo(key)==0){
                                                                StringTokenizer st=new StringTokenizer(value,";" );
                                                                 while(st.hasMoreTokens()){
                                                                                 String s=st.nextToken();
                                                                                 String filename=s.split(":")[0];
                                                                                 String adds=s.split(":")[1];
                                                                                 String val=key+adds;
                                                                                 //System.out.println(filename+"  "+ val);
                                                                                
                                                                                 //System.out.println("                             ");
                                                                                context.write( new Text(filename),new Text(val));
                                                                }
                                                }
                                }
                }
 
}
 
 
 
class  Address implements Comparable<Address>{
                 public String word ;
                 public int index;
                Address(String word, int index){
                                 this.word =word;
                                 this.index =index;
                }
                 public String toString(){
                                 return word +" "+ index;
                }
                 public int compareTo(Address a){
                                 if(index <a.index) return -1;
                                 else return 1;
                }
}
 
public class MyReducer extends Reducer<Text, Text, Text, Text> {
 
                 public void reduce(Text _key, Iterable<Text> values, Context context)
                                                 throws IOException, InterruptedException {
                                 // process values
                                Configuration conf=context.getConfiguration();
                                 int wordnum=Integer.parseInt(conf.get( "argsnum"))-2;
                                 int i=0;
                                ArrayList<String> sentence= new ArrayList<String>();
                                 for (i=2;i<wordnum+2;i++){
                                                String arg=conf.get("args" +i);
                                                sentence.add(arg);
                                }
                                
                                ArrayList<Address> list= new ArrayList<Address>();
                                
                                 for (Text val : values) {
                                                String[] line=val.toString().split("<|>|," );
                                                 for(int j=1;j<line.length;j++){
                                                                Address a=new Address(line[0],Integer.parseInt(line[j]));
                                                                list.add(a);
                                                }
                                                i++;
                                }
                                Collections. sort(list);
                                
                                 for(Address x:list){
                                                System. out.println(x);
                                                System. out.println("                    " );
                                }
                                
                                 int sum=0;
                                 int n=1;
                                Address start=list.get(0);
                 for(i=0;i<list.size();i++){
                                Address now=list.get(i);
                                 if(sentence.indexOf(now.word )-sentence.indexOf(start.word)==1&&now. index-start.index ==1){
                                                n++;
                                                start. word=now.word ;
                                                start. index=now.index ;
                                } else{
                                                n=1;
                                                start. word=now.word ;
                                                start. index=now.index ;
                                }
 
                                                 if(n==wordnum){
                                                                System. out.println("match is " +now);
                                                                sum++;
                                                                n=1;
                                                }
                                                
                                
                }
                                
                                 /*
                                for (i=0;i<list.size()-2;i++){
                                Address t1=list.get(i);
                                Address t2=list.get(i+1);
                                Address t3=list.get(i+2);
                                if((t1.index+2)==t3.index&&(t2.index+1)==t3.index){
                                                if(t1.add!=t2.add&&t1.add!=t3.add&&t2.add!=t3.add){
                                                                sum++;
                                                }
                                }
                                
                }
                
                
                System.out.println("                                       ");
                System.out.println("sum is "+sum);
                System.out.println("                                       ");
                */
                                 if(sum>0){
                                                context.write(_key, new Text(String.valueOf(sum)));
                                }
                }
 
}
 
 
 

MapReduce 简单的全文搜索2的更多相关文章

  1. MapReduce 简单的全文搜索

    上一个已经实现了反向索引,那么为什么不尝试下全文搜索呢.例如有了 Hello     file3.txt:1; MapReduce     file3.txt:2;fil1.txt:1;fil2.tx ...

  2. Django 博客实现简单的全文搜索

    作者:HelloGitHub-追梦人物 文中所涉及的示例代码,已同步更新到 HelloGitHub-Team 仓库 搜索是一个复杂的功能,但对于一些简单的搜索任务,我们可以使用 Django Mode ...

  3. 如何在MySQL中获得更好的全文搜索结果

    如何在MySQL中获得更好的全文搜索结果 很多互联网应用程序都提供了全文搜索功能,用户可以使用一个词或者词语片断作为查询项目来定位匹配的记录.在后台,这些程序使用在一个SELECT 查询中的LIKE语 ...

  4. window环境下,php+sphinx+coreseek实现简单的中文全文搜索

    就以我个人理解来说,sphinx其实是介于客户端和mysql之间的一个索引表,把数据库的没一条记录假设为文档,那么这个索引表其实保存的就是这条记录的关键词及其对应的文档id 1.sphinx的安装 下 ...

  5. SQLSERVER全文搜索

    SQLSERVER全文搜索 看这篇文章之前请先看一下下面我摘抄的全文搜索的MSDN资料,基本上MSDN上关于全文搜索的资料的我都copy下来了 并且非常认真地阅读和试验了一次,并且补充了一些SQL语句 ...

  6. [Elasticsearch] 全文搜索 (一) 基础概念和match查询

    全文搜索(Full Text Search) 现在我们已经讨论了搜索结构化数据的一些简单用例,是时候开始探索全文搜索了 - 如何在全文字段中搜索来找到最相关的文档. 对于全文搜索而言,最重要的两个方面 ...

  7. MySQL 全文搜索支持, mysql 5.6.4支持Innodb的全文检索和类memcache的nosql支持

    背景:搞个个人博客的全文搜索得用like啥的,现在mysql版本号已经大于5.6.4了也就支持了innodb的全文搜索了,刚查了下目前版本号都到MySQL Community Server 5.6.1 ...

  8. MySQL 全文搜索支持

    MySQL 全文搜索支持 从MySQL 4.0以上 myisam引擎就支持了full text search 全文搜索,在一般的小网站或者blog上可以使用这个特性支持搜索. 那么怎么使用了,简单看看 ...

  9. Apache Solr采用Java开发、基于Lucene的全文搜索服务器

    http://docs.spring.io/spring-data/solr/ 首先介绍一下solr: Apache Solr (读音: SOLer) 是一个开源.高性能.采用Java开发.基于Luc ...

随机推荐

  1. 把vim变成C编辑器

    转载一篇博客,地址是http://my.oschina.net/kutengshe/blog/423497

  2. 什么是dtd文件,为什么需要

    DTD为英文Document Type Definition,中文意思为“文档类定义”.DTD肩负着两重任务:一方面它帮助你编写合法的代码,另一方面它让浏览器正确地显示器代码.也许你会问它们居然有这样 ...

  3. 拓扑排序<反向拓扑+有向环的判断>

    题目链接 #include <set> #include <map> #include <cmath> #include <queue> #includ ...

  4. UIRoot

    scalingStyle: Flexible:固定大小,不管设备屏幕的大小是多少,都以固定的像素显示UI Constrained: 可适应屏幕 如要使640*480像素的背景图适应屏幕,要如下设置 c ...

  5. ios 集合总结

    NSArray 用于对象有序集合(相当于是数组) 它有两个限制: 1. 它只能存储objective-c的对象,但不能存储C中的基本数据类型,如int , float, enum, struct等. ...

  6. BootStrap详解之(二)

    六.内容 Bootstrap 将全局 font-size 设置为 14px,line-height 设置为 1.428 中心内容 .lead 将字体大小.加粗.行高修改 (无卵高深用) 标记 mark ...

  7. android点滴之HandlerThread的用法

    转载请注明出处:http://blog.csdn.net/lskshz/article/details/25364909 一.介绍 HandlerThread继承自Thread,当线程开启时,也就是它 ...

  8. ntfs mount fail after upgrade win10

    http://www.cnblogs.com/wangbo2008/p/3782730.html linux下挂载NTFS分区错误修复   今天在linux下打开win的NTFS硬盘总是提示出错了,而 ...

  9. httpwebrequest 模拟登录 获取cookies 以前的代码,记录备忘!

    2个类,一个基类,一个构建头信息调用类 关于如何获取到post中的内容,你之需要用http抓包工具把你与目标网站的请求信息抓下来后,打开分析下按照抓下来的包中的数 据进行构建就行了 using Sys ...

  10. 深入理解最强桌面地图控件GMAP.NET ---离线地图

    enjoyeclipse 深入理解最强桌面地图控件GMAP.NET ---离线地图 这章会介绍GMAP.NET的核心功能之一:离线地图.这个功能可以满足很多政府项目.保密项目.或者由于种种原因不能上网 ...