JavaScript - Ayuda con XSearch Engine version5.2

   
Vista:

Ayuda con XSearch Engine version5.2

Publicado por Jose Ramon (1 intervención) el 11/03/2013 09:50:40
Buenas a todos...

Uso el xsearch 5.2 para hacer una búsqueda dentro de una intranet... pero después de tener todo funcionando... me encuentro con dos problemas... el primero es el indexado que es manual, pero tampoco es mucha la frecuencias de cambio... y se soporta ese problema... el problema mas grande es que necesito ordenar los resultados de mayor a menor... y no encuentro la forma...

Les dejo aqui el codigo para ver si alguien me puede echar una mano...

Gracias de Antemano...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/* XSearch Engine version5.2      */ 
/* developed by P.Bestebroer      */ 
/* http://www.dynamic-core.net       */ 
 
function trecords(){
   this.index=(trecords.count++)
   this.link=''
   this.keywords=''
   this.description=''
   return this
}
 
trecords.prototype.set=function(link,keywords,description) {
   this.link=link
   this.keywords=keywords
   this.description=description
}
 
trecords.prototype.searchstring=function() { return this.link+' '+this.keywords+' '+this.description }
trecords.prototype.count=0
 
function add(link,keywords,description) {
   al=records.length
   records[al]=new trecords()
   records[al].set(link,keywords,description)
}
 
records = new Array()
finds=0
sites=0
version=""
andresult=false
SortResults=true
display_start=0
displast=5
 
function qsort(f, l){
// not used anymore, causes stack-overflow in large database 
// Qsort function by Rob B. 
   var a=f
   var b=l
   var s
   var m = results[(a+b)>>1].val
      while (a<=b) {
      while (results[a].val>m) {a++}
      while (m>results[b].val) {b--}
 
      if (a<=b) {
                  s=results[a]
         results[a]=results[b]
         results[b]=s
               a++
               b--
           }
   }
   if (f<b) this.qsort(f, b)
   if (a<l) this.qsort(a, l)
}
 
function bsort() {
   for (var i=results.length-1; i>=0; i--) {
      for (var j=i; j>=0; j--) {
         if (results[i].val>results[j].val) {
            s=results[i]
            results[i]=results[j]
            results[j]=s
         }
      }
   }
}
 
function searchAll(keyword){
   var timeA=new Date()
   var nw=0
   finds=0
   sites=0
 
   var x = parseIt(keyword)
   if(x == -1) return
   total_keywords=x
 
   document.open()
   document.clear()
   document.write('<link rel="stylesheet" href="xsearch.css">')
   AddBody()
 
   if (keyword.length>80) keyword=keyword.substring(0,80)+"..."
 
   results=new Array()
   for (q=0; q<records.length; q++) {
      results[q]=new Array()
      results[q].rec=q
      results[q].val=0
   }
 
   for (nw=0; nw<keywords.length; nw+=1) search(keywords[nw])
   if (andresult) {
      for (a=0; a<results.length; a+=1) {
         if (results[a].val>0) {
            if (results[a].val<=(total_keywords-1)<<1) {
               results[a].val=0
               sites-=1
            }
         }
      }
   }
   if (SortResults && keywords!='[all]') bsort()
 
   // Now we build the output page 
   displast=display_start
   displast+=5
   if (displast>sites) displast=sites
 
   var timeB=new Date()
 
   if (finds==0) { display_start=-1; displast=0 }
   document.write("<center><h3>Resultados <b><span class='azur'>"+(display_start+1)+"</span>-<span class='azur'>"+(displast)+"</span></b> de <b><span class='azur'>"+sites+"</span></b> para <b><span class='azur'>"+keyword+"</span>.</b>  Tiempo de búsqueda: <b><span class='azur'>"+((timeB-timeA)/1000)+"</span></b> segundos.</h3></center>")
 
   if (displast>sites && finds!=0) displast=sites+1
 
 
   if (finds==0) {
      document.write("<h3><font >No se encontró <b>''"+keyword+"''</b></font></h3>"+
                  "<p>La palabra - <b>"+keyword+"</b> - no se encuentra en la base de datos.</p>"+
                  "<LI>Verifique que haya deletreado bien la palabra.</li>"+
                  "<LI>Intente nuevamente utilizando otra palabra clave.</li>"+
 
                  "<LI>Pruebe ingresando varias palabras en un mismo campo.</li></span>"+
                  "</p>")
      DisplayXSearch()
      document.close()
      return
   }
 
   q2=display_start
   q3=displast
   for (q=display_start; q<q3; q+=1) {
      if (results[q].val>0) {
         rc=results[q].rec
         document.write("<span class='xtitle'>"+records[rc].link+"</span><br>")
         x1=records[rc].link.indexOf('http://')
         if (x1==-1) x1=records[rc].link.indexOf('href=')+5
         else x1+=7
         x2=records[rc].link.indexOf('>')-1
         if (x1>0 && x2>0) {
            tmp=records[rc].link.substring(x1,x2)
            x2=tmp.indexOf(' ')
            if (x2>0) tmp=tmp.substring(0,x2)
            if (tmp.substring(0,1)=="'") tmp=tmp.substring(1,tmp.length-2)
            if (tmp.substring(0,1)=='"') tmp=tmp.substring(1,tmp.length-1)
            document.write("<table border=0 width=800><tr><td><span class='xresult'>"+records[rc].description+"</span></td></tr></table><span class='xlocation'>"+tmp+"</span><br><br>")
         }
         q2++
         }
   }
 
   if (finds>5) {
      document.write("<BR>")
      pages=Math.round(finds/5)
      if (finds%10<6)   pages++
 
      // Create the parameter string 
      paramstring=searchname+"?keywords="+keyword+"&and="+andresult+"&sort="+SortResults
 
      document.write("<center><span class='xsmall'>")
      if (display_start>0) document.write("<a href='"+paramstring+"&disp="+(display_start-5)+"'>anterior</a>")
      document.write("&nbsp;&nbsp;&nbsp;")
 
      for (i=1; i<=pages; i+=1){
         if ((((i-1)*5)+1)<=sites) document.write("<a href='"+paramstring+"&disp="+(((i-1)*5))+"'>"+i+"</a>&nbsp&nbsp ")
      }
      document.write("&nbsp;&nbsp;&nbsp;")
      if (displast<=sites) document.write("<a href='"+paramstring+"&disp="+(displast)+"'>siguiente</a>")
      document.write("</span></center>")
   }
   DisplayXSearch()
   document.close()
}
 
