2011-12-14 88 views
2

我似乎无法理解的弹性搜索方面的工作QueryDSL。以下是我的查询对象,以及我的tags数组的映射。我试图让这些基于标签进行分面导航。每个“元素”将在标签数组中有多个标签关联[并非所有元素都有标签。有些会有一个空阵列。]。每个标签都是具有ID和标签属性的对象。我似乎无法得到面搜索弹性搜索

我已经试过嵌套方面的做法和获取“标签不嵌套”的错误,所以后来我尝试这下面。我没有收到错误,但返回的JSON中没有facets对象。我一直在使用此页面寻求帮助:http://www.elasticsearch.org/guide/reference/api/search/facets/index.html

谁能帮我正确格式化这个和了解这些组织?我感谢任何帮助!

// this is my query object 
{ 
    "sort":{ "created_at":{ "order":"desc" } }, 
    "query":{ 
    "constant_score":{ 
     "filter":{ 
     "and":[ 
      { "missing":{ "field":"parent_id" } }, 
      { "missing":{ "field":"wall_id" } }, 
      { "term":{ "active":true } } 
     ] 
     } 
    } 
    }, 
    "facets":{ 
    "tags":{ "terms":{ "field":"tags.tag" } } 
    } 
} 


// this is the mapping for the tags array 
"tags":{ 
    "type":"nested", 
    "include_in_parent":true, 
    "properties":{ 
    "id":{ "type":"integer" }, 
    "tag":{ "type":"string" } 
    } 
}, 
+1

我不能重现这个使用0.18.2。返回的对象中是否真的没有方面对象? – 2011-12-14 23:17:13

+0

不,我是0.18.5。以下是具有标签的元素的结构:http://pastebin.com/1ruBXKJ9 – swatkins 2011-12-15 00:39:15

回答

7

我确实努力尝试复制这种(使用0.18.5),但没有任何运气。下面的一些细节。第一个例子试图复制你描述的内容。第二个不会将标签映射为include_in_parent,并使用方面请求中的“嵌套”字段。正如你所看到的,在这两种情况下,方面都会返回。

这使我相信,有或者是一些错误的映射的休息或某些文件(不是你提供的样品之一),是造成问题。

当你试图嵌套的方式,也许你用“嵌套”:“tags.tag”,而不是“嵌套”:“标签”?

(UN)成功的尝试#1:

创建索引与映射:

$ curl -XPOST 'http://localhost:9200/testindex/' -d '{ 
"mappings": { 
    "element": { 
     "properties": { 
      "tags": { 
       "type": "nested", 
       "include_in_parent": true, 
       "properties": { 
        "id": { 
         "type": "integer" 
        }, 
        "tag": { 
         "type": "string" 
        } 
       } 
      } 
     } 
    } 
} 

}”

指数文件:

$ curl -XPOST 'http://localhost:9200/testindex/element' -d '{ 
    "element_id": 4682, 
    "parent_id": null, 
    "wall_id": null, 
    "username": "John Doe", 
    "avatar": "56f1bb0a96d02b90e5915ff38ea189ec.jpg", 
    "title": "Easy Chart is a great easy comparison chart maker for math and...", 
    "description": "<p>Easy Chart is a great easy comparison chart maker for math and science skills. It even allows you to pick the appropriate chart type.</p>", 
    "groups": [ 
     { 
      "id": 6, 
      "name": "Fourth Grade (All Subjects)", 
      "name_short": "4th Grade" 
     }, 
     { 
      "id": 17, 
      "name": "Eighth Grade Science", 
      "name_short": "8th Science" 
     }, 
     { 
      "id": 13, 
      "name": "Seventh Grade Science", 
      "name_short": "7th Science" 
     }, 
     { 
      "id": 9, 
      "name": "Sixth Grade Science", 
      "name_short": "6th Science" 
     } 
    ], 
    "tags": [ 
     { 
      "id": 33, 
      "tag": "iPad" 
     }, 
     { 
      "id": 32, 
      "tag": "iPod" 
     } 
    ], 
    "webpages": [], 
    "videos": [], 
    "documents": [], 
    "photos": [], 
    "reports": [], 
    "bookmarks": [], 
    "likes": [], 
    "dislikes": [], 
    "element_type": "Post", 
    "active": true, 
    "deleted": false, 
    "updated_at": "2011-11-27T21:37:38-0600", 
    "created_at": 1322451458000, 
    "created_at_formatted": "2 weeks ago" 
}' 

