我找不到太多关于如何正确定义索引函数的文档,这样我就可以对我需要的信息进行全文搜索。
我使用了炼金术API在我的文档中添加了“实体”json。例如,我有一份文件,其内容如下:
"_id": "redacted",
"_rev": "redacted",
"session": "20152016",
"entities": [
{
"relevance": "0.797773",
"count": "3",
"type": "Organization",
"text": "California Constitution"
},
{
"relevance": "0.690092",
"count": "1",
"type": "Organization",
"text": "Governors Highway Safety Association"
}
]我还没有找到任何代码片段,说明如何构造查看嵌套json的搜索索引函数。
我试图索引整个对象似乎是不正确的。这是完整的设计文件:
{
"_id": "_design/entities",
"_rev": "redacted",
"views": {},
"language": "javascript",
"indexes": {
"entities": {
"analyzer": "standard",
"index": "function (doc) {\n if (doc.entities.relevance > 0.5){\n index(\"default\", doc.entities.text, {\"store\":\"yes\"});\n }\n\n}"
}
}
}并且搜索索引格式化得更清楚一些
function (doc) {
if (doc.entities.relevance > 0.5){
index("default", doc.entities.text, {"store":"yes"});
}
}按照下面的建议添加for循环很有意义。但是,我仍然不能返回任何结果。我的查询是"search/entities?q=Governors“
服务器响应是:{"total_rows":0,“书签”:“g2o”,“行”:[]}
发布于 2015-12-04 21:52:11
"for..in“样式循环似乎不起作用。但是,我确实使用更标准的循环循环来获得结果。
function (doc) {
if(doc.entities){
var arrayLength = doc.entities.length;
for (var i = 0; i < arrayLength; i++) {
if (parseFloat(doc.entities[i].relevance) > 0.5)
index("default", doc.entities[i].text);
}
}
}干杯!
发布于 2015-12-04 20:34:49
您需要对doc.entities数组中的元素进行循环。
function (doc) {
for(entity in doc.entities){
if (parseFloat(entity.relevance) > 0.5){
index("default", entity.text, {"store":"yes"});
}
}
}发布于 2015-12-21 16:54:11
这就是我试过的:
function(doc){
if(doc.entities){
for( var p in doc.entities ){
if (doc.entities[p].relevance > 0.5)
{
index("entitiestext", doc.entities[p].text, {"store":"yes"});
}
}
}
}使用的查询字符串:"q=entitiestext:California Constitution&include_docs=true“结果:
{
"total_rows": 1,
"bookmark": "xxxx",
"rows": [
{
"id": "redacted",
"order": [
0.03693288564682007,
1
],
"fields": {
"entitiestext": [
"Governors Highway Safety Association",
"California Constitution"
]
},
"doc": {
"_id": "redacted",
"_rev": "4-7f6e6db246abcf2f884dc0b91451272a",
"session": "20152016",
"entities": [
{
"relevance": "0.797773",
"count": "3",
"type": "Organization",
"text": "California Constitution"
},
{
"relevance": "0.690092",
"count": "1",
"type": "Organization",
"text": "Governors Highway Safety Association"
}
]
}
}
]}
使用的查询字符串: q=entitiestext:California组合
结果:
{
"total_rows": 1,
"bookmark": "xxxx",
"rows": [
{
"id": "redacted",
"order": [
0.03693288564682007,
1
],
"fields": {
"entitiestext": [
"Governors Highway Safety Association",
"California Constitution"
]
}
}
]}
https://stackoverflow.com/questions/34096812
复制相似问题