【ElasticSearch 6.*】 学习九:高级查询之子条件查询

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_31617637/article/details/85109577

概念

高级查询分为子条件查询复合条件查询

子条件查询:在查询过程中,除了判断文档是否满足查询条件外,ES还会计算一个_socre来标识匹配的程度,旨在判断目标文档和查询条件匹配的有多好(匹配度)。子条件查询包括Query ContextFilter context两种查询。

queryContext 常用查询1 全文本查询:针对文本类型数据

  • 模糊匹配
    请求连接(POSThttp://localhost:9200/book/_search
    请求参数(条件):

    {
    	"query": {
    		"match":{
    			"title": "陆小凤啊"
    		}
    	}
    }
    

    查询返回结果:
    不单有陆小凤啊 ,还有别的,是因为系统把title查询的做了分词处理,然后同时模糊查询的。

    {
        "took": 1836,
        "timed_out": false,
        "_shards": {
            "total": 3,
            "successful": 3,
            "skipped": 0,
            "failed": 0
        },
        "hits": {
            "total": 10,
            "max_score": 4.688145,
            "hits": [
                {
                    "_index": "book",
                    "_type": "noval",
                    "_id": "4",
                    "_score": 4.688145,
                    "_source": {
                        "title": "陆小凤啊",
                        "author": "古龙",
                        "word_count": "11",
                        "publish_date": "1910-09-20"
                    }
                },
                {
                    "_index": "book",
                    "_type": "noval",
                    "_id": "9",
                    "_score": 0.36826366,
                    "_source": {
                        "title": "杨过啊",
                        "author": "金庸",
                        "word_count": "1111",
                        "publish_date": "1910-09-20"
                    }
                },
                {
                    "_index": "book",
                    "_type": "noval",
                    "_id": "3",
                    "_score": 0.32590744,
                    "_source": {
                        "title": "楚留香啊",
                        "author": "古龙",
                        "word_count": "2811",
                        "publish_date": "1990-09-20"
                    }
                },
                {
                    "_index": "book",
                    "_type": "noval",
                    "_id": "10",
                    "_score": 0.32590744,
                    "_source": {
                        "title": "铁中棠啊",
                        "author": "金庸",
                        "word_count": "1111",
                        "publish_date": "1910-09-20"
                    }
                },
                {
                    "_index": "book",
                    "_type": "noval",
                    "_id": "11",
                    "_score": 0.26054117,
                    "_source": {
                        "title": "丁鹏啊",
                        "author": "金庸",
                        "word_count": "2221",
                        "publish_date": "1910-09-20"
                    }
                },
                {
                    "_index": "book",
                    "_type": "noval",
                    "_id": "5",
                    "_score": 0.23251483,
                    "_source": {
                        "title": "李寻欢啊",
                        "author": "古龙",
                        "word_count": "1221",
                        "publish_date": "1910-09-20"
                    }
                },
                {
                    "_index": "book",
                    "_type": "noval",
                    "_id": "6",
                    "_score": 0.23251483,
                    "_source": {
                        "title": "花满楼啊",
                        "author": "古龙",
                        "word_count": "22",
                        "publish_date": "1910-09-20"
                    }
                },
                {
                    "_index": "book",
                    "_type": "noval",
                    "_id": "7",
                    "_score": 0.20993245,
                    "_source": {
                        "title": "西门吹雪啊",
                        "author": "古龙",
                        "word_count": "2222",
                        "publish_date": "1910-09-20"
                    }
                },
                {
                    "_index": "book",
                    "_type": "noval",
                    "_id": "8",
                    "_score": 0.19363807,
                    "_source": {
                        "title": "郭靖啊",
                        "author": "金庸",
                        "word_count": "212",
                        "publish_date": "1910-09-20"
                    }
                },
                {
                    "_index": "book",
                    "_type": "noval",
                    "_id": "12",
                    "_score": 0.17225473,
                    "_source": {
                        "title": "鸠摩智啊",
                        "author": "金庸",
                        "word_count": "44",
                        "publish_date": "1910-09-20"
                    }
                }
            ]
        }
    }
    
  • 短语匹配/习语匹配(精确匹配)
    请求连接(POSThttp://localhost:9200/book/_search
    请求参数(条件):
    match_phrase:短语匹配关键字

    {
    	"query": {
    		"match_phrase":{
    			"title": "陆小凤啊"
    		}
    	}
    }
    

    查询返回结果:

    {
        "took": 6944,
        "timed_out": false,
        "_shards": {
            "total": 3,
            "successful": 3,
            "skipped": 0,
            "failed": 0
        },
        "hits": {
            "total": 1,
            "max_score": 4.688145,
            "hits": [
                {
                    "_index": "book",
                    "_type": "noval",
                    "_id": "4",
                    "_score": 4.688145,
                    "_source": {
                        "title": "陆小凤啊",
                        "author": "古龙",
                        "word_count": "11",
                        "publish_date": "1910-09-20"
                    }
                }
            ]
        }
    }
    
  • 多个字段模糊匹配查询
    请求参数
    multi_match:多个字段的关键字
    query:模糊匹配的内容
    fields: 字段的集合

    {
    	"query": {
    		"multi_match":{
    			"query": "陆小凤啊",
    			"fields": ["author","title"]
    		}
    	}
    }
    
    
  • 语法查询
    请求参数
    query_string: 语法查询的关键字
    query:查询条件 and和or 必须大写
    fields:查询字段(可选

    //OR 或者
    {
    	"query": {
    		"query_string":{
    			"query": "陆小凤 OR 金庸",
    			"fields": ["author","title"]
    		}
    	}
    }
       //AND 
    {
    	"query": {
    		"query_string":{
    			"query": "(陆小凤 AND西门) OR 金庸",
    			"fields": ["author","title"]
    		}
    	}
    }
    

queryContext 常用查询2 字段级别查询: 针对结构化数据,如数字、日期等

  • 具体项查询
    请求参数:
    term:具体项
    {
    	"query": {
    		"term":{
    			"author": "金庸"
    		}
    	}
    }
    
  • 范围查询(int类型和时间类型):
    请求字段:
    range:范围查询关键字
    gte:大于等于
    gt:大于
    lte:小于等于
    lt:小于
    请求参数:
    {
    	"query": {
    		"range":{
    			"word_count": {
    				"gte": "100",
    				"lte": "2000"
    			}
    		}
    	}
    }
    //当前可以使用now 来表示
    {
    	"query": {
    		"range":{
    			"publish_date": {
    				"gte": "1990-09-20",
    				"lte": "now"
    			}
    		}
    	}
    }
    

Filter context

在查询过程中,只判断该文档是否满足条件,只有Yes或者No

查询条件
filter:数据过滤 关键字 配合bool 关键字一起使用

{
	"query": {
		"bool":{
			"filter": {
				"term": {
					"word_count": 2222
				}
			}
		}
	}
}

返回值:

{
    "took": 3856,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 0,
        "hits": [
            {
                "_index": "book",
                "_type": "noval",
                "_id": "7",
                "_score": 0,
                "_source": {
                    "title": "西门吹雪啊",
                    "author": "古龙",
                    "word_count": "2222",
                    "publish_date": "1910-09-20"
                }
            }
        ]
    }
}

tip:filter context 查询到的数据会被缓存,第二次查询会加快。

猜你喜欢

转载自blog.csdn.net/qq_31617637/article/details/85109577