【问题标题】:searching for subdocument in elastic search在弹性搜索中搜索子文档
【发布时间】:2015-06-01 15:00:26
【问题描述】:

我有一份表格文件

'search':
[
    {
        id:'1',
        "content": [
            {
                "text": "random text .....",
                "time": 150.023
            },
            {
                "text": "random text .....",
                "time": 160.023
            }
          ]
    },

    {
        id:'2',
        "content": [
            {
                "text": "random text .....",
                "time": 150.023
            },
            {
                "text": "random text .....",
                "time": 160.023
            }
          ]
    }
    .
    .
    .
]

我想搜索文本字段并获取其 id、文本和时间,例如

我的示例文档和索引搜索如下所示

'search':
[
    {
        id:'1',
        "content": [
            {
                "text": "Algorithm and Data structure",
                "time": 150.023
            },
            {
                "text": "Selection Sort",
                "time": 160.023
            }
          ]
    },

    {
        id:'2',
        "content": [
            {
                "text": "Database and schema",
                "time": 1530.023
            }
          ]
    }
    .
    .
    .
]

现在当我搜索“text:Algorithm”时,我需要 id:'1'、“text”:“算法和数据结构”和“时间”:150.023。

如何使用 elasticsearch 获得上述结果。请提供一些解决方案。提前谢谢你。

【问题讨论】:

    标签: python search indexing elasticsearch


    【解决方案1】:

    在 elasticsearch 中,搜索是按文档进行的。您可以在搜索中单独获取某些字段,但不能以您在数组中查找的方式获取。 这里最好的解决方案是将搜索字段声明为nested,这样您就可以进行元素特定搜索,然后获取整个文档,然后在客户端检索您要查找的元素。

    或者,您可以更改数据建模并围绕搜索元素对文档进行建模。

    这意味着不是将单个文档维护为 -

    'search':
    [
        {
            id:'1',
            "content": [
                {
                    "text": "random text .....",
                    "time": 150.023
                },
                {
                    "text": "random text .....",
                    "time": 160.023
                }
              ]
        },
    
        {
            id:'2',
            "content": [
                {
                    "text": "random text .....",
                    "time": 150.023
                },
                {
                    "text": "random text .....",
                    "time": 160.023
                }
              ]
        }
        .
        .
        .
    ]
    

    您可以为每个上述文档维护模型多个文档,其中单个文档看起来像 -

    {
      "id": "1",
      "content": {
        "text": "random text .....",
        "time": 150.023
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-08-12
      • 2015-12-18
      • 1970-01-01
      • 1970-01-01
      • 2016-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多