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

    谷歌在 2016年 I/O 大会上推出了 Firebase 的新版本.Firebase 平台提供了为移动端(iOS和Android)和 Web 端创建后端架构的完整解决方案. 从一开始的移动后端即服务 ...

  2. jquery获得select的文本

    本来以为jQuery("#select1").val();是取得选中的值, 那么jQuery("#select1").text();就是取得的文本. 这是不正确 ...

  3. ViewPager和Fragment组合 v4包下的页面切换

    /* *主页面下 */ //-------------主页面下---------------------- package com.example.viewpagerfragment; import ...

  4. List<Map>去重排序

    数据格式 [ { "id":"d3e8a9d6-e4c6-4dd8-a94f-07733d3c1b59", "parentId":" ...

  5. AJAX(XMLHttpRequest)进行跨域请求方法详解(三)

    注意:以下代码请在Firefox 3.5.Chrome 3.0.Safari 4之后的版本中进行测试.IE8的实现方法与其他浏览不同. 3,带验证信息的请求 身份验证是Web开发中经常遇到的问题,在跨 ...

  6. Entity Framework 6新功能Logging/Store Procedure

    摘要 在Entity Framework6中有两个新的功能,DB Loggin和Stored Procedure的映射 Entity Framework 6已经从Beta版本来到了RC1版本,我们可以 ...

  7. Mysql 随机查询数据

    SELECT * FROM tablename ORDER BY RAND() LIMIT 10

  8. SQL Server2008数据库中删除用户,提示数据库主体在该数据库中拥有 架构,无法删除

    一个数据库,运行在SQL Server 2008下,数据库用户无法删除,在删除时提示“数据库主体在该数据库中拥有架构,无法删除”.原因很简单,就是由于此用户在数据库中拥有某些架构的所有权,将相关架构的 ...

  9. 用CSS让文字居于div的底部

    css对文字的布局上没有靠容器底部对齐的参数,目前使用的一个不错的方法也比较好.就是用position属性来解决,看下面的代码,用position的相对和绝对定位功能也轻松的实现了,文字靠近div低部 ...

  10. PAT (Advanced Level) 1069. The Black Hole of Numbers (20)

    简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...