function Cat() {
   document.open()
   document.clear()
   document.write('<link rel="stylesheet" href="xsearch.css">')
   AddBody()
   DisplayXSearch()
   document.close()
}
 
function stripInput(key) {
   while(key.substring(0,1) == ","  || key.substring(0,1) == " " ) key = key.substring(1,key.length)
   while(key.substring(key.length-1,key.length) == "," || key.substring(key.length-1,key.length) == " ") key = key.substring(0,key.length-1)
   return key
}
 
function parseIt(key) {
   key=stripInput(key)+" "
   var y=0
 
   while(key.indexOf(" ") > 0) {
      if (key.substring(0,1)=='"') {
         var pos=key.indexOf('"',2)
         keywords[y]=key.substring(1,pos)
         keywords[y]=stripInput(keywords[y])
         y++
         key=key.substring(pos+1,key.length)
      } else {
         var pos=key.indexOf(' AND ')
         if ((pos>0) && (key.indexOf(' ')>=pos)) {
            pos=key.indexOf(' ',pos+5)
            keywords[y]=key.substring(0,pos)
            keywords[y]=stripInput(keywords[y])
            y++
            key=key.substring(pos+1,key.length)
            if (key.substring(0,4)=='AND ') {
               pos=keywords[y-1].indexOf(' ')+5
               key=keywords[y-1].substring(pos,keywords[y-1].length)+' '+key
            }
         } else {
              var pos=key.indexOf(' OR ')
              if ((pos>0) && (key.indexOf(' ')>=pos))   {
               pos=key.indexOf(' ')
               keywords[y]=key.substring(0,pos)
               keywords[y]=stripInput(keywords[y])
               if (keywords[y]!=keywords[y-1])   y++
               key=key.substring(pos+1,key.length)
               pos=key.indexOf('OR ')
               key=key.substring(pos+3,key.length)
               pos=key.indexOf(' ')
               keywords[y]=key.substring(0,pos)
               keywords[y]=stripInput(keywords[y])
               y++
               key=key.substring(pos+1,key.length)
               if (key.substring(0,3)=='OR ') key=keywords[y-1]+' '+key
            } else {
               var pos = key.indexOf(" ")
               keywords[y]=key.substring(0,pos)
               keywords[y] = stripInput(keywords[y])
               y++
               if(y > 50) return -1
               key=key.substring(pos+1,key.length)
            }
         }
      }
   }
   return y-1
}
 
var keywords = new Array()
var results
 
