Javascript elasticlunr.js不显示搜索查询结果

Javascript elasticlunr.js不显示搜索查询结果,javascript,jquery,search,Javascript,Jquery,Search,我用过搜索引擎 我编辑了他们的示例源代码 $('input').bind("enterKey", function (e) { var value_test = $("#inputSuccess1").val(); if ($(this).val() < 2) return var config = $('#configuration').val(); config.trim(); var json_config = null; if (

我用过搜索引擎

我编辑了他们的示例源代码

$('input').bind("enterKey", function (e) {

    var value_test = $("#inputSuccess1").val();

    if ($(this).val() < 2) return
    var config = $('#configuration').val();
    config.trim();
    var json_config = null;
    if (config != '') {
        json_config = new elasticlunr.Configuration(config, idx.getFields()).get();
    }

    var query = value_test;
    var results = null;

    console.log(query);

    if (json_config == null) {
        results = idx.search(query).map(function (result) {
            console.log(result);
            return questions.filter(function (q) {
                console.log(q);
                return q.page_id === parseInt(result.ref, 10)
            })[0]
        })
    } else {
        results = idx.search(query, json_config).map(function (result) {
            return questions.filter(function (q) {
                return q.page_id === parseInt(result.ref, 10)
            })[0]
        })
    }
    renderQuestionList(results);
    console.log(results);
});
}))

我使用xampp,当我按下enter键时,就会发生这种情况。这些“空列表”来自上一个.json文件,但id不同。我找不到那部分


它有很多代码,所以我上传到这里(编辑:删除链接。现在修复)

表单
标记之间存在
输入
标记。这意味着,按enter键后,表单将被提交,因此页面将重新加载,从而删除您放置的任何信息,并显示未经修改的结果

在开发人员提供的示例中,使用了
$('input').bind('keyup',debounce(function(){
)。因此,在输入文本时会搜索标记。但是,在您的示例中,控件甚至不会转移到您打算运行的代码

请检查这个 在输入中按enter键时应显示警报

$('input').bind("enterKey",function(e){
    alert();
});
尝试将实际代码与keyup事件一起使用,并将按下的键与enterKey代码(即13)进行比较

$('input').keyup(function(e) {
    // 13 is ENTER
    if (e.which === 13)
    // Perform action
});

我阅读了手册,错过并忽略了node.js部分。正如你所看到的,index_builder.js使用fs,这是我学到的,它使用node.js。你必须使用node.js并运行index_builder.js。这将从你的data.json(存储的搜索结果)构建index.json文件。该代码使用了我必须重建索引的示例源代码。此外,我正在对本地主机使用xampp

Step 1. Download and Install node.js 
Step 2. Run Xampp Shell on elasticlunr project directory
Step 3. Type in node index_builder.js
Step 4. Elastic Search will work with the entered query
这是我的index_builder.js的代码

var elasticlunr = require('./elasticlunr.js'),
fs = require('fs');

var idx = elasticlunr(function () {
this.setRef('page_id');

this.addField('title');
this.addField('tags');
this.addField('body');
this.addField('url');
});



fs.readFile('./example_data.json', function (err, data) {
if (err) throw err;

var raw = JSON.parse(data);

var questions = raw.questions.map(function (q) {
return {
  page_id: q.page_id,
  title: q.title,
  body: q.body,
  url: q.url,
  tags: q.tags.join(' ')
  };
});

questions.forEach(function (question) {
idx.addDoc(question);
});

fs.writeFile('./example_index.json', JSON.stringify(idx), function (err) {
if (err) throw err;
console.log('done');
});
});

谢谢!

是否可以提供小提琴或plunk?我会尝试将其放入plunkI,我认为我找到了现有链接的解决方案,但我现在会等待plunk。我可以在plunk中传输整个文件夹吗?你指的是plnkr.co,对吗?只要它基于前端。我使用xampp进行本地。我看到输入在表单ho中无论如何,即使表单中有输入,它也能工作,但它会给我一个空结果(参见第三次编辑)。这对网络主机很有帮助。它会按照您刚才说的做。它会重新加载。谢谢。要将答案标记为已接受,请单击答案旁边的复选标记,将其从灰色切换为已填写。
Step 1. Download and Install node.js 
Step 2. Run Xampp Shell on elasticlunr project directory
Step 3. Type in node index_builder.js
Step 4. Elastic Search will work with the entered query
var elasticlunr = require('./elasticlunr.js'),
fs = require('fs');

var idx = elasticlunr(function () {
this.setRef('page_id');

this.addField('title');
this.addField('tags');
this.addField('body');
this.addField('url');
});



fs.readFile('./example_data.json', function (err, data) {
if (err) throw err;

var raw = JSON.parse(data);

var questions = raw.questions.map(function (q) {
return {
  page_id: q.page_id,
  title: q.title,
  body: q.body,
  url: q.url,
  tags: q.tags.join(' ')
  };
});

questions.forEach(function (question) {
idx.addDoc(question);
});

fs.writeFile('./example_index.json', JSON.stringify(idx), function (err) {
if (err) throw err;
console.log('done');
});
});