在cloudant中构建Json搜索索引
我已经为示例数据库建立了搜索索引,并成功地在cloudant中运行了一个搜索查询。例如,我有一个数据库:在cloudant中构建Json搜索索引,json,search,indexing,lucene,cloudant,Json,Search,Indexing,Lucene,Cloudant,我已经为示例数据库建立了搜索索引,并成功地在cloudant中运行了一个搜索查询。例如,我有一个数据库: { "_id": "aardvark", "_rev": "3-fe45a3e06244adbe7ba145e74e57aba5", "min_weight": 40, "max_weight": 65, "min_length": 1, "max_length": 2.2, "latin_name": "Orycteropus afer", "wiki_pa
{
"_id": "aardvark",
"_rev": "3-fe45a3e06244adbe7ba145e74e57aba5",
"min_weight": 40,
"max_weight": 65,
"min_length": 1,
"max_length": 2.2,
"latin_name": "Orycteropus afer",
"wiki_page": "http://en.wikipedia.org/wiki/Aardvark",
"class": "mammal",
"diet": "omnivore"
}
对于索引“\u id”或“class”,我可以创建如下搜索索引:
function(doc){
index("default", doc._id);
...
}
或
然而,我不知道如何对Json格式进行索引。例如,我有一个Json格式:
"_id": "08ff683d86484139",
"_rev": "4-cf6f34c6a2a22780a646b86a3f8d1848",
"lastUpdated": "2014-01-31 00:00:00",
"issueId": 62655,
"isThirdParty": true,
"dateCreated": "2014-01-29 00:00:00",
"attributeCollection": {
"attributeArray": [
{
"updateable": false,
"lookup": "issuetype",
"issueAttributeDefinitionId": 13,
"attributeType": 1,
"name": "Web Type",
"value": [
"Improper Neutralization of Input During Web Page Generation"
]
},
"appReleaseId": 57,
"hash": "953b33eca52938ab2d21e27eb171998b"
}
我的问题是如何索引Json格式的“attributeCollection”中的属性。特别是,如何为
"name": "Web Type",
及
我不相信您可以在子文档的数组字段上创建json索引,但是您可以创建一个搜索索引,用于根据文档结构查询
name
和value
字段
+
,然后选择New Search Indexfunction (doc) {
if (doc.attributeCollection && doc.attributeCollection.attributeArray) {
for (var i=0; i<doc.attributeCollection.attributeArray.length; i++) {
if (doc.attributeCollection.attributeArray[i].name) {
index("name", doc.attributeCollection.attributeArray[i].name, { store : true });
}
if (doc.attributeCollection.attributeArray[i].value) {
for (var j=0; j<doc.attributeCollection.attributeArray[i].value.length; j++) {
index("value", doc.attributeCollection.attributeArray[i].value[j], { store : true });
}
}
}
}
}
以下是此查询的示例响应:
{
"total_rows":1,
"bookmark":"g2wAAAABaANkAChkYmNvcmVAZGIyLmJtLWRhbC1zdGFuZGFyZDIuY2xvdWRhbnQubmV0bAAAAAJiQAAAAGJf____amgCRj9_92eAAAAAYQBq",
"rows":[
{
"id":"08ff683d86484139",
"order":[
0.0078043024986982346,
0
],
"fields":{
"name":"Web Type",
"value":"Improper Neutralization of Input During Web Page Generation"
},
"doc":{
"_id":"08ff683d86484139",
"_rev":"1-f4f6b73bbf3420412a5619e74f4cae00",
"lastUpdated":"2014-01-31 00:00:00",
"issueId":62655,
"isThirdParty":true,
"dateCreated":"2014-01-29 00:00:00",
"attributeCollection":{
"attributeArray":[
{
"updateable":false,
"lookup":"issuetype",
"issueAttributeDefinitionId":13,
"attributeType":1,
"name":"Web Type",
"value":[
"Improper Neutralization of Input During Web Page Generation"
]
}
]
},
"appReleaseId":57,
"hash":"953b33eca52938ab2d21e27eb171998b"
}
}
]
}
您可以在此处了解有关如何创建和查询搜索索引的更多信息:
Nguyen,我假设您试图解决的用例与名称和/或值过滤相关。我在下面提供了一个解决方案,但它不使用“json”索引。如果下面的答案不能解决您的用例,请让我知道它的不足之处。谢谢
function (doc) {
if (doc.attributeCollection && doc.attributeCollection.attributeArray) {
for (var i=0; i<doc.attributeCollection.attributeArray.length; i++) {
if (doc.attributeCollection.attributeArray[i].name) {
index("name", doc.attributeCollection.attributeArray[i].name, { store : true });
}
if (doc.attributeCollection.attributeArray[i].value) {
for (var j=0; j<doc.attributeCollection.attributeArray[i].value.length; j++) {
index("value", doc.attributeCollection.attributeArray[i].value[j], { store : true });
}
}
}
}
}
https://<yourcloudanthost>/<databasename>
/_design/attributes
/_search/by_name_value
?limit=10
&q=name:%27Web+Type%27+OR+value:%27Improper%20Neutralization%20of%20Input%20During%20Web%20Page%20Generation%27
&include_docs=true
&q=
name:'Web Type'
OR
value:'Improper Neutralization of Input During Web Page Generation'
{
"total_rows":1,
"bookmark":"g2wAAAABaANkAChkYmNvcmVAZGIyLmJtLWRhbC1zdGFuZGFyZDIuY2xvdWRhbnQubmV0bAAAAAJiQAAAAGJf____amgCRj9_92eAAAAAYQBq",
"rows":[
{
"id":"08ff683d86484139",
"order":[
0.0078043024986982346,
0
],
"fields":{
"name":"Web Type",
"value":"Improper Neutralization of Input During Web Page Generation"
},
"doc":{
"_id":"08ff683d86484139",
"_rev":"1-f4f6b73bbf3420412a5619e74f4cae00",
"lastUpdated":"2014-01-31 00:00:00",
"issueId":62655,
"isThirdParty":true,
"dateCreated":"2014-01-29 00:00:00",
"attributeCollection":{
"attributeArray":[
{
"updateable":false,
"lookup":"issuetype",
"issueAttributeDefinitionId":13,
"attributeType":1,
"name":"Web Type",
"value":[
"Improper Neutralization of Input During Web Page Generation"
]
}
]
},
"appReleaseId":57,
"hash":"953b33eca52938ab2d21e27eb171998b"
}
}
]
}