美文网首页
Elasticsearch06结构化搜索

Elasticsearch06结构化搜索

作者: 极光火狐狸 | 来源:发表于2017-08-25 01:46 被阅读29次

准备数据

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

 

参考

相关文章

  • Elasticsearch06结构化搜索

    准备数据 创建索引(仅含映射) 写入数据 结构化搜索 SQL:select * from products whe...

  • 结构化搜索

    总结 本章介绍es的结构化搜索,通过demo演示es的结构化搜索 结构化搜索 什么是结构化搜索结构化搜索就是对结构...

  • 4.2-结构化搜索

    结构化数据 结构化搜索(Structured search) 是指对结构化数据的搜索⽇期,布尔类型和数字都是结构化...

  • 【elasticsearch】13、结构化搜索

    结构化数据 结构化搜索(structured search)是指对结构化数据的搜索日期,布尔类型和数字都是结构化的...

  • 结构化搜索

    一、结构化数据 1.1 结构化搜索(Structured search)是指对结构化数据的搜索 日期,布尔类型和数...

  • ElasticSearch - 结构化搜索

    结构化数据 结构化搜索是指对结构化数据的搜索日期,布尔类型和数字都是结构化的 文本也可以是结构化的。比如笔的颜色:...

  • 几个搜索的相关话题

    结构化搜索与全文搜索 ES在搜索时有两种类型,即全文搜索与结构化搜索。其相对应于"term"系列的查询 和 "ma...

  • 7.7-第二部分总结与测验

    回顾总结:搜索与算分 结构化搜索与⾮结构化搜索Term 查询和基于全⽂本 Match 搜索的区别对于需要做精确匹配...

  • 为什么用Elasticsearch做全文搜索而不用MySQL

    数据类型 全文搜索引擎很好的支持非结构化数据的搜索,可以更好地快速搜索大量存在的任何单词非结构化文本。例如Goog...

  • 结构化搜索

    结构化搜索:在结构化查询中,我们得到的结果总是非是即否,要么存在于集合之中,要么存在于集合之外,结构化查询不关心文...

网友评论

      本文标题:Elasticsearch06结构化搜索

      本文链接:https://www.haomeiwen.com/subject/hgbydxtx.html