python-3.x - 添加未在ElasticSearch索引中显示的新文档

标签 python-3.x elasticsearch

我是ElasticsSearch的新手,今天很乱。我的localhost上运行了一个节点,正在创建/更新cat索引。当我在documents索引中添加更多cat时,我注意到当我执行GET请求以查看documents中的所有Postman时,并未添加我制作的新cats。添加第十个cat后,我开始注意到该问题。所有代码都在下面。
ElasticSearch Version: 6.4.0 Python Version: 3.7.4

  my_cat_mapping = {
      "mappings": {
        "_doc": { 
          "properties": { 
            "breed":    { "type": "text"  }, 
            "info" : {
                "cat" :  {"type" : "text"},
                "name" :      {"type" : "text"},
                "age" : {"type" : "integer"},
                "amount"  : {"type" : "integer"}
            },  
            "created"  :  {
              "type":   "date", 
              "format": "strict_date_optional_time||epoch_millis"
            }
          }
        }
      }
    }



cat_body = {
    "breed" : "Persian Cat",
        "info":{
            "cat":"Black Cat",
            "name": " willy",
            "age": 5,
            "amount": 1
        }

}



def document_add(index_name, doc_type, body, doc_id = None):
    """Funtion to add a document by providing index_name,
    document type, document contents as doc and document id."""
    resp = es.index(index=index_name, doc_type=doc_type, body=body, id=doc_id)
        print(resp)


document_add("cat", "cat_v1", cat_body, 100 )

最佳答案

由于文档ID传递为100,因此它仅更新相同的cat文档。我假设它每次运行都不会改变!

您每次都必须更改文档ID doc_id,以添加新的cat而不是更新现有的cat。

...

cat_id = 100
cat_body = {
    "breed" : "Persian Cat",
        "info":{
            "cat":"Black Cat",
            "name": " willy",
            "age": 5,
            "amount": 1
        }
}

...

document_add("cat", "cat_v1", cat_body, cat_id )

这样,您可以同时更改cat_id和cat_body来获得新猫。

关于python-3.x - 添加未在ElasticSearch索引中显示的新文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57913301/

相关文章:

grails - 在grails中使用elasticsearch查询嵌套字段

java - Elasticsearch 2.2.0 ESIntegTestCase : ensureYellow() results in “timed out waiting for yellow”

python - 在 Python 3 中从服务器返回回复时引发 BadStatusLine 异常

python - VSCode PyLint 未检测到我的 Python DTO 类成员

python - 来自 2 个系列的笛卡尔积

Elasticsearch:查找带空格和不带空格的值

python - 为什么我的字体在 tkinter 中无法正确显示?

python - 使用 python requests-HTML 获取标签的父元素

elasticsearch - Elastic Search中如何限制然后排序

elasticsearch - Elasticsearch范围查询不适用于匹配查询