Elasticsearch06结构化搜索
Elasticsearch06结构化搜索
AllenOR灵感 发表于9个月前
Elasticsearch06结构化搜索
  • 发表于 9个月前
  • 阅读 4
  • 收藏 0
  • 点赞 0
  • 评论 0

移动开发云端新模式探索实践 >>>   

准备数据

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

 

参考

  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 10
博文 2139
码字总数 82983
×
AllenOR灵感
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: