Elasticsearch高级查询

##########################高级查询
子条件查询:特定字段查询所指定值
1.Query context
  在查询过程中,除了判断文档是否满足查询条件外,
  ES还会计算一个—_score来标识匹配的程度,用来判断目标和查询条件有多好
    1.全文本查询:针对文本类型数据
        1.模糊匹配:match
        localhost:9200/book/_search
        方式:POST
        {
            "query":{
                "match":{
                    "author":"哈哈"
                }
            }
        }

        2.短语匹配(match_phrase)
        localhost:9200/book/_search
        方式:POST
        {
            "query":{
                "match_phrase":{
                    "author":"哈哈"
                }
            }
        }

        3.多字段模糊匹配(multi_match)
        localhost:9200/book/_search
        方式:POST
        {
            "query":{
                "multi_match":{
                    "query":"哈哈",
                    "fields":["author","title"]
                }
            }
        }
        4.语法查询(qurry_string)
        localhost:9200/book/_search
        方式:POST
        {
            "query":{
                "qurey_string":{
                    "query":"哈哈 or 大法"
                }
            }
        }

        {
            "query":{
                "qurey_string":{
                    "query":"哈哈 or 大法",
                    "fields":["title","author"]
                }
            }
        }


    2.字段级别查询:针对结构化数据,如数字日期等
    localhost:9200/book/_search
    方式:POST
    {
        "query":{
            "term":{    ##字段关键字
                "author":"刘大壮"
            }
        }
    }

    {
        "query":{
            "range":{  ######范围
                "word_count":e{
                    "gte":1000 ###大于等于
                    "lte":10000 ###小于等于
                }
            }
        }
    }

2.Filter context结合bool关键字使用
  符合条件查询:以一定的逻辑组合子条件查询
  在查询过程中,只判断该文档是否满足条件,只有Yes或者No
  localhost:9200/book/_search
  方式:POST
   {
    "query":{
        "bool":{
            "filter":{
                "term":{
                    "word_count":1000
                }
            }
        }
    }
   }
1.大部分filter查询不query速度
2.filter不会计算相关度得分,且结果会有缓存,效率高
3.全文搜索,评分排序使用query(其他一般都用filter)
4.是非过滤,精确匹配,使用filter

3. 复合条件查询
   localhost:9200/_search
   方式:POST
   1.固定分数查询(constant_score)不支持match,只支持filter
   {
    "query":{
        "constant_score":{   ######固定分数查询关键字
            "filter":{
                "match":{
                    "title":"大法"
                }        
            },
            "boost":2   ###指定分数
        }
    }
   }
   2.布尔查询(should和must和must_not的区别)
   ####################should
   {
    "query":{
        "bool":{
            "should":[ ###里面的条件是“或”的关系
                {
                    "match":{
                        "author":"哈哈"                    
                    },
                    "match":{
                        "title":"大法"
                    }
                }
            ]
        }
    }
   }
   ##################must
    {
    "query":{
        "bool":{
            "must":[ ###里面的条件是“与”的关系
                {
                    "match":{
                        "author":"哈哈"                    
                    },
                    "match":{
                        "title":"大法"
                    }
                }
            ]
        }
    }
   }
   ################must+filter
    {
    "query":{
        "bool":{
            "must":[ ###里面的条件是“与”的关系
                {
                    "match":{
                        "author":"哈哈"                    
                    },
                    "match":{
                        "title":"大法"
                    }
                }
            ],
            "filter":{
                "term":{
                    "word_count":1000
                }
            }
        }
    }
   }
   ####################must_not
   {
    "query":{
        "bool":{
            "must_not":{
                "term":{
                    "author":"张三"
                }
            }
        }
    }
   }

猜你喜欢

转载自my.oschina.net/zhouwang93/blog/1786638