搜索:

$ curl -XPOST 'http://localhost:9200/testindex/element/_search' -d '{ 
    "sort":{ "created_at":{ "order":"desc" } }, 
    "query":{ 
    "constant_score":{ 
     "filter":{ 
     "and":[ 
      { "missing":{ "field":"parent_id" } }, 
      { "missing":{ "field":"wall_id" } }, 
      { "term":{ "active":true } } 
     ] 
     } 
    } 
    }, 
    "facets":{ 
    "tags":{ "terms":{ "field":"tags.tag" } } 
    } 
}' 

结果:

{ 
    "took": 4, 
    "timed_out": false, 
    "_shards": { 
     "total": 5, 
     "successful": 5, 
     "failed": 0 
    }, 
    "hits": { 
     "total": 1, 
     "max_score": null, 
     "hits": [ 
      { 
       "_index": "testindex", 
       "_type": "element", 
       "_id": "RZK41LngTKOhMUS6DXRi7w", 
       "_score": null, 
       "_source": { 
        "element_id": 4682, 
        "parent_id": null, 
        "wall_id": null, 
        "username": "John Doe", 
        "avatar": "56f1bb0a96d02b90e5915ff38ea189ec.jpg", 
        "title": "Easy Chart is a great easy comparison chart maker for math and...", 
        "description": "<p>Easy Chart is a great easy comparison chart maker for math and science skills. It even allows you to pick the appropriate chart type.</p>", 
        "groups": [ 
         { 
          "id": 6, 
          "name": "Fourth Grade (All Subjects)", 
          "name_short": "4th Grade" 
         }, 
         { 
          "id": 17, 
          "name": "Eighth Grade Science", 
          "name_short": "8th Science" 
         }, 
         { 
          "id": 13, 
          "name": "Seventh Grade Science", 
          "name_short": "7th Science" 
         }, 
         { 
          "id": 9, 
          "name": "Sixth Grade Science", 
          "name_short": "6th Science" 
         } 
        ], 
        "tags": [ 
         { 
          "id": 33, 
          "tag": "iPad" 
         }, 
         { 
          "id": 32, 
          "tag": "iPod" 
         } 
        ], 
        "webpages": [], 
        "videos": [], 
        "documents": [], 
        "photos": [], 
        "reports": [], 
        "bookmarks": [], 
        "likes": [], 
        "dislikes": [], 
        "element_type": "Post", 
        "active": true, 
        "deleted": false, 
        "updated_at": "2011-11-27T21:37:38-0600", 
        "created_at": 1322451458000, 
        "created_at_formatted": "2 weeks ago" 
       }, 
       "sort": [ 
        1322451458000 
       ] 
      } 
     ] 
    }, 
    "facets": { 
     "tags": { 
      "_type": "terms", 
      "missing": 0, 
      "total": 2, 
      "other": 0, 
      "terms": [ 
       { 
        "term": "ipod", 
        "count": 1 
       }, 
       { 
        "term": "ipad", 
        "count": 1 
       } 
      ] 
     } 
    } 
} 

(UN)成功的尝试#2:

创建索引与映射:

$ curl -XPOST 'http://localhost:9200/testindex2/' -d '{ 
"mappings": { 
    "element": { 
     "properties": { 
      "tags": { 
       "type": "nested", 
       "include_in_parent": false, 
       "properties": { 
        "id": { 
         "type": "integer" 
        }, 
        "tag": { 
         "type": "string" 
        } 
       } 
      } 
     } 
    } 
} 

}”

指数文件:

