Basic usage of python operation elasticsearch

Create an es index library, which is equivalent to creating a table

     from elasticsearch import Elasticsearch
     from elasticsearch.helpers import bulk
     es = Elasticsearch(hosts='localhost:9200')
     #创建索引表
     es.indices.create(index='my', ignore=400)

Insert a piece of data into the es index library

  data = {
    
    "name": "小明", "age": "8", "gender": "男"}
  res = es.index(index='my', doc_type='doc', body=data)

Batch insert data into the index library

    from elasticsearch.helpers import bulk
    data_lis = [{
    
    "name": "小红", "age": "8", "gender": "女"}, {
    
    "name": "小王", "age": "8",    "gender": "男"}]
    # 批量插入es创建的索引表
    res2 = bulk(es, data_lis, index='my' )

Query all data

    data = es.search(index='my')
    #查询所有的第二种方法
    body = {
    
    
            'query':{
    
    
            'match_all':{
    
    }
             }
        }
    data = es.search(index='my', body=body)

term: query data based on the value of a field

    # es 官方文档: term 和 terms 是 包含(contains) 操作,而非 等值(equals) (判断)
    body2 =  {
    
    
                 "query":{
    
    
                   "term":{
    
    
                      "age":8 }
                          }
                        }
    data_list = es.search(index='my', body=body2)

terms: query data based on multiple values ​​of a field

    body3 = {
    
    
               "query":{
    
    
                "terms":{
    
    
                 "age:[ 8,10 ] }}}
    age_list = es.search(index='my', body=body3)

match: query based on a field containing a certain character

 body4 =  {
    
    
           "query":{
    
    
               "match":{
    
    
                  "name":"红" } 
                  }
           }
    red_list = es.search(index='my', body=body4)

multi_match: query based on multiple fields containing a certain character

    body = {
    
    
            "query":{
    
    
                  "multi_match":{
    
    
                      "query":"红",
                       "fields":["name","addr"]
                       }
                          }
                  }
     #查询name和addr中包含红的数据
     es.search(index="my", body=body)

match_phrase: matching tiltle contains quick fox, the two fields must be adjacent

    body = {
    
    
    "query": {
    
    
        "match_phrase": {
    
    
            "title": {
    
    
            	"query": "quick fox",
            	"slop":  1
            }
        }
    }
}
     #查询name和addr中包含红的数据
     es.search(index="my", body=body)

Query based on id

    body = {
    
    
			"query": {
    
    
				"ids": {
    
    
					"type": "doc",
					"values": ["vxGrGGwBv1w0KXkAjsEd""wBG_GGwBv1w0KXkAcsEn"
					]
				}
			} }
    h = es.search(index="my",  body=body)

Compound query

# bool有3类查询关系,must(都满足), should(其中一个满足), must_not(都不满足)
{
    
    
    "query": {
    
    
        "bool": {
    
    
            "must": [{
    
    
                "term": {
    
    
                    "name": "小红"
                }
            }, {
    
    
                "term": {
    
    
                    "age": 18
                }
            }]
        }
    }
}
es.search(index='my', body=body)

range: range query

body = {
    
    
    "query": {
    
    
        "range": {
    
    
            "age": {
    
    
                "gte": 18,
                "lte": 30
            }
        }
    }
}

# 查询18<=age<=30的所有数
es.search(index="my_index", doc_type="test_type", body=body)

Slice query

body = {
    
    
    "query": {
    
    
        "match_all": {
    
    }
    },
    "from": 2,
    "size": 4
}
# 从第2条数据开始,获取4条数据
es.search(index="my", body=body)

Wildcard query

body = {
    
    
    "query":
        {
    
    
        "wildcard":
            {
    
    
            "name": "*红"
        }
    }
}
# 查询name字段以红结尾的数据
es.search(index="my",body=body)   

Prefix query

body = {
    
    
    "query": {
    
    
        "prefix": {
    
    
            "name": "李"
        }
    }
}
# 查询前缀为"李"的所有数据
es.search(index='my', body=body)

exists: query whether a field is not null data

# 查询某个字段不是空值:
body = {
    
    
  "query": {
    
    
    "bool": {
    
    
      "must": {
    
    
        "exists": {
    
    
          "field": "name"
        }
      }
    }
  }
}
es.search(index='my', body=body)

Set the weight of the query field

# ^ 字符语法为单个字段提升权重,在字段名称的末尾添加 ^boost ,其中 boost 是一个浮点数:
body = {
    
    
    "query": {
    
    
        "multi_match": {
    
    
            "query": "明 红",
            "field": ["name^2", 'last_name^1.5', "first_name^0.9"]
        }
    }
}
es.search(index='my', body=body)

Sort

body = {
    
    
    "query": {
    
    
        "match_all": {
    
    }
    },
    "sort": {
    
    
        "age": {
    
                     # 根据age字段升序排序
            "order": "asc"       # asc升序,desc降序
        }
    }
}

# 多字段排序,注意顺序!写在前面的优先排序
body = {
    
    
    "query": {
    
    
        "match_all": {
    
    }
    },
    "sort": [{
    
    
        "age": {
    
                    # 先根据age字段升序排序
            "order": "asc"      # asc升序,desc降序
        }
    }, {
    
    
        "name": {
    
                   # 后根据name字段升序排序
            "order": "asc"      # asc升序,desc降序
        }
    }],
}
es.search(index='my', body=body)

count execute the query and get the number of query matches

body = {
    
    
    "query": {
    
    
        "prefix": {
    
    
            "name": "李"
        }
    }
}
# 查询前缀为"李"的所有数据
es.count(index='my', body=body)

aggs: aggregate query to get the oldest age

body = {
    
    
    "query": {
    
    
        "match_all": {
    
    }
    },
    "aggs": {
    
    
        "max_age": {
    
    
            "max": {
    
    
                "field": "age"
            }
            
        }
    }
}
es.search(index='my', body=body)

aggs: aggregate query to get the smallest age

body = {
    
    
    "query": {
    
    
        "match_all": {
    
    }
    },
    "aggs": {
    
    
        "min_age": {
    
    
            "min": {
    
    
                "field": "age"
            }

        }
    }
}
es.search(index='my', body=body)

aggs: Aggregate query to get the age of the sum

body = {
    
    
    "query": {
    
    
        "match_all": {
    
    }
    },
    "aggs": {
    
    
        "sum_age": {
    
    
            "sum": {
    
    
                "field": "age"
            }

        }
    }
}
es.search(index='my', body=body)

aggs: aggregate query to get the average age

body = {
    
    
    "query": {
    
    
        "match_all": {
    
    }
    },
    "aggs": {
    
    
        "avg_age": {
    
    
            "avg": {
    
    
                "field": "age"
            }

        }
    }
}
es.search(index='my', body=body)

Guess you like

Origin blog.csdn.net/xxy_yang/article/details/96995547