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)

 

参考

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的阅读 13,567评论 5 6
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 10,083评论 0 23
  • “杀杀杀!” 苍穹圣者嘶吼一声,顿时间让前方的巨大人潮停顿一秒,他的威慑力即使对方是千军万马,自己只是孤身一人也还...
    隔江枫火阅读 349评论 0 0
  • 以前没有想过怎样去爱一个人,也不愿意去想,以为只要随着时间的推移就会懂得怎样去爱。单纯的以为船到桥头自然直,万物到...
    雅风_ee62阅读 185评论 0 0
  • 最近天气热,我把我们家巧克力的毛毛都剃掉了,本来是一只很可爱的贵宾狗狗,毛卷卷的很可爱,现在被剃毛,只剩表皮一层...
    我滴个神唉阅读 276评论 0 0