如何索引Elasticsearch中的对象列表?

2024-10-02 12:27:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我吸收到ElasticSearch中的文档格式如下所示:

{
   'id':'514d4e9f-09e7-4f13-b6c9-a0aa9b4f37a0'
   'created':'2019-09-06 06:09:33.044433',
   'meta':{
      'userTags':[
         {
            'intensity':'1',
            'sentiment':'0.84',
            'keyword':'train'
         },
         {
            'intensity':'1',
            'sentiment':'-0.76',
            'keyword':'amtrak'
         }
      ]
   }
}

…被Python吞食:

r = requests.put(itemUrl, auth = authObj, json = document, headers = headers)

这里的想法是ElasticSearch将keywordintensitysentiment作为以后可以查询的字段。然而,在ElasticSearch方面,我可以观察到这种情况并没有发生(我使用Kibana作为搜索UI)——相反,我看到的是字段“meta.userTags“值是整个对象列表。你知道吗

如何在列表中创建ElasticSearch索引元素?你知道吗


Tags: 文档id列表格式trainelasticsearchkeywordmeta
2条回答

我使用您提供的文档体创建了一个新索引“testind”,并使用Postman REST客户端键入“testTyp”:

POST http://localhost:9200/testind/testTyp
{
   "id":"514d4e9f-09e7-4f13-b6c9-a0aa9b4f37a0",
   "created":"2019-09-06 06:09:33.044433",
   "meta":{
      "userTags":[
         {
            "intensity":"1",
            "sentiment":"0.84",
            "keyword":"train"
         },
         {
            "intensity":"1",
            "sentiment":"-0.76",
            "keyword":"amtrak"
         }
      ]
   }
}

当我查询索引的映射时,得到的是:

GET http://localhost:9200/testind/testTyp/_mapping
{  
  "testind":{  
    "mappings":{  
      "testTyp":{  
        "properties":{  
          "created":{  
            "type":"text",
            "fields":{  
             "keyword":{  
                "type":"keyword",
                "ignore_above":256
              }
            }
          },
          "id":{  
            "type":"text",
            "fields":{  
              "keyword":{  
                "type":"keyword",
                "ignore_above":256
              }
            }
          },
          "meta":{  
            "properties":{  
              "userTags":{  
                "properties":{  
                  "intensity":{  
                    "type":"text",
                    "fields":{  
                      "keyword":{  
                        "type":"keyword",
                        "ignore_above":256
                      }
                    }
                  },
                  "keyword":{  
                    "type":"text",
                    "fields":{  
                      "keyword":{  
                        "type":"keyword",
                        "ignore_above":256
                      }
                    }
                  },
                  "sentiment":{  
                    "type":"text",
                    "fields":{  
                      "keyword":{  
                        "type":"keyword",
                        "ignore_above":256
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

正如您在映射中看到的,这些字段是映射的一部分,将来可以根据需要进行查询,因此,只要字段名不是这些中的一个-https://www.elastic.co/guide/en/elasticsearch/reference/6.4/sql-syntax-reserved.html(您可能希望避免使用术语“keyword”,因为在以后编写搜索查询时可能会混淆,因为字段名和类型都是相同的-“keyword”)。另外,请注意,映射是通过Elasticsearch中的动态映射(https://www.elastic.co/guide/en/elasticsearch/reference/6.3/dynamic-field-mapping.html#dynamic-field-mapping)创建的,因此数据类型由Elasticsearch根据您拥有的值来确定提供。但是,这可能并不总是准确的,因此为了防止出现这种情况,可以使用PUT\u mapping API为索引定义自己的映射,然后防止出现新字段在类型中添加到映射。你知道吗

索引列表不需要特殊的映射—每个字段都可以包含一个或多个相同类型的值。见array datatype。你知道吗

对于对象列表,它们可以作为objectnested数据类型进行索引。默认弹性使用object数据类型。在这种情况下,您可以查询meta.userTags.keyword或/和meta.userTags.sentiment。结果将始终包含具有独立匹配值的完整文档,即搜索keyword=trainsentiment=-0.76您将找到具有id=514d4e9f-09e7-4f13-b6c9-a0aa9b4f37a0的文档。你知道吗

如果这不是您想要的,您需要为字段userTags定义nested数据类型映射并使用nested query。你知道吗

相关问题 更多 >

    热门问题