ES增删改查

了解了一下python对es 7.5的操作,记录下,不难:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
from settings import Config
mapping = {
    'properties': {
        'topic': {
            'type': 'text',
            'analyzer': 'ik_max_word',
            'search_analyzer': 'ik_max_word'
        },
        'content': {
            'type': 'text',
            'analyzer': 'ik_max_word',
            'search_analyzer': 'ik_max_word'
        },
        'like': {
            'type': 'integer'
        },
        'read': {
            'type': 'integer'
        }
    }
}

data = {
   'user':'xxx',
   'topic':'44444',
   'content':'xxxxxxxxxxxddddd',
   'tm':'2019-1-20 22:00:00',
   'like':'1',
   'read':'2'
}

#create index,create mapping
Config.es.indices.delete(index='question', ignore=[400, 404])
Config.es.indices.create(index='question', ignore=400)
result = Config.es.indices.put_mapping(index='question', body=mapping)
print(result)

#insert data
result = Config.es.create(index='question', id=4, body=data)

#update document
result = Config.es.index(index='question',body=data, id=4)
print(result)
#以下为查找操作
#search index all content
result = Config.es.search(index='question')
print(result)

#query dsl,topic包含单词ddddd或lllll的所有内容
dsl = {
    'query': {
        'match': {
            "topic": "ddddd lllll"
        }
    }
}

result = Config.es.search(index='question', body=dsl)
print(result)

#kibana create index(mapping),post data
PUT kb_question
{
 "mappings":{
    "kb_question":{
      "properties": {
          "content": {
              "type": "text",
              "analyzer": "ik_max_word",
              "search_analyzer": "ik_max_word"
          },
          "topic": {
              "type": "text",
              "analyzer": "ik_max_word",
              "search_analyzer": "ik_max_word"
          },
          "like": {
              "type": "integer"
          },
          "read": {
              "type": "integer"
          },
          "user_id": {
              "type": "integer"
          },
          "user": {
              "type": "text"
          },
          "tm":{
              "type":"date",
              "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
          }
      }
      
    }
}
}


POST kb_question/kb_question/
{
   "user": "xiqing",
   "user_id": "704981",
   "topic": "test test",
   "content": "ccccccccccccccc la la lala la la la la la!!",
   "read":"0",
   "like":"0",
   "tm":"2019-1-20 15:45:00"
}


GET _cat/indices/kb_question
#DELETE /kb_question


PUT kb_answer
{ "mappings":{
    "kb_answer":{
		"properties": {
		   "user": {
				"type": "text"
		   },
		   "q_id": {
				"type": "integer"
			},
		   "answer": {
				"type": "text",
				"analyzer": "ik_max_word",
				"search_analyzer": "ik_max_word"
			},
			"like": {
				"type": "integer"
			},
			"tm":{
				"type":"date",
				"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
			}
		}
   }
}
}

GET _cat/indices/kb_answer

POST  kb_answer/kb_answer/
{
   "user": "xiqing",
   "q_id": "888",
   "answer": "answer2 answer2 answer2 answer2 answer2!!",
   "like":0,
   "tm":"2019-1-21 18:45:00"
}
posted @ 2020-01-20 10:48  风云_就是她了  阅读(437)  评论(0编辑  收藏  举报