上一个全文搜索实现了模糊查找,这个主要实现了精确查找,就是比如你查找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. 使用React Native一年后的感受

    转载自:http://www.dobest.me/blog/2016/06/12/%E4%BD%BF%E7%94%A8React%20Native%E4%B8%80%E5%B9%B4%E5%90%8E ...

  2. 在win7/8/10鼠标右键添加“管理员取得所有权”

    Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\*\Shell\TakeAuthority]"icon"=" ...

  3. 提高MySQL查询速度

    参考百度知道 关于mysql处理百万级以上的数据时如何提高其查询速度的方法 最近一段时间由于工作需要,开始关注针对Mysql数据库的select查询语句的相关优化方法. 由于在参与的实际项目中发现当m ...

  4. ARP/代理ARP

    1.ARP首先讲到ARP,ARP是地址解析协议,它的作用是在以太网环境下,通过3层的IP地址来找寻2层的MAC地址,得到一张ARP缓存表.转发数据的时候根据ARP缓存表来进行传输.下图详细说明数据传输 ...

  5. 篇一:eclipse创建maven工程

    一.概览 maven创建的项目主要分为三类:war(网页工程).jar(Java工程).pom(父工程); war:网页工程,包含webapp,用于view层 jar:Java工程,用于提供方法.se ...

  6. jsonp的简单实现

    jsonp: function(url, data, callback){ if( wfQuery.isFunction(data) ){ callback = data; data = {}; } ...

  7. 很好的一个dp题目 Codeforces Round #326 (Div. 2) D dp

    http://codeforces.com/contest/588/problem/D 感觉吧,这道题让我做,我应该是不会做的... 题目大意:给出n,L,K.表示数组的长度为n,数组b的长度为L,定 ...

  8. gen_grant_exec.sql

    set echo off feedback off verify off pagesize 0 linesize 120 define v_grantee                 = & ...

  9. 2016大连网络赛 Weak Pair

    Weak Pair Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Prob ...

  10. PAT (Advanced Level) 1092. To Buy or Not to Buy (20)

    简单题. #include<cstdio> #include<cstring> ; char s1[maxn],s2[maxn]; ]; ]; int main() { sca ...