Elasticsearch06结构化搜索

准备数据

创建索引(仅含映射)
# -.- 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)

 

参考

转载于:https://my.oschina.net/u/3579120/blog/1533357

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值