上一个全文搜索实现了模糊查找,这个主要实现了精确查找,就是比如你查找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. Note over Chinese Encodings

    I been confused years ago. Till recently I collected my thoughts together, and now I am clear about ...

  2. Co-Debugging JNI with Android Studio and Visual Studio

    Tutorials > Android > Integration with other tools > Co-Debugging JNI with Android Studio a ...

  3. 快学Scala-第一章 基础

    知识点: Scala程序并不是一个解释器,实际发生的是,你输入的内容被快速的编译成字节码,然后这段字节码交由Java虚拟机执行. 以val定义的值是一个常量,以var定义的值是一个变量,声明值或变量但 ...

  4. HDU-1301 Jungle Roads(最小生成树[Prim])

    Jungle Roads Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total ...

  5. Node.js学习 - Web Server

    Client - 客户端,一般指浏览器,浏览器可以通过 HTTP 协议向服务器请求数据. Server - 服务端,一般指 Web 服务器,可以接收客户端请求,并向客户端发送响应数据. Busines ...

  6. jq的合成事件

    jq中有两个合成事件 hover()和toggle() 1.hover() hover方法用于模拟光标悬停事件.当光标移动到元素上时,会触发指定的第一个函数(enter),当光标移出这个元素时,会触发 ...

  7. 【项目笔记】布局文件报错Suspicious size: this will make the view invisible, probably intended for layout_width

    写着写着就懵逼了,一直以为布局文件没写错啊,horizontal就是竖直啊,原来布局文件报错,不仅仅需要从报错的地方解决问题,还需要从其他地方去分析. 很明显是方向orientation选错了,应该写 ...

  8. Linux格式化硬盘 常用命令小记

    今天新蛋上订购了一块1TB的硬盘打算装Ubuntu,当然先要做好功课,查一下注意事项啦! 基本功,格式化命令,以格式化 /dev/sda1 分区为例:$ sudo umount /dev/sda1   ...

  9. 转 图片缓存之内存缓存技术LruCache,软引用

    每当碰到一些大图片的时候,我们如果不对图片进行处理就会报OOM异常,这个问题曾经让我觉得很烦恼,后来终于得到了解决,那么现在就让我和大家一起分享一下吧.这篇博文要讲的图片缓存机制,我接触到的有两钟,一 ...

  10. 优化C/C++代码的小技巧(转)

    源:http://www.cnblogs.com/lizhenghn/p/3969531.html 说明: 无意看到一篇小短文,猜测作者应该是一个图形学领域的程序员或专家,介绍了在光线(射线)追踪程序 ...