上一个全文搜索实现了模糊查找,这个主要实现了精确查找,就是比如你查找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. Java的引用c++的引用和C指针的区别

    Java的引用本质上就是C中的指针,而c++的引用则完全不同:有一个类 class Point { int x; int y;} 同样的一个Point p; 在Java中p表示一个引用,它等同于C语言 ...

  2. ReactiveCocoa & FRP & MVVM

    Functional Reactive Programming(以下简称FRP)是一种响应变化的编程范式.先来看一小段代码 a = 2 b = 2 c = a + b // c is 4 b = 3 ...

  3. ReactiveCocoa / RxSwift 笔记一

    原创:转载请注明出处 ReactiveCocoa / RxSwift Native app有很大一部分的时间是在等待事件发生,然后响应事件,比如 1.等待网络请求完成, 2.等待用户的操作, 3.等待 ...

  4. gcc编译错误表

    conversion from %s to %s not supported by iconv”iconv 不支持从 %s 到 %s 的转换” iconv_open”iconv_open” no ic ...

  5. UVALive 2324 Human Gene Functions(动态规划)

    题意:求出将两个字符串改成一样长度所能形成最大的相似度. 思路:这个可以说是编辑距离的一个变形,编辑距离最终状态时要两个字符串完全一致,这个就是要求长度一样,而且这个只允许插入“—”这一个字符.模仿编 ...

  6. 滑轮关节(b2PulleyJoint)

    package{ import Box2D.Collision.b2AABB; import Box2D.Collision.b2RayCastInput; import Box2D.Collisio ...

  7. reflow和repaint区别?

    作者:zccst 重绘和重排之前也知道,但也没有可以详细了解他们的机制,区别,以及对性能的影响. A repaint occurs when changes are made to an elemen ...

  8. time_wait和clost_wait说明

    在服务器的日常维护过程中,会经常用到下面的命令: netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}' 它会显示例如 ...

  9. 转 ORACLE数据库它可以存储 中文 字节或字符

    一:因为ORACLE数据库它可以存储字节或字符,例如 CHAR(12 BYTE) CHAR(12 CHAR)的意义是不同的.一般来说默认是存储字节,你可以查看数据库参数NLS_LENGTH_SEMAN ...

  10. ormlite 批处理操作

    提高ormlite的批处理速度 http://stackoverflow.com/quegoogstions/11761472/ormlites-createorupdate-seems-slow-w ...