| [ Web Proxy ] |
| Viewing: https://raw.githubusercontent.com/We2one/Notes/master/python-basic-notes/python-days/Day_47.md | [Back] [Original] |
[Postman.png]
3.
1. : ,,
2. :
3. : ,
##### ES
1.
1. Es
| | database | table | rows | Columns |
| ------------- | -------- | ----- | -------- | ------- |
| Elasticsearch | index | type | document | fields |
2. (), ()
3. Es
2. (index)
1. , put
1.
[postman_index01.png]
[postman_index02.png]
2. ,,
| | |
| ---------- | ----------------- |
| carlist | , |
| mappings | , |
| car | , |
| properties | |
| c_name | , |
| type | |
| text | |
3. Es
| | |
| ------- | ------------------------------------------------------------ |
| string | |
| long | 64 , |
| integer | 32 , |
| short | 16 , |
| byte | 8 , |
| double | 64 , |
| float | 32 , |
| date | , |
| Boolean | |
| Binary | |
| Array | |
| Object | json () |
| nested | json |
| text | ,, Analyzer
[postman_getindex01.png]
+ `yellow open hello rs7mwZXPQL6d-bKUy_DKng 5 1 0 0 1.2kb 1.2kb`
+ yellow : , ,,
+ open : , close
+ index_name :
3. : `ip:9200//`
+
[postman_getindex02.png]
4. : `ip:9200//_search`
+
[postman_getindex03.png]
5. : DELETE
+
[postman_delete.png]
3. (document post ) : `ip//`
+
[postman_post01.png]
4. (document get ) : `ip///_search`
+
[postman_post02.png]
#### ES
##### ES
[postman_esmodel.png]
##### Python ES
1. elasticsearch : `pip install elasticsearch -i http://pypi.tuna.tsinghua.edu.cn/simple`
2.
```python
import random
from elasticsearch import Elasticsearch
es = Elasticsearch("10.10.123.131", timeout=360) # ,
# 10000
c_names = """
""".split("\n")
c_citys = "\
\
\
".split("")
for i in range(10000):
data = {
"c_name": random.choice(c_names),
"c_date": "%s-%s-%s" % (
random.randint(1983, 2020),
random.randint(1, 12),
random.randint(1, 28)
),
"c_mileage": random.randint(1, 10000),
"c_city": random.choice(c_citys),
"c_price": random.randint(8, 100),
"c_sale": random.randint(1, 3),
"c_service": random.randint(30, 100)
} #
res = es.index(index="carlist", doc_type="car", body=data) #
print(res)
print('....')
```
#### ES
##### (Python postman GET )
1.
```json
{
"query": {
"": {
"": ""
}
}
}
```
2. **term**
```python
from elasticsearch import Elasticsearch
es = Elasticsearch(hosts="10.10.123.131", timeout=360)
body = {
#
"query": {
"term": {
"c_name": ""
}
}
}
result = es.search(index="carlist", doc_type="car", body=body)
print(result)
print(result["hits"]["hits"]) #
```
3. **terms**
```python
from elasticsearch import Elasticsearch
es = Elasticsearch(hosts="10.10.123.131", timeout=360)
body = {
#
"query": {
"terms": {
"c_name": ["", ""]
}
}
}
result = es.search(index="carlist", doc_type="car", body=body)
print(result)
print(result["hits"]["hits"]) #
```
4. **match_all**
```python
from elasticsearch import Elasticsearch
es = Elasticsearch(hosts="10.10.123.131", timeout=360)
body = {
#
"query": {
"match_all":{
}
}
}
result = es.search(index="carlist", doc_type="car", body=body)
print(result)
print(len(result["hits"]["hits"])) # 10 10
```
5. : **from**: , **size**: ( from )
```python
from elasticsearch import Elasticsearch
es = Elasticsearch(hosts="10.10.123.131", timeout=360)
body = {
#
"query": {
"match_all":{
}
},
"from": 0,
"size": 100
}
result = es.search(index="carlist", doc_type="car", body=body)
# print(result)
print(len(result["hits"]["hits"])) # 100
print(result["hits"]["hits"])
```
6. **sort**
1. `"sort: [ '' ]"`
```python
from elasticsearch import Elasticsearch
es = Elasticsearch(hosts="10.10.123.131", timeout=360)
body = {
#
"query": {
"match_all":{
},
"sort": [
"c_date" #
],
"from": 0,
"size": 100
}
result = es.search(index="carlist", doc_type="car", body=body)
# print(result)
print(len(result["hits"]["hits"]))
print(result["hits"]["hits"])
```
2. : ``"sort: { '': { 'order': 'desc' } }"``
```python
from elasticsearch import Elasticsearch
es = Elasticsearch(hosts="10.10.123.131", timeout=360)
body = {
#
"query": {
"match_all":{
},
},
"sort": {
"c_date": {
"order": "desc"
}
},
"from": 0,
"size": 100
}
result = es.search(index="carlist", doc_type="car", body=body)
# print(result)
print(len(result["hits"]["hits"]))
print(result["hits"]["hits"])
```
7. : **match** ,****,
```python
from elasticsearch import Elasticsearch
es = Elasticsearch(hosts="10.10.123.131", timeout=360)
body = {
"query": {
"match": {
"c_name": ""
}
}
}
result = es.search(index="carlist", doc_type="car", body=body)
# print(result)
print(len(result["hits"]["hits"]))
print(result["hits"]["hits"])
```
8. **bool**
1.
```json
{
"query": {
"bool": {
"": [
{
"1": {
"1": "1"
}
},
{
"2": {
"2": "2"
}
},
{
"3": {
"3": "3"
}
},
]
}
}
}
```
2. **must** : ,
```python
from elasticsearch import Elasticsearch
es = Elasticsearch(hosts="10.10.123.131", timeout=360)
body = {
"query": {
"bool": {
"must": [
{
"term": {
"c_name": ""
}
},
{
"term": {
"c_price": 18
}
}
]
}
}
}
result = es.search(index="carlist", doc_type="car", body=body)
# print(result)
print(len(result["hits"]["hits"]))
print(result["hits"]["hits"])
```
3. **should** : ,
```python
from elasticsearch import Elasticsearch
es = Elasticsearch(hosts="10.10.123.131", timeout=360)
body = {
"query": {
"bool": {
"should": [
{
"term": {
"c_name": ""
}
},
{
"term": {
"c_price": 18
}
}
]
}
}
}
result = es.search(index="carlist", doc_type="car", body=body)
# print(result)
print(len(result["hits"]["hits"]))
print(result["hits"]["hits"])
```
4. **must_not** : ,
```python
from elasticsearch import Elasticsearch
es = Elasticsearch(hosts="10.10.123.131", timeout=360)
body = {
"query": {
"bool": {
"must_not": [
{
"term": {
"c_name": ""
}
},
]
}
}
}
result = es.search(index="carlist", doc_type="car", body=body)
# print(result)
print(len(result["hits"]["hits"]))
print(result["hits"]["hits"])
```
5. must should : must shoud ,should bool must
```python
from elasticsearch import Elasticsearch
es = Elasticsearch(hosts="10.10.123.131", timeout=360)
# 18
body = {
"query": {
"bool": {
"must": [
{
"term": {
"c_price": 18
}
},
{
"bool": {
"should": [
{
"term": {
"c_name": ""
}
},
{
"term": {
"c_name": ""
}
}
]
}
}
]
}
}
}
result = es.search(index="carlist", doc_type="car", body=body)
# print(result)
print(len(result["hits"]["hits"]))
print(result["hits"]["hits"])
```
9. **range**
1.
| | |
| ---- | -------- |
| lt | |
| lte | |
| gt | |
| gte | |
2.
```python
from elasticsearch import Elasticsearch
es = Elasticsearch(hosts="10.10.123.131", timeout=360)
body = {
# 10 -15
"query": {
"range": {
"c_price": {
"lt": 15,
"gt": 10
}
}
},
"from": 0,
"size": 100,
}
result = es.search(index="carlist", doc_type="car", body=body)
# print(result)
print(len(result["hits"]["hits"]))
print(result["hits"]["hits"])
```
10. **aggs**
1.
| | |
| ------------ | -------- |
| avg | |
| sum | |
| max | |
| min | |
2.
```
{
"query": {
"": {
"": ""
}
},
"aggs": {
"": {
"": {
"field": ""
}
}
}
}
```
3.
```python
from elasticsearch import Elasticsearch
es = Elasticsearch(hosts="10.10.123.131", timeout=360)
body = {
#
"query": {
"term": {
"c_name": ""
}
},
"aggs": {
"price_avg": {
"avg": {
"field": "c_price"
}
}
}
}
result = es.search(index="carlist", doc_type="car", body=body)
print(result)
print(len(result["hits"]["hits"]))
print(result["aggregations"]["price_avg"]["value"]) #
```
11.
```python
from elasticsearch import Elasticsearch
es = Elasticsearch(hosts="10.10.123.131", timeout=360)
#
body = {
"query": {
"match_all": {
}
},
"aggs": {
"case_sum": {
"terms": {
"field": "c_name"
},
"aggs": {
"price_avg": {
"avg": {
"field": "c_price"
}
}
}
}
}
}
result = es.search(index="carlist", doc_type="car", body=body)
# print(result)
print(len(result["hits"]["hits"]))
print(result["aggregations"]["case_sum"])
print(result["aggregations"]["case_sum"]["buckets"])
```
| Web Proxy Viewer | New URL | Original Page |