• (14)ElasticSearch 基本查询(中文查询)


      1、准备数据

    PUT /lib
    {
        "settings":{
            "number_of_shards":3,
            "number_of_replicas":0
        },
        "mappings":{
            "user":{
                "properties":{
                    "name":{
                        "type":"text",
                        "analyzer":"ik_max_word"
                    },
                    "address":{
                        "type":"text",
                        "analyzer":"ik_max_word"
                    },
                    "age":{
                        "type":"integer"
                    },
                    "interests":{
                        "type":"text",
                        "analyzer":"ik_max_word"
                    },
                    "birthday":{
                        "type":"date"
                    }
                }
            }
        }
    }
    PUT /lib/user/1
    {
        "name":"赵六",
        "address":"黑龙江省铁岭",
        "age":50,
        "birthday":"1970-12-12",
        "interests":"喜欢喝酒,锻炼,说相声"
    }
    
    PUT /lib/user/2
    {
        "name":"赵明",
        "address":"北京海淀区清河",
        "age":20,
        "birthday":"1998-10-12",
        "interests":"喜欢喝酒,锻炼,唱歌"
    }
    
    PUT /lib/user/3
    {
        "name":"lisi",
        "address":"北京海淀区清河",
        "age":23,
        "birthday":"1998-10-12",
        "interests":"喜欢喝酒,锻炼,唱歌"
    }
    
    PUT /lib/user/4
    {
        "name":"王五",
        "address":"北京海淀区清河",
        "age":26,
        "birthday":"1995-10-12",
        "interests":"喜欢编程,听音乐,旅游"
    }
    
    PUT /lib/user/5
    {
        "name":"张三",
        "address":"北京海淀区清河",
        "age":29,
        "birthday":"1988-10-12",
        "interests":"喜欢摄影,听音乐,跳舞"
    }

      2、操作演示

      1)查询出name中带有赵字的,赵明和赵六的文档

    GET /lib/user/_search/
    {
        "query":{
            "term":{"name":""}
        }
    }
    GET /lib/user/_search/
    {
        "query":{
            "match":{"name":""}
        }
    }

      2)查询出interests中含有喝酒或者唱歌的文档

    GET /lib/user/_search/
    {
        "query":{
            "terms":{
                "interests":["喝酒","唱歌"]
            }
        }
    }

      3)查询出interests中含有喝酒或者唱歌的文档,从下标为0开始,只查询出两个

    GET /lib/user/_search/
    {
        "from":0,
        "size":2,
        "query":{
            "terms":{
                "interests":["喝酒","唱歌"]
            }
        }
    }

      4)查询出interests中含有喝酒或者唱歌的文档,并且 显示版本号

    GET /lib/user/_search/
    {
        "version":true,
        "query":{
            "terms":{
                "interests":["喝酒","唱歌"]
            }
        }
    }

      5)查询出name含有赵或者含有六的文档,会查询出赵明和赵六,因为match会分词。

    GET /lib/user/_search/
    {
        "query":{
            "match":{
                "name":"赵六"
            }
        }
    }

      6)查询出age是20的文档,结果显示赵明,数字型不分词。

    GET /lib/user/_search/
    {
        "query":{
            "match":{
                "age":20
            }
        }
    }

      7)查询出所有文档

    GET /lib/user/_search/
    {
        "query":{
            "match_all":{}
        }
    }

      8)多字段查询multi_match,字段name或者interests中含有唱歌的都会查询出来

    GET /lib/user/_search/
    {
        "query":{
            "multi_match":{
                "query":"唱歌",
                "fields":["interests","name"]
            }
        }
    }

      9)短语匹配查询match_phrase,关键字作为一个短语,必须完全匹配,如下,必须含有“锻炼,说相声”这个短语才能查到

    GET /lib/user/_search/
    {
        "query":{
            "match_phrase":{
                "interests":"锻炼,说相声"
            }
        }
    }

      10)指定返回的字段 _source,如下,返回的结果中只有 address和 name

    GET /lib/user/_search/
    {
        "_source":["address","name"],
        "query":{
            "match":{
                "interests":"唱歌"
            }
        }
    }

      11)前缀匹配查询 match_phrase_prefix,查询name,以“赵”开头

    GET /lib/user/_search/
    {
        "query":{
            "match_phrase_prefix":{
                "name":{"query":""}
            }
        }
    }
  • 相关阅读:
    VB已死?还是会在Roslyn之下焕发新生?
    GitHub在Visual Studio 2015中获得TFS/VSO同等地位
    单体应用与微服务优缺点辨析
    对于JavaScript的函数.NET开发人员应该知道的11件事
    TypeScript 1.5 Beta带来修饰元数据支持
    Visual Studio 2015 RC中的ASP.NET新特性和问题修正
    Visual Studio从此走入非Windows程序猿家
    Azure DocumentDB对比MongoDB
    正确理解DTO、值对象和POCO
    大数据技术之_19_Spark学习_05_Spark GraphX 应用解析小结
  • 原文地址:https://www.cnblogs.com/javasl/p/11405395.html
Copyright © 2020-2023  润新知