2011-12-14 2 views
2

Я, похоже, не понимаю QueryDSL для граней в упругом поиске. Ниже мой объект запроса и отображение для моего массива tags. Я пытаюсь заставить их использовать гранвую навигацию на основе тегов. Каждый «элемент» будет иметь несколько тегов, связанных в массиве тегов. [Не все элементы будут иметь теги. У некоторых будет пустой массив.]. Каждый тег - это объект с свойствами id и tag.Я не могу получить граненый поиск, работающий в упругом поиске

Я пробовал подход вложенного фасета и получил ошибку, что «теги не вложены», поэтому я попробую это ниже. Я не получаю сообщение об ошибке, но в возврате JSON нет объекта фасетов. Я использовал эту страницу для справки: 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. Действительно ли нет объекта фасетки в возвращаемом объекте? –

+0

Нет, а я 0.18.5.Вот структура элемента с тегами: http://pastebin.com/1ruBXKJ9 – swatkins

ответ

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 
       } 
      ] 
     } 
    } 
} 
+0

Джоэл, я ценю вашу помощь по этому поводу. Прослеживая ваши шаги и даже запуская ваши запросы в моем исходном индексе. Я обнаружил, что у меня есть коррумпированный индекс. Я восстановил индекс и выполнил запросы и получил объект facets в моем результате. Большое спасибо за помощь! – swatkins

Смежные вопросы