$ curl -XPOST 'http://localhost:9200/testindex2/element' -d '{ 
    "element_id": 4682, 
    "parent_id": null, 
    "wall_id": null, 
    "username": "John Doe", 
    "avatar": "56f1bb0a96d02b90e5915ff38ea189ec.jpg", 
    "title": "Easy Chart is a great easy comparison chart maker for math and...", 
    "description": "<p>Easy Chart is a great easy comparison chart maker for math and science skills. It even allows you to pick the appropriate chart type.</p>", 
    "groups": [ 
     { 
      "id": 6, 
      "name": "Fourth Grade (All Subjects)", 
      "name_short": "4th Grade" 
     }, 
     { 
      "id": 17, 
      "name": "Eighth Grade Science", 
      "name_short": "8th Science" 
     }, 
     { 
      "id": 13, 
      "name": "Seventh Grade Science", 
      "name_short": "7th Science" 
     }, 
     { 
      "id": 9, 
      "name": "Sixth Grade Science", 
      "name_short": "6th Science" 
     } 
    ], 
    "tags": [ 
     { 
      "id": 33, 
      "tag": "iPad" 
     }, 
     { 
      "id": 32, 
      "tag": "iPod" 
     } 
    ], 
    "webpages": [], 
    "videos": [], 
    "documents": [], 
    "photos": [], 
    "reports": [], 
    "bookmarks": [], 
    "likes": [], 
    "dislikes": [], 
    "element_type": "Post", 
    "active": true, 
    "deleted": false, 
    "updated_at": "2011-11-27T21:37:38-0600", 
    "created_at": 1322451458000, 
    "created_at_formatted": "2 weeks ago" 
}' 

搜索:

$ curl -XPOST 'http://localhost:9200/testindex2/element/_search' -d '{ 
    "sort":{ "created_at":{ "order":"desc" } }, 
    "query":{ 
    "constant_score":{ 
     "filter":{ 
     "and":[ 
      { "missing":{ "field":"parent_id" } }, 
      { "missing":{ "field":"wall_id" } }, 
      { "term":{ "active":true } } 
     ] 
     } 
    } 
    }, 
    "facets":{ 
    "tags":{ "terms":{ "field":"tags.tag" }, "nested":"tags" } 
    } 
}' 

结果:

{ 
    "took": 17, 
    "timed_out": false, 
    "_shards": { 
     "total": 5, 
     "successful": 5, 
     "failed": 0 
    }, 
    "hits": { 
     "total": 1, 
     "max_score": null, 
     "hits": [ 
      { 
       "_index": "testindex2", 
       "_type": "element", 
       "_id": "_F1TTGJETOipo8kVR7ZXkQ", 
       "_score": null, 
       "_source": { 
        "element_id": 4682, 
        "parent_id": null, 
        "wall_id": null, 
        "username": "John Doe", 
        "avatar": "56f1bb0a96d02b90e5915ff38ea189ec.jpg", 
        "title": "Easy Chart is a great easy comparison chart maker for math and...", 
        "description": "<p>Easy Chart is a great easy comparison chart maker for math and science skills. It even allows you to pick the appropriate chart type.</p>", 
        "groups": [ 
         { 
          "id": 6, 
          "name": "Fourth Grade (All Subjects)", 
          "name_short": "4th Grade" 
         }, 
         { 
          "id": 17, 
          "name": "Eighth Grade Science", 
          "name_short": "8th Science" 
         }, 
         { 
          "id": 13, 
          "name": "Seventh Grade Science", 
          "name_short": "7th Science" 
         }, 
         { 
          "id": 9, 
          "name": "Sixth Grade Science", 
          "name_short": "6th Science" 
         } 
        ], 
        "tags": [ 
         { 
          "id": 33, 
          "tag": "iPad" 
         }, 
         { 
          "id": 32, 
          "tag": "iPod" 
         } 
        ], 
        "webpages": [], 
        "videos": [], 
        "documents": [], 
        "photos": [], 
        "reports": [], 
        "bookmarks": [], 
        "likes": [], 
        "dislikes": [], 
        "element_type": "Post", 
        "active": true, 
        "deleted": false, 
        "updated_at": "2011-11-27T21:37:38-0600", 
        "created_at": 1322451458000, 
        "created_at_formatted": "2 weeks ago" 
       }, 
       "sort": [ 
        1322451458000 
       ] 
      } 
     ] 
    }, 
    "facets": { 
     "tags": { 
      "_type": "terms", 
      "missing": 0, 
      "total": 2, 
      "other": 0, 
      "terms": [ 
       { 
        "term": "ipod", 
        "count": 1 
       }, 
       { 
        "term": "ipad", 
        "count": 1 
       } 
      ] 
     } 
    } 
}