准备数据
创建索引(仅含映射)
# -.- coding:utf-8 -.-
# 准备数据
# 产品编号productID不希望被模糊搜索,
# 所以在定义mapping结构时, 声明了index="not_analyzed"
from elasticsearch import Elasticsearch
from pprint import pprint
es = Elasticsearch(hosts=["192.168.1.132"])
es.indices.create(
index="my_store",
body={
"mappings": {
"products": {
"properties": {
"price": {
"type": "integer"
},
"productID": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
)
写入数据
# -.- coding:utf-8 -.-
from elasticsearch import Elasticsearch
from pprint import pprint
es = Elasticsearch(hosts=["192.168.1.132"])
s = es.bulk(
index="my_store",
doc_type="products",
body=[
{"create": {"_id": 1}},
{"price": 10, "productID": "XHDK-A-1293-#fJ3"},
{"create": {"_id": 2}},
{"price": 20, "productID": "KDKE-B-9947-#kL5"},
{"create": {"_id": 3}},
{"price": 30, "productID": "JODL-X-1937-#pV7"},
{"create": {"_id": 4}},
{"price": 30, "productID": "QQPX-R-3956-#aD8"},
]
)
pprint(s)
结构化搜索
SQL:
select * from products where price = 20;
from elasticsearch import Elasticsearch
from pprint import pprint
es = Elasticsearch(hosts=["192.168.1.132"])
s = es.search(
index="my_store",
doc_type="products",
body={
"query": {
"constant_score": {
"filter": {
"term": {
"price": 20
}
}
}
}
}
)
pprint(s)
SQL:
select * from products where price=20 or price=30;
from elasticsearch import Elasticsearch
from pprint import pprint
es = Elasticsearch(hosts=["192.168.1.132"])
s = es.search(
index="my_store",
doc_type="products",
body={
"query": {
"constant_score": {
"filter": {
"terms": {"price": [20, 30]}
}
}
}
}
)
pprint(s)
SQL:
select * from products where productID="XHDK-A-1293-#fJ3";
from elasticsearch import Elasticsearch
from pprint import pprint
es = Elasticsearch(hosts=["192.168.1.132"])
s = es.search(
index="my_store",
doc_type="products",
body={
"query": {
"constant_score": {
"filter": {
"term": {
"productID": "XHDK-A-1293-#fJ3"
}
}
}
}
}
)
pprint(s)
SQL:
select * from products
where (price = 20 or productID="XHDK-A-1293-#fJ3")
and (price != 30);
from elasticsearch import Elasticsearch
from pprint import pprint
es = Elasticsearch(hosts=["192.168.1.132"])
s = es.search(
index="my_store",
doc_type="products",
body={
"query": {
"constant_score": {
"filter": {
"bool": {
"should": [
{"term": {"price": 20}},
{"term": {"productID": "XHDK-A-1293-#fJ3"}}
],
"must_not": {
"term": {"price": 30}
}
}
}
}
}
}
)
pprint(s)
SQL:
select * from products
where productID="KDKE-B-9947-#kL5"
or (productID="JODL-X-1937-#pV7" and price = 30);
from elasticsearch import Elasticsearch
from pprint import pprint
es = Elasticsearch(hosts=["192.168.1.132"])
s = es.search(
index="my_store",
doc_type="products",
body={
"query": {
"constant_score": {
"filter": {
"bool": {
"should": [
{"term": {"productID": "KDKE-B-9947-#kL5"}},
{"bool": {
"must": [
{"term": {"productID": "JODL-X-1937-#pV7"}},
{"term": {"price": 30}}
]
}}
]
}
}
}
}
}
)
pprint(s)
SQL:
select * from products where price between 20 and 40;
from elasticsearch import Elasticsearch
from pprint import pprint
es = Elasticsearch(hosts=["192.168.1.132"])
s = es.search(
index="my_store",
doc_type="products",
body={
"query": {
"constant_score": {
"filter": {
"range": {
"price": {
"gte": 20,
"lte": 40,
}
}
}
}
}
}
)
pprint(s)
参考
- [x] 结构化搜索
网友评论