我找不到关于如何正确定义索引函数的文档,以便我可以对我需要的信息进行全文搜索。
我已经使用Alchemy 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循环非常有意义。 但是,我仍然无法返回任何结果。 我的疑问是 “https://user.cloudant.com/calbills/_design/entities/_search/entities?q=Governors”
服务器响应是: { “TOTAL_ROWS”:0, “书签”: “g2o”, “行”:[]}
答案 0 :(得分:3)
" for..in"风格循环似乎不起作用。 但是,我确实使用更标准的for循环来获得结果。
process.exit()
干杯!
答案 1 :(得分:1)
您需要循环doc.entities数组中的元素。
function (doc) {
for(entity in doc.entities){
if (parseFloat(entity.relevance) > 0.5){
index("default", entity.text, {"store":"yes"});
}
}
}
答案 2 :(得分:0)
这就是我的尝试:
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:加州宪法
结果:
{
"total_rows": 1,
"bookmark": "xxxx",
"rows": [
{
"id": "redacted",
"order": [
0.03693288564682007,
1
],
"fields": {
"entitiestext": [
"Governors Highway Safety Association",
"California Constitution"
]
}
}
]
}