Adding functions in search index of loudant

后端 未结 1 1618
谎友^
谎友^ 2021-01-29 05:15

I have a Json document in cloudant as:

{
  \"_id\": \"3-f812228f45b5f4e4962505561953ew245\",
  \"_rev\": \"3-f812228f45b5f4e496250556195372b2\",
  \"wiki_page\":         


        
相关标签:
1条回答
  • 2021-01-29 05:57

    Update

    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"
    

    Here is the final script:

    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]);
        }
      }
    }
    

    Updated JSFiddle:

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

    Original Answer

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

    https://jsfiddle.net/14e7L3gw/

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

    Here is what is happening:

    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.
    0 讨论(0)
提交回复
热议问题