Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么赢了';在JSON数组中索引多个字符串?_Javascript_Json_Lunrjs - Fatal编程技术网

Javascript 为什么赢了';在JSON数组中索引多个字符串?

Javascript 为什么赢了';在JSON数组中索引多个字符串?,javascript,json,lunrjs,Javascript,Json,Lunrjs,Lunr在查找大多数结果方面做得很好,但我不明白为什么它不会返回JSON数组中包含的多单词字符串 下面是一个示例JSON文件,以了解我的数据的结构: [{ "title": "Rolling Loud", "date": "May 5–7", "location": "Miami, FL, USA", "rock-artists": [], "hh-artists": ["Kendrick Lamar", "Future"], "electroni

Lunr在查找大多数结果方面做得很好,但我不明白为什么它不会返回JSON数组中包含的多单词字符串

下面是一个示例JSON文件,以了解我的数据的结构:

[{
    "title": "Rolling Loud",
    "date": "May 5–7",
    "location": "Miami, FL, USA",
    "rock-artists": [],
    "hh-artists": ["Kendrick Lamar", "Future"],
    "electronic-artists": [],
    "other-artists": []
}]
当我搜索“迈阿密”和“未来”时,Lurn返回节日。然而,当搜索“肯德里克”或“肯德里克·拉马尔”时,伦尔不会返回节日

相关代码:

// initialize lunr
var idx = lunr(function () {
    this.field('id');
    this.field('title', { boost: 3 });
    this.field('date');
    this.field('location');
    this.field('rockArtists', { boost: 3 });
    this.field('hhArtists', { boost: 3 });
    this.field('electronicArtists', { boost: 3 });
    this.field('otherArtists', { boost: 3 });

    // add festivals to lunr
    for (var key in data) {
        this.add({
           'id': key,
           'title': data[key].title,
           'date': data[key].date,
           'location': data[key].location,
           'rockArtists': data[key]['rock-artists'],
           'hhArtists': data[key]['hh-artists'],
           'electronicArtists': data[key]['electronic-artists'],
           'otherArtists': data[key]['other-artists']
        });
    }
});
谢谢

Lunr正在为
hh艺术家
字段编制索引,您应该能够通过在索引中查找一个值来确认这一点:

idx.invertedIndex['Kendrick Lamar']
当文档字段是数组时,lunr假定数组的元素已经被拆分为标记以进行索引。因此,不是将“Kendrick”和“Lamar”作为单独的标记添加到索引中,而是将“Kendrick Lamar”作为单个标记添加

这会在尝试搜索时引发问题,因为搜索“Kendrick Lamar”实际上是搜索“Kendrick”或“Lamar”,因为搜索字符串在空格上拆分以获取令牌。索引中既没有“肯德里克”也没有“拉马尔”,因此没有结果

要获得您希望的结果,您可以将数组转换为字符串,并让lunr处理将其拆分为令牌的操作:

this.add({
  'hhArtists': data[key]['hh-artists'].join(' ')
})
Lunr正在为
hh艺术家
字段编制索引,您应该能够通过在索引中查找一个值来确认这一点:

idx.invertedIndex['Kendrick Lamar']
当文档字段是数组时,lunr假定数组的元素已经被拆分为标记以进行索引。因此,不是将“Kendrick”和“Lamar”作为单独的标记添加到索引中,而是将“Kendrick Lamar”作为单个标记添加

这会在尝试搜索时引发问题,因为搜索“Kendrick Lamar”实际上是搜索“Kendrick”或“Lamar”,因为搜索字符串在空格上拆分以获取令牌。索引中既没有“肯德里克”也没有“拉马尔”,因此没有结果

要获得您希望的结果,您可以将数组转换为字符串,并让lunr处理将其拆分为令牌的操作:

this.add({
  'hhArtists': data[key]['hh-artists'].join(' ')
})

循环中,..in
中的
这个
是什么?我是否应该在函数中调用
add()
?我在从循环外部调用
idx.add
时遇到问题,所以我将它放在函数内部,通过
this
访问变量。
console.log(this)
中用于..在
log中?它返回
Builder{}
,包含大量子项,包括
\u字段:[“id”,“title”,等等]
平均文档长度:98.125
。未尝试
lunrjs
。你能在plnkr上重现这个问题吗?在
循环中,..in
中这个
是什么?我是否应该在函数中调用
add()
?我在从循环外部调用
idx.add
时遇到问题,所以我将它放在函数内部,通过
this
访问变量。
console.log(this)
中用于..在
log中?它返回
Builder{}
,包含大量子项,包括
\u字段:[“id”,“title”,等等]
平均文档长度:98.125
。未尝试
lunrjs
。你们能在plnkr重现这个问题吗?很好。据我所知,文件中没有提到这一点。这是一个很好的发现。据我所知,文件中没有提到这一点。