在响亮的搜索索引中添加功能 [英] Adding functions in search index of loudant

查看:140
本文介绍了在响亮的搜索索引中添加功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在cloudant中有一个Json文档:

I have a Json document in cloudant as:

{
  "_id": "3-f812228f45b5f4e4962505561953ew245",
  "_rev": "3-f812228f45b5f4e496250556195372b2",
  "wiki_page": "http://en.wikipedia.org/wiki/African_lion",
  "name": "african lion",
  "class": "mammal",
  "diet": "herbivore"
}

当我输入afrian lion或lion african的查询时,我想创建一个可以搜索此文档的搜索索引,...

I want to make a search index that can search this document when I input queries as "afrian lion" or "lion african",...

我创建了一个函数,可以在doc.name中返回所有排列的情况以进行索引(这个函数运行良好,并且它也在纯JS环境中检查过)。但是,它在cloudant中不起作用,当我输入查询时输出返回 null

I make a function that can return all cases of permutation in "doc.name" for indexing (This function works well and it also had been checked in pure JS environment). However, it does't work in cloudant, the output return null when i input a query.

这是我做的代码搜索索引:

This is a code that I made in search index:

function(doc){
  var list = [];
  function permute(ss, used, res, level, list){
    if(level==ss.length&&res!==""){
        list.push(res);
        return;
    }
    for(var i=0; i<ss.length; i++){
    console.log("loops");
        if (used[i]===true){
          continue;
        }

        if(level>=0){
          if (res!="" && list.indexOf(res)<0){
              list.push(res.trim());
          }

          used[i]=true;
          permute(ss, used, res+" "+ss[i], level+1, list)
          used[i]=false;
        }
      }
  }
  function permuteword(s){
      var ss=s.split(" ");
      var used = [];
    var res = "";
    list = [];
    permute(ss, used, res, 0, list);
    console.log(list);
  }

  var contentIndex=[];
  contentIndex=permuteword("african lion");
  for(var i=0; i<contentIndex.length; i++){
    index("default", contentIndex[i]);
  }
}

我该如何解决这个问题?

How can i solve the problem?

推荐答案

更新

您的更新看起来不错,但是还有一个问题:您没有从 permuteword 函数返回列表。我相信你还需要删除对 console.log 的调用。一旦我做了这两件事,我就可以使用以下搜索查询来使用Cloudant(我还将你的硬编码调用非洲狮子改回了doc.name):

Your update looks good, but there is still one issue: you are not returning the list from the permuteword function. I believe you also need to remove calls to console.log. Once I did these two things I was able to get it to work with Cloudant using the following search queries (I also changed your hard-coded call with "african lion" back to doc.name):

default:"african"
default:"african lion"
default:"lion"
default:"lion african"

以下是最终脚本:

function(doc){
  var list = [];
  function permute(ss, used, res, level, list){
    if(level==ss.length&&res!==""){
        list.push(res);
        return;
    }
    for(var i=0; i<ss.length; i++){
        if (used[i]===true){
          continue;
        }

        if(level>=0){
          if (res!="" && list.indexOf(res)<0){
              list.push(res.trim());
          }

          used[i]=true;
          permute(ss, used, res+" "+ss[i], level+1, list)
          used[i]=false;
        }
      }
  }
  function permuteword(s){
    var ss=s.split(" ");
    var used = [];
    var res = "";
    list = [];
    permute(ss, used, res, 0, list);
    return list;
  }

  if (doc.name) {
    var contentIndex=permuteword(doc.name);
    for(var i=0; i<contentIndex.length; i++){
      index("default", contentIndex[i]);
    }
  }
}

更新的JSFiddle:

Updated JSFiddle:

https://jsfiddle.net/14e7L3gw/1/

原始答案

我相信您的Javascript存在问题。 permuteword 函数未返回任何结果。看到这个JSFiddle:

I believe there are issues with your Javascript. The permuteword function is not returning any results. See this JSFiddle:

https://jsfiddle.net/14e7L3gw /

注意:我添加了一些日志记录并注释掉了对index的调用。使用浏览器调试器运行以查看输出。

Note: I added some logging and commented out the call to index. Run with your browser debugger to see the output.

以下是发生的事情:


  1. 第一次拨打 permuteword 调用 permute([african,lion],[],,0,[] );

  2. 第一个 if in permuteword 失败,因为级别(0)!= ss.length()(2)和 res ==。

  3. 然后该函数循环通过 ss ,但从不做任何事情,因为 level = 0.

  4. 最终 permuteword 返回一个空数组,因此没有任何内容索引。

  1. The first call to permuteword calls permute(["african","lion"], [], "", 0, []);
  2. The first if in permuteword fails because level (0) != ss.length() (2) and res == "".
  3. Then the function loops through ss, but never does anything because level = 0.
  4. Ultimately permuteword returns an empty array, so nothing gets indexed.

这篇关于在响亮的搜索索引中添加功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