Elasticsearch:按内部匹配排序

时间:2019-08-02 04:42:40

标签: elasticsearch

我有一个这样的有效负载,所有文档中都会有相同的有效负载,但是每个文档中每个标签的权重都不同。

{
  "tags": [
    {
      "tag": "tag1",
      "weight": 5
    },
    {
      "tag": "tag2",
      "weight": 10
    },
    {
      "tag": "tag3",
      "weight": 7
    }
  ]
}

我想在数组的元素而不是整个数组中进行匹配,所以在映射中使用了嵌套字段。

{
  "mappings": {
    "doc": {
      "properties": {
        "tags": {
          "type": "nested",
          "properties": {
            "tag": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "weight": {
              "type": "double"
            }
          }
        }
      }
    }
  }
}

我的要求是搜索标签并根据文档的重量对特定标签进行排序。 我已经在下面的查询中进行了尝试,但是没有按照预期进行排序。

{
  "from": 0,
  "size": 50,
  "sort": [
    {
      "tags.weight": {
        "order": "asc",
        "nested_filter": {
          "term": {
            "tags.tag": "tag1"
          }
        }
      }
    }
  ]
}

编辑添加了示例

Exa: 可以说有3个文档

{     
      "uid":1,
      "tags": [
        {
          "tag": "tag1",
          "weight": 5
        },
        {
          "tag": "tag2",
          "weight": 10
        },
        {
          "tag": "tag3",
          "weight": 7
        }
      ]
    },
{     
      "uid":2,
      "tags": [
        {
          "tag": "tag1",
          "weight": 9
        },
        {
          "tag": "tag2",
          "weight": 10
        },
        {
          "tag": "tag3",
          "weight": 7
        }
      ]
    },
{     
      "uid":3,
      "tags": [
        {
          "tag": "tag1",
          "weight": 7
        },
        {
          "tag": "tag2",
          "weight": 10
        },
        {
          "tag": "tag3",
          "weight": 7
        }
      ]
    }

当搜索“ tag1”并按升序排序时,我应该得到以下输出 也就是说,应根据“ tag1”的权重对文件进行排序。

{
  "uid": 1,
  "tags": [
    {
      "tag": "tag1",
      "weight": 5
    },
    {
      "tag": "tag2",
      "weight": 10
    },
    {
      "tag": "tag3",
      "weight": 7
    }
  ]
},
{
  "uid": 3,
  "tags": [
    {
      "tag": "tag1",
      "weight": 7
    },
    {
      "tag": "tag2",
      "weight": 10
    },
    {
      "tag": "tag3",
      "weight": 7
    }
  ]
},
{
  "uid": 2,
  "tags": [
    {
      "tag": "tag1",
      "weight": 9
    },
    {
      "tag": "tag2",
      "weight": 10
    },
    {
      "tag": "tag3",
      "weight": 7
    }
  ]
}

提前谢谢!

3 个答案:

答案 0 :(得分:1)

根据inner_hits,可以设置sort

示例:

"inner_hits":{
  "sort": [
    {
      "tags.tag": {
        "order": "asc"
      }
    }
  ]
}

然后,在result内的hits上,您将拥有inner_hits对已排序对象的所有权。

答案 1 :(得分:0)

您缺少with SSHTunnelForwarder( ('jump_server_host', jump_ssh_port), # jump server ssh_username='username', ssh_pkey="/path/to/new/id_rsa", remote_bind_address=('remote_host', remote_ssh_port), # remote_server local_bind_address=('0.0.0.0', 30023) # local_bind_port set to 30023, your can select new one ) as server: server.start() print(server.local_bind_port) server.close() / 30023参数。这就是您需要的:

path

对于旧版本的Elastic:

nested_path

答案 2 :(得分:0)

Nishanat的答案是正确的。在词条查询中,您可以使用'tags.tag.keyword'代替'tags.tag'