function AddBody() {
   var keytext='"'+searchname+'?keywords="+'
   var andtext='"&and="+'
 
   document.write('<script>function doSearch(){'+
                  'searchwords=document.searchform.searchwords.value; '+
                  'while (searchwords.indexOf(" ")>-1){ pos=searchwords.indexOf(" ");'+
                  'searchwords=searchwords.substring(0,pos)+"+"+searchwords.substring(pos+1); }'+
                  'document.location='+keytext+' searchwords+'+andtext+'"0"}'+
                  '<'+'/'+'script>'
                  )
 
   templateBody()
   document.write("<form name='searchform' method='post' action='javascript:doSearch()'><table border='0' width='100%'><tr><td align='center'><font face='Arial, Helvetica, sans-serif' size='3'><input name='searchwords' type='text' size='30'>&nbsp;&nbsp;<a href='javascript:doSearch()'>"+ButtonCode+"</a></font></td></tr></table></form>")
   if (usebannercode) bannerCode()
}
 
function DisplayXSearch() {
   // This line can be removed, but please don't add anything like 
   // copyright by <your name here> because that's not allowed, and 
   // if you remove or change this line, make sure there is some credit here 
   // and a link to www.dynamic-core.net so that other people can also 
   // get their hands on this FREE search engine.. Thank you. 
   document.write("")
   templateEnd()
}
 
function search(keyword) {
   var hit=0
   var addcomplete=0
 
   for (q=0; q<records.length; q++) {
      addcomplete=0
      search_parm=" "+records[q].searchstring()+" "
      search_parm=search_parm.toLowerCase()
 
      if (keyword.indexOf(' AND ')>0) {
         firstword=keyword.substring(0,keyword.indexOf(' ')).toLowerCase()
         lastword=keyword.substring(keyword.indexOf(' AND ')+5,keyword.length).toLowerCase()
         if ((search_parm.indexOf(" "+firstword+" ") != -1) && (search_parm.indexOf(" "+lastword+" ")!= -1 )) {
            hit++
            finds++
            if(hit<2) {
               if (results[q].val==0) sites++
               results[q].val +=2
            }
         }
      } else {
         keyword=keyword.toLowerCase()
         if ((search_parm.indexOf(" "+keyword+" ") != -1) ||(keyword=="[all]")) {
            hit++
            finds++
            if(hit<2) {
               if (results[q].val==0) sites++
               results[q].val+=2
            }
         } else {
            // check for a half hit (ie. search:share find:SHAREware) 
            if (search_parm.indexOf(keyword) != -1)   {
               hit++
               finds++
               if(hit < 2) {
                  if (results[q].val==0) sites++
                  results[q].val+=1
                  x=search_parm.indexOf(keyword)+keyword.length
                  pos=search_parm.substring(1,x-keyword.length)
                  while (pos.indexOf(" ")!=-1) {
                     y=pos.indexOf(" ")
                     pos=pos.substring(y+1,pos.length)
                  }
                  if (pos.length<=2) addcomplete++
 
                  pos=search_parm.substring(x,search_parm.length)
                  fullresult=search_parm.substring(x,x+pos.indexOf(" "))
 
                  if (fullresult.length<=2) addcomplete++
                  if (addcomplete>1) results[q].val+=1
               }
            }
         }
      }
      hit=0
   }
}
 
var searchwords = ''
var newload = true
 
function initXsearch() {
   if (searchwords!='') searchAll(searchwords)
   else if (newload) Cat()
}
 
function tparams(){
   parameters=document.location.search
   parameters=unescape(parameters.substring(1,parameters.length)+'&')
 
   this.params=new Array()
   i=0
   while (parameters.indexOf('&',0)!=-1) {
      al=this.params.length
      this.params[al]=new Array()
 
      tmp=parameters.substring(0,parameters.indexOf('&',0))
      parameters=parameters.substring(parameters.indexOf('&',0)+1)
 
      if (tmp.indexOf('=')!=-1) {
         this.params[al].command=tmp.substring(0,tmp.indexOf('='))
         this.params[al].value=tmp.substring(tmp.indexOf('=')+1)
      } else {
         this.params[al].command=tmp
         this.params[al].value=''
      }
   }
 
   return this
}
tparams.prototype.getValue=function(param){
   value=''
   param=param.toLowerCase()
   al=this.params.length
   for (var i=0; i<al; i+=1) if (this.params[i].command==param) value=this.params[i].value
   return value
}
 
params=new tparams()
if (params.getValue('keywords')!=''){
   searchwords=params.getValue('keywords')
   origsearchwords=searchwords
   while (searchwords.indexOf('+')>-1) {
      pos=searchwords.indexOf('+')
      searchwords=searchwords.substring(0,pos)+' '+searchwords.substring(pos+1)
   }
}
if (params.getValue('sort')!='')
   if (params.getValue('sort')=='0' || params.getValue('sort')=='false') SortResults=false
   else SortResults=true
if (params.getValue('and')!='')
   if (params.getValue('and')=='0' || params.getValue('and')=='false') andresult=false
   else andresult=true
if (params.getValue('disp')!='') display_start=parseInt(params.getValue('disp'))


Como dije antes... Gracias de antemano...
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder