Elasticsearch—DSL搜索实践

POST        http://192.168.2.223:9200/shop/_mapping
{
    "properties": {
        "id": {
            "type": "long"
        },
        "age": {
            "type": "integer"
        },
        "username": {
            "type": "keyword"
        },
        "nickname": {
            "type": "text",
            "analyzer": "ik_max_word"
        },
        "money": {
            "type": "float"
        },
        "desc": {
            "type": "text",
            "analyzer": "ik_max_word"
        },
        "sex": {
            "type": "byte"
        },
        "birthday": {
            "type": "date"
        },
        "face": {
            "type": "text",
            "index": false
        }
    }
}
POST   http://192.168.2.223:9200/shop/_doc/1001
{
"id": 1011,
"age": 31,
"username": "sprder",
"nickname": "皮特帕克",
"money": 180.8,
"desc": "它是一个超级英雄",
"sex": 1,
"birthday": "1989-08-14",
"face": "https://www.zhouhong.com/static/img/index/logo.png"
}
{
"id": 1008,
"age": 19,
"username": "zhoujiang",
"nickname": "周江",
"money": 1056.8,
"desc": "周江大学毕业后,进了阿里",
"sex": 1,
"birthday": "1995-06-14",
"face": "https://www.zhouhong.com/static/img/index/logo.png"
}
{
"id": 1007,
"age": 19,
"username": "msgame",
"nickname": "gamexbox",
"money": 1056.8,
"desc": "明天去进货,最近微软处理很多游戏机,还要买xbox游戏卡带",
"sex": 1,
"birthday": "1985-05-14",
"face": "https://www.zhouhong.com/static/img/index/logo.png"
}
{
"id": 1003,
"age": 20,
"username": "bigFace",
"nickname": "飞翔的巨鹰",
"money": 66.8,
"desc": "周江和导游坐飞机去海外旅游,去了新马泰和欧洲",
"sex": 1,
"birthday": "1996-01-14",
"face": "https://www.zhouhong.com/static/img/index/logo.png"
}
{
"id": 1002,
"age": 19,
"username": "zhouhong",
"nickname": "周红",
"money": 77.8,
"desc": "今天上下班都很堵,车流量很大",
"sex": 1,
"birthday": "1993-01-24",
"face": "https://www.zhouhong.com/static/img/index/logo.png"
}
{
 "id": 1012,
"age": 31,
"username": "super hero",
"nickname": "super hero",
"money": 188.8,
"desc": "BatMan, GreenArrow, SpiderMan, IronMan... are all Super Hero",
"sex": 1,
"birthday": "1980-08-14",
"face": "https://www.zhouhong.com/static/img/index/logo.png"
}
{
"id": 1010,
"age": 30,
"username": "tata",
"nickname": "隔壁老王",
"money": 100.8,
"desc": "隔壁老外去国外出差,带给我很多好吃的",
"sex": 1,
"birthday": "1988-07-14",
"face": "https://www.zhouhong.com/static/img/index/logo.png"
}
{
 "id": 1009,
"age": 22,
"username": "shaonian",
"nickname": "骚年轮",
"money": 96.8,
"desc": "骚年在大学毕业后,考研究生去了",
"sex": 1,
"birthday": "1998-07-14",
"face": "https://www.zhouhong.com/static/img/index/logo.png"
}
{
"id": 1006,
"age": 19,
"username": "zhouhong",
"nickname": "我叫周红",
"money": 156.8,
"desc": "我叫周红,今年20岁,是一名毕业生,我在琦䯲星球做演讲",
"sex": 1,
"birthday": "1993-04-14",
"face": "https://www.zhouhong.com/static/img/index/logo.png"
}
{
"id": 1005,
"age": 25,
"username": "gotoplay",
"nickname": "ps游戏机",
"money": 155.8,
"desc": "今年生日,女友送了我一台play station游戏机,非常好玩,非常不错",
"sex": 1,
"birthday": "1989-03-14",
"face": "https://www.zhouhong.com/static/img/index/logo.png"
}
{
 "id": 1004,
"age": 22,
"username": "flyfish",
"nickname": "水中鱼",
"money": 55.8,
"desc": "昨天周红在学校的池塘里,看到有很多鱼在游泳",
"sex": 0,
"birthday": "1988-02-14",
"face": "https://www.zhouhong.com/static/img/index/logo.png"
}
{
 "id": 1001,
"age": 18,
"username": "zhoujiang",
"nickname": "周江",
"money": 88.8,
"desc": "周江在大学学习java和前端",
"sex": 0,
"birthday": "1992-12-24",
"face": "https://www.zhouhong.com/static/img/index/logo.png"
}
{
    "took": 8,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 1,
        "hits": [
            {
                "_index": "shop",
                "_type": "_doc",
                "_id": "1003",
                "_score": 1,
                "_source": {
                    "id": 1003,
                    "age": 20,
                    "username": "bigFace",
                    "nickname": "飞翔的巨鹰",
                    "money": 66.8,
                    "desc": "周江和导游坐飞机去海外旅游,去了新马泰和欧洲",
                    "sex": 1,
                    "birthday": "1996-01-14",
                    "face": "https://www.zhouhong.com/static/img/index/logo.png"
                }
            }
        ]
    }
}
{
    "query": {
        "match_all": {}
    }
}
{
    "query": {
        "match_all": {}
    },
    "_source": ["id","username","age"]
}
{
    "query": {
        "match_all": {}
    },
    "_source": ["id","username","age"],
    "from": 0,
    "size": 5
}
{
    "query": {
        "match_phrase": {
            "desc": {
                "query": "今天 车流量",
                "slop": 100
            }
        }
    }
}
{
    "query": {
        "term": {
            "desc": "学习"
        }
    }
}
  • *terms 对多个关键字查询
{
    "query": {
        "terms": {
            "desc": ["学习","周红","周江"]
        }
    }
}
{
    "query": {
        "match": {
            "desc": "周红"
        }
    },
    "_source": ["id","username","age"]
}
{
    "query": {
        "match": {
            "desc": {
                "query": "周红",
                "operator": "and"
            }
        }
    },
    "_source": ["id","username","age"]
}
{
    "query": {
        "match": {
            "desc": {
                "query": "女友生日送我好玩的xbox游戏机",
                "minimum_should_match": "60%"
            }
        }
    }
}
{
    "query": {
        "ids": {
            "type": "_doc",
            "values": ["1001","1005","1006"]
        }
    },
    "_source": ["id","username","desc"]
}
{
    "query": {
        "multi_match": {
            "query": "游戏",
            "fields": [
                "desc^10","nickname"
            ]
        }
    }
}
{
    "query": {
        "bool": {
            "must": [
                {
                    "multi_match": {
                        "query": "游戏",
                        "fields": ["desc","nickname"]
                    }
                },
                {
                    "term": {
                        "age": "19"
                    }
                }
            ]
        }
    }
}
  • post_filter元素是一个顶层元素,只会对搜索结果进行过滤。不会计算数据的匹配度相关性分数,不会根据分数去排序,query则相反,会计算分数,也会按照分数去排序。
  • 使用场景:

实操:查询账户金额大于80元,小于等于155.8元的用户

gte:大于等于

lte:小于等于

gt:大于

lt:小于

{
    "query": {
        "match": {
            "sex": "1"
        }
    },
    "post_filter": {
        "range": {
            "money": {
                "gte": 60,
                "lte": 155.8
            }
        }
    }
}
{
    "query": {
        "match": {
            "sex": "1"
        }
    },
    "sort": [
        {
            "money": "asc"
        },
        {
            "age": "asc"
        }
    ]
}
1.创建索引
POST        /shop2/_mapping
{
    "properties": {
        "id": {
            "type": "long"
        },
        "nickname": {
            "type": "text",
            "analyzer": "ik_max_word",
            "fields": {
                "keyword": {
                    "type": "keyword"
                }
            }
        }
    }
}
2.插入数据
POST         /shop2/_doc
{
    "id": 1001,
    "nickname": "美丽的风景"
}
{
    "id": 1002,
    "nickname": "漂亮的小哥哥"
}
{
    "id": 1003,
    "nickname": "飞翔的巨鹰"
}
{
    "id": 1004,
    "nickname": "完美的天空"
}
{
    "id": 1005,
    "nickname": "广阔的海域"
}
3.排序     POST    http://192.168.2.223:9200/shop2/_doc/_search
{
    "sort": [
        {
            "nickname.keyword": "desc"
        }
    ]
}
{
    "query": {
        "exists": {
            "field": "desc"
        }
    }
}
{
    "query": {
        "match": {
            "desc": "周红"
        }
    },
    "highlight": {
        "pre_tags": ["<span>"],
        "post_tags": ["</span>"],
        "fields": {
            "desc": {}
        }
    }
}
&#x7ED3;&#x679C;&#xFF1A;&#x9ED8;&#x8BA4;&#x4E3A;em&#x6807;&#x7B7E;&#xFF0C;&#x4E0A;&#x9762;&#x8BBE;&#x7F6E;&#x4E3A;&#x81EA;&#x5B9A;&#x4E49;&#x7684;<span>&#x6807;&#x7B7E;&#xFF0C;&#x5BF9;&#x9875;&#x9762; em/span &#x6807;&#x7B7E;&#x505A;&#x4E00;&#x4E2A;&#x989C;&#x8272;&#x8BBE;&#x7F6E;&#x5C31;&#x53EF;&#x4EE5;&#x5B9E;&#x73B0;&#x9AD8;&#x4EAE;&#x663E;&#x793A;&#x4E86;&#x3002;</span>
{
    "took": 110,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": 1.1329247,
        "hits": [
            {
                "_index": "shop",
                "_type": "_doc",
                "_id": "1004",
                "_score": 1.1329247,
                "_source": {
                    "id": 1004,
                    "age": 22,
                    "username": "flyfish",
                    "nickname": "&#x6C34;&#x4E2D;&#x9C7C;",
                    "money": 55.8,
                    "desc": "&#x6628;&#x5929;&#x5468;&#x7EA2;&#x5728;&#x5B66;&#x6821;&#x7684;&#x6C60;&#x5858;&#x91CC;&#xFF0C;&#x770B;&#x5230;&#x6709;&#x5F88;&#x591A;&#x9C7C;&#x5728;&#x6E38;&#x6CF3;",
                    "sex": 0,
                    "birthday": "1988-02-14",
                    "face": "https://www.zhouhong.com/static/img/index/logo.png"
                },
                "highlight": {
                    "desc": [
                        "&#x6628;&#x5929;<em>&#x5468;&#x7EA2;</em>&#x5728;&#x5B66;&#x6821;&#x7684;&#x6C60;&#x5858;&#x91CC;&#xFF0C;&#x770B;&#x5230;&#x6709;&#x5F88;&#x591A;&#x9C7C;&#x5728;&#x6E38;&#x6CF3;"
                    ]
                }
            },
            {
                "_index": "shop",
                "_type": "_doc",
                "_id": "1006",
                "_score": 0.9585575,
                "_source": {
                    "id": 1006,
                    "age": 19,
                    "username": "zhouhong",
                    "nickname": "&#x6211;&#x53EB;&#x5468;&#x7EA2;",
                    "money": 156.8,
                    "desc": "&#x6211;&#x53EB;&#x5468;&#x7EA2;&#xFF0C;&#x4ECA;&#x5E74;20&#x5C81;&#xFF0C;&#x662F;&#x4E00;&#x540D;&#x6BD5;&#x4E1A;&#x751F;&#xFF0C;&#x6211;&#x5728;&#x7426;&#x4BF2;&#x661F;&#x7403;&#x505A;&#x6F14;&#x8BB2;",
                    "sex": 1,
                    "birthday": "1993-04-14",
                    "face": "https://www.zhouhong.com/static/img/index/logo.png"
                },
                "highlight": {
                    "desc": [
                        "&#x6211;&#x53EB;<em>&#x5468;&#x7EA2;</em>&#xFF0C;&#x4ECA;&#x5E74;20&#x5C81;&#xFF0C;&#x662F;&#x4E00;&#x540D;&#x6BD5;&#x4E1A;&#x751F;&#xFF0C;&#x6211;&#x5728;&#x7426;&#x4BF2;&#x661F;&#x7403;&#x505A;&#x6F14;&#x8BB2;"
                    ]
                }
            }
        ]
    }
}

Original: https://www.cnblogs.com/Tom-shushu/p/14433888.html
Author: Tom-shushu
Title: Elasticsearch—DSL搜索实践

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/607754/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球