我似乎无法在弹性搜索中进行刻面搜索 [英] I can't seem to get faceted search working in elastic search

查看:119
本文介绍了我似乎无法在弹性搜索中进行刻面搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎不了解弹性搜索方面的QueryDSL。以下是我的查询对象,以及我的标签数组的映射。我试图让这些放在一个基于标签的方方面面的导航。每个元素将在标签数组中具有相关的多个标签[不是所有元素都将具有标签。有些将有一个空数组。每个标签都是具有id和tag属性的对象。



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



任何人都可以帮助我正确格式化,了解组织这些?我感谢任何帮助!

  //这是我的查询对象
{
sort:{ create_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}}
}
}


//这是标签数组的映射
标签:{
type:nested,
include_in_parent:true,
properties:{
id:{type },
tag:{type:string}
}
},


解决方案

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



这导致我相信其他映射或某些文档(不是您提供的示例)导致问题的问题。 p>

当您尝试嵌套方法时,也许您使用嵌套:tags.tag而不是嵌套:标签?



(Un)成功尝试#1:



使用映射创建索引:

  $ curl -XPOST'http:// localhost:9200 / testindex /'-d'{
mappings:{
元素:{
properties:{
tags:{
type:nested,
include_in_parent:true,
:{
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是一个很好的数学和...的简单比较图表制作者,
description:< p> Easy Chart is a很好的数学和科学技能的比较图表制作者。
groups:[
{
id:6,
name: 第四年级(所有学科),
name_short:4th Grade
},
{
id:17,
name 八年级科学,
name_short:第8科学
},
{
id:13,
名称:第七年级科学,
name_short:第七科学
},
{
id:9,
名称:六年级科学
name_short:第6科学
}
],
标签:[
{
id:33,
标签:iPad
},
{
id:32,
标签:iPod
}
] ,
webpages:[],
videos:[],
documents:[],
照片:[],
报告:[],
书签:[],
喜欢:[],
不喜欢:[] ,
element_type:Post,
active:true,
deleted:false,
updated_at:2011-11-27T21:37: 38-0600,
created_at:1322451458000,
created_at_formatted:2周前
}'

搜索:

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

结果:

  {
也k:4,
timed_out:false,
_shards:{
total:5,
success:5,
: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是一个很好的数学和...的简单比较图表制作者,
description:< ; p> Easy Chart是一个很好的数学和科学技能的比较图表制作者。
groups:[
{
id:6,
name: 第四年级(所有学科),
name_short:4th Grade
},
{
id:17,
name 八年级科学,
name_short:第8科学
},
{
id:13,
名称:第七年级科学,
name_short:第七科学
},
{
id:9,
名称:六年级科学
name_short:6th Scienc e
}
],
标签:[
{
id:33,
标签:iPad
$,
{
id:32,
标签:iPod
}
],
webpages ],
videos:[],
documents:[],
照片:[],
报告:[],
bookmarks:[],
喜欢:[],
不喜欢:[],
element_type:Post,
active ,
deleted:false,
updated_at:2011-11-27T21:37:38-0600,
created_at:132245145 8000,
created_at_formatted:2周前
},
sort:[
1322451458000
]
}
]
},
facets:{
tags:{
_type:terms,
missing:0,
total:2,
other:0,
terms:[
{
term:ipod,
count 1
},
{
term:ipad,
count:1
}
]
}
}
}

(Un)成功尝试#2: strong>



使用映射创建索引:

  $ curl -XPOST 'http:// localhost:9200 / testindex2 /'-d'{
mappings:{
element:{
prope rties:{
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是一个很好的数学和...的简单比较图表制作者,
描述:< p> Easy Chart是一个很好的数学和科学技能的比较图表制作者。
groups:[
{
id:6,
name: 第四年级(所有学科),
name_short:4th Grade
},
{
id:17,
name 八年级科学,
name_short:第8科学
},
{
id:13,
名称:第七年级科学,
name_short:第七科学
},
{
id:9,
名称:六年级科学
name_short:第6科学
}
],
标签:[
{
id:33,
标签:iPad
},
{
id:32,
标签:iPod
}
] ,
webpages:[],
videos:[],
documents:[],
照片:[],
报告:[],
书签:[],
喜欢:[],
不喜欢:[] ,
element_type:Post,
active:true,
deleted:false,
updated_at:2011-11-27T21:37: 38-0600,
created_at:1322451458000,
created_at_formatted:2周前
}'

搜索:

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

结果:

 take:17,
timed_out:false,
_shards:{
total:5,
: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是一个很好的数学和...的简单比较图表制作工具,
description:< p> Easy Chart是一个很好的数学和科学技能的比较图表制作者。
groups:[
{
id:6,
name: 第四年级(所有学科),
name_short:4th Grade
},
{
id:17,
name 八年级科学,
name_short:第8科学
},
{
id:13,
名称:第七年级科学,
name_short:第七科学
},
{
id:9,
名称:六年级科学
name_short:6th Scienc e
}
],
标签:[
{
id:33,
标签:iPad
$,
{
id:32,
标签:iPod
}
],
webpages ],
videos:[],
documents:[],
照片:[],
报告:[],
bookmarks:[],
喜欢:[],
不喜欢:[],
element_type:Post,
active ,
deleted:false,
updated_at:2011-11-27T21:37:38-0600,
created_at:132245145 8000,
created_at_formatted:2周前
},
sort:[
1322451458000
]
}
]
},
facets:{
tags:{
_type:terms,
missing:0,
total:2,
other:0,
terms:[
{
term:ipod,
count 1
},
{
term:ipad,
count:1
}
]
}
}
}


I can't seem to understand the QueryDSL for facets in elastic search. Below are my query object, and the mapping for my tags array. I'm trying to get these to put in a faceted navigation based on tags. Each "element" will have multiple tags associated in the tags array [Not all elements will have tags. Some will have an empty array.]. Each tag is an object with id and tag properties.

I've tried the nested facet approach and get the error that "tags is not nested", so then I try this below. I don't receive an error, but there is no facets object in the return JSON. I've been using this page for help: http://www.elasticsearch.org/guide/reference/api/search/facets/index.html.

Can anyone help me to format this correctly and understand the organization of these? I appreciate any help!

// 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" }
  }
},

解决方案

I've tried hard to replicate this (using 0.18.5) but without any luck. Some details below. The first example tries to replicate what you describe. The second doesn't map tags as include_in_parent and uses the "nested" field in the facet request. As you can see, in both cases the facet is returned.

This leads me to believe that there is either something wrong with the rest of the mappings or some document (not the sample one you provided) that is causing issues.

When you tried the nested approach, perhaps you used "nested":"tags.tag" instead of "nested":"tags"?

(Un)Successful attempt #1:

Create index with mappings:

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

}'

Index document:

    $ 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"
}'

Search:

$ 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" } }
  }
}'

Result:

{
    "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)Successful attempt #2:

Create index with mappings:

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

}'

Index document:

    $ 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"
}'

Search:

$ 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" }
  }
}'

Result:

{
    "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
                }
            ]
        }
    }
}

这篇关于我似乎无法在弹性搜索中进行刻面搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