ElasticSearch高级查询

ElasticSearch高级查询

https://www.imooc.com/video/15759/0

ElasticSearch查询





1,子条件查询:特定字段查询所指特定值

1.1query context,有_score
1.1.1全文本查询,针对文本类型数据
1.1.1.1 模糊匹配
POST http://127.0.0.1/book/_search
{
    "query":{
        "match":{
            "author":"瓦力"
        }
    }
}
{
    "query":{
        "match":{
            "title":"ElasticSearch入门"
        }
    }
}


1.1.1.2 习语匹配
{
    "query":{
        "match_phrase":{
            "title":"ElasticSearch入门"
        }
    }
}

1.1.1.3 多字段匹配
{
    "query":{
        "multi_match":{
            "query":"瓦力",
            "fields":["author","title"]
        }
    }
}

1.1.1.4 语法查询
{
    "query":{
        "query_string":{
            "query":"ElasticSearch AND 大法"
        }
    }
}

{
    "query":{
        "query_string":{
            "query":"(ElasticSearch AND 大法) OR Python"
        }
    }
}

{
    "query":{
        "query_string":{
            "query":"瓦力 OR ElasticSearch",
            "fields":["title","author"]
        }
    }
}
1.1.2字段级别查询,针对结构化数据,如数字、日期等

{
    "query":{
        "term":{
            "word_count":1000
        }
    }
}

{
    "query":{
        "term":{
            "author":"瓦力"
        }
    }
}
{
    "query":{
        "range":{
            "word_count":{
                "gte":1000,
                "lte":2000
            }
        }
    }
}
{
    "query":{
        "range":{
            "word_count":{
                "gt":1000,
                "lte":2000
            }
        }
    }
}
{
    "query":{
        "range":{
            "publish_date":{
                "gte":"2017-01-01",
                "lte":"2017-12-31"
            }
        }
    }
}
{
    "query":{
        "range":{
            "publish_date":{
                "gte":"2017-01-01",
                "lte":"now"
            }
        }
    }
}
1.2filter context
filter表示查找是不是

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



2,复合条件查询:以一定的逻辑组合子条件查询


2.1 固定分数查询
{
    "query": {
        "match": {
            "title": "ElasticSearch"
        }
    }
}

{
    "query": {
        "constant_score": {
            "filter": {
                "match": {
                    "title": "ElasticSearch"
                }
            }
        }
    }
}

{
    "query": {
        "constant_score": {
            "filter": {
                "match": {
                    "title": "ElasticSearch"
                }
            },
            "boost": 2
        }
    }
}

2.2 bool查询

should - 表示 或者
{
    "query": {
        "bool": { 
            "should": [
                {
                    "match":{
                        "author": "瓦力"
                    }
                },
                {
                    "match": {
                        "title": "ElasticSearch"
                    }
                    
                }
                
            ]
        }
    }
}

must - 表示 必须

{
    "query": {
        "bool": { 
            "must": [
                {
                    "match":{
                        "author": "瓦力"
                    }
                },
                {
                    "match": {
                        "title": "ElasticSearch"
                    }
                    
                }
                
            ]
        }
    }
}

{
    "query": {
        "bool": { 
            "must": [
                {
                    "match":{
                        "author": "瓦力"
                    }
                },
                {
                    "match": {
                        "title": "ElasticSearch"
                    }
                    
                }
                
            ],
            "filter": [
                {
                    "term": {
                        "word_count": 1000
                    }
                }
            ]
        }
    }
}
{
    "query": {
        "bool": { 
            "must_not": {
                "term": {
                    "author": "瓦力"
                }
            }
        }
    }
}

https://www.imooc.com/video/15759/0
ElasticSearch查询




1,子条件查询:特定字段查询所指特定值
1.1query context,有_score1.1.1全文本查询,针对文本类型数据1.1.1.1 模糊匹配POST http://127.0.0.1/book/_search{    "query":{        "match":{            "author":"瓦力"        }    }}{    "query":{        "match":{            "title":"ElasticSearch入门"        }    }}

1.1.1.2 习语匹配{    "query":{        "match_phrase":{            "title":"ElasticSearch入门"        }    }}
1.1.1.3 多字段匹配{    "query":{        "multi_match":{            "query":"瓦力",            "fields":["author","title"]        }    }}
1.1.1.4 语法查询{    "query":{        "query_string":{            "query":"ElasticSearch AND 大法"        }    }}
{    "query":{        "query_string":{            "query":"(ElasticSearch AND 大法) OR Python"        }    }}
{    "query":{        "query_string":{            "query":"瓦力 OR ElasticSearch",            "fields":["title","author"]        }    }}1.1.2字段级别查询,针对结构化数据,如数字、日期等
{    "query":{        "term":{            "word_count":1000        }    }}
{    "query":{        "term":{            "author":"瓦力"        }    }}{    "query":{        "range":{            "word_count":{                "gte":1000,                "lte":2000            }        }    }}{    "query":{        "range":{            "word_count":{                "gt":1000,                "lte":2000            }        }    }}{    "query":{        "range":{            "publish_date":{                "gte":"2017-01-01",                "lte":"2017-12-31"            }        }    }}{    "query":{        "range":{            "publish_date":{                "gte":"2017-01-01",                "lte":"now"            }        }    }}1.2filter contextfilter表示查找是不是
{"query": {"bool": {"filter": {"term": {"word_count": 1000}}}}}


2,复合条件查询:以一定的逻辑组合子条件查询

2.1 固定分数查询{"query": {"match": {"title": "ElasticSearch"}}}
{"query": {"constant_score": {"filter": {"match": {"title": "ElasticSearch"}}}}}
{"query": {"constant_score": {"filter": {"match": {"title": "ElasticSearch"}},"boost": 2}}}
2.2 bool查询
should - 表示 或者{"query": {"bool": { "should": [{"match":{"author": "瓦力"}},{"match": {"title": "ElasticSearch"}}]}}}
must - 表示 必须
{"query": {"bool": { "must": [{"match":{"author": "瓦力"}},{"match": {"title": "ElasticSearch"}}]}}}
{"query": {"bool": { "must": [{"match":{"author": "瓦力"}},{"match": {"title": "ElasticSearch"}}],"filter": [{"term": {"word_count": 1000}}]}}}{"query": {"bool": { "must_not": {"term": {"author": "瓦力"}}}}}

原文地址:https://www.cnblogs.com/stono/p/9060433.html