当前位置: 首页 > 面试题库 >

我似乎无法在elasticsearch中使用多面搜索

耿玄裳
2023-03-14
问题内容

我似乎无法理解QueryDSL的elasticsearch方面。以下是我的查询对象以及我的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" }
  }
},

问题答案:

我已经尽力复制(使用0.18.5),但是没有任何运气。下面是一些细节。第一个示例尝试复制您描述的内容。第二个不将标签映射为include_in_parent,而是在构面请求中使用“嵌套”字段。如您所见,在两种情况下,都将返回构面。

这使我相信,其余的映射或者某些文档(不是您提供的示例文件)出了问题是导致问题的原因。

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

(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
                }
            ]
        }
    }
}


 类似资料:
  • 问题内容: 我在Spark上使用Python时遇到问题。我的应用程序具有一些依赖项,例如numpy,pandas,astropy等。我无法使用virtualenv创建具有所有依赖项的环境,因为群集上的节点除HDFS外没有任何公共的挂载点或文件系统。因此,我坚持使用。我将站点程序包的内容打包到一个ZIP文件中,然后提交与option一样的作业(如在Spark executor节点上安装Python依

  • 所以我不知道如何让桌子在雪花中旋转。我试图在这里为帮助页面建模https://docs.snowflake.com/en/sql-reference/constructs/pivot.html。 枢轴输出 sql文本:

  • 目前我只是第一次使用SQL,我一直在尝试向现有表中添加新的列,以实现学生注册之类的交易。 我在网上关注了一些教程,它们都指向了我到目前为止一直在尝试的内容,基本上是这样的: 根据我阅读的内容和观看的教程,它应该可以工作,但phpmyadmin会显示一条错误消息: 分析过程中发现1个错误。 无法识别的alter操作。(位置0的“”附近) 1064年的今天,您的SQL语法出现错误;查看与您的Maria

  • 我有一个非常简单的超文本标记语言文件,其中有一个按钮。当单击此按钮时,函数loadDoc()在javascript文件(ajax.js)中运行。带有IDajax_text的div被更改为“单击”。到目前为止还不错。 现在,我试图对一个php文档进行ajax调用。php文档应该响应“Hello World!”。我试图通过使用警报(alert(msg))来显示此消息。php文档与我的HTML文档位于同

  • 我正在开发新的web,我想使用ArcGIS javascript API4使用WMTS服务 选择空间参考EPSG:25830时,Javascript API 4.11中的WMTSLayer似乎不起作用。 问题是API生成的请求不正确,Tilerow参数错误。 正在发送的请求是这样的。http://www.ign.es/wmts/ign-base?SERVICE=WMTS 而且一定是... http

  • 我有一个有效的javascript测验。目的是保存用户的分数。 这就是我所拥有的。在测验(quiz.html)结束时,弹出一个警报,并显示用户的分数(这工作很好)。关闭警告框后,将出现一个新窗口complete.html(这也起作用)。 在我的complete.html中,我有以下代码。 但是,我尝试使用,但是分数没有出现在SPAN#quiztotalscore中。我错过什么了吗?我是否需要先将分