Python ES查詢


 

數據檢索功能

# rs = es.search(index="index", doc_type="type", filter_path=["hits.hits._*"])
rs = es.search(index='shang', filter_path=['hits.hits._*'], size=30, from_=3)
print(json.dumps(rs, indent=2, ensure_ascii=False))

常用參數

  • index - 索引名
  • q - 查詢指定匹配 使用Lucene查詢語法
  • from_ - 查詢起始點  默認0
  • doc_type - 文檔類型
  • size - 指定查詢條數 默認10
  • field - 指定字段 逗號分隔
  • sort - 排序  字段:asc/desc
  • body - 使用Query DSL
  • scroll - 滾動查詢

 

1、range 范圍查詢

  • gt: > 大於
  • lt: < 小於
  • gte: >= 大於或等於
  • lte: <= 小於或等於
  • include_lower: 是否包含范圍的左邊界,默認是true
  • include_upper: 是否包含范圍的右邊界,默認是true
# 查詢年齡1歲到17歲
query = {
    'query': {
        'range': {
            'age': {
                "from": "1",
                "to": "17",
                "include_lower": 'true',    # 意思是包括1
                "include_upper": 'false'    # 不包括17
            }
        }
    }
}

# 查詢年齡1歲到17歲

query = {
    'query': {
        'range': {
            'age': {
                'lt': 17,
                'gte': 1
            }
        }
    }
}

# 匹配的所有文檔
allDoc = es.search(index='index', doc_type='type', body=query)
print(json.dumps(allDoc['hits']['hits'], indent=2, ensure_ascii=False))
View Code

2、match 查詢

  • match query:知道分詞器的存在, 會對field進行分詞操作,然后再查詢
  • match_all:查詢所有文檔
  • multi_match:可以指定多個字段
  • match_phrase:短語匹配查詢

 

# match查詢
# 匹配name包含"python"關鍵字的數據
query = {
    'query': {
        'match': {
            "name": "python",
        }
    }
}


# match_all 查找所有文檔
query = {
    'query': {
        'match_all': {}
    }
}


# 切片查詢
query = {
    'query': {
        'match_all': {
              'age': 18            
    }
    },
    "from": 2,    # 從第二條數據開始
    "size": 4    # 獲取4條數據
}    


# multi_match 多字段搜索
# 查詢name和addr包含"深圳"關鍵字的數據
query = {
    'query': {
        "multi_match": {
            "query": "深圳",
            "fields": ["name", "addr"]
        }
    }
}

# 查詢name中包含’深圳‘的
query = {
    'query': {
        "match_phrase": {
            "name": "深圳",
        }
    }
}    


# match_phrase和match的區別是這里是真正包含'深圳',而不是只要滿足其中一個字就會被模糊命中
View Code

 

3、term 查詢

  • term query:不知道分詞器的存在, 會確切尋找
  • term:查詢滿足條件的文檔(相等關系而不是包含關系)
  • terms:查詢多個滿足條件的文檔
# term 匹配一個    查詢 xx = “xx”
# 查詢name="python"的所有數據
query1 = {
    'query': {
        'term': {
            "name": "python"
        }
    }
}


# terms 多個條件   # terms: 查詢 xx = “xx” 或 xx = “yy”
# 查詢出name="c"或name="ios"的所有數據
query2 = {
    'query': {
        'terms': {
            "name": [
                'c',
                'ios'
            ]
        }
    }
}


allDoc = es.search(index='index', doc_type='type', body=query)
print(json.dumps(allDoc['hits']['hits'], indent=2, ensure_ascii=False))
View Code

 4、bool 查詢

  • must  and
  • must_not  not
  • should  or  
query = {
    'query': {
        "bool": {
            "must": [
                {
                    "term": {
                        "name": "python"
                    }
                },
                {
                    "term": {
                        "age": 18
                    }
                }
            ]
        }
    },
    "sort": {
        "time": {
            "order": "desc"
        }
    }
}

# 獲取name="python"並且age=18的所有數據
# allDoc = es.search(index='index', doc_type='type', body=query)
# print(json.dumps(allDoc['hits']['hits'], indent=2, ensure_ascii=False))
View Code

 5、prefix 前綴匹配查詢

{
	"query":{
		"prefix":{
		"title":{
		"value":"r"      # 匹配title 字段 r開頭的
		}
	}
}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM