欢迎关注大数据技术架构与案例微信公众号:过往记忆大数据
过往记忆博客公众号iteblog_hadoop
欢迎关注微信公众号:
过往记忆大数据

23种非常有用的ElasticSearch查询例子(4)

  本系列文章将展示ElasticSearch中23种非常有用的查询使用方法。由于篇幅原因,本系列文章分为六篇,本文是此系列的第四篇文章。欢迎关注大数据技术博客微信公共账号:iteblog_hadoop。

Simple Query String(简单查询字符串)

  simple_query_stringquery_string的另一种版本,其更适合为用户提供一个搜索框中,因为其使用+/|/- 分别替换AND/OR/NOT,如果用输入了错误的查询,其直接忽略这种情况而不是抛出异常。使用如下:

/////////////////////////////////////////////////////////////////////
 User: 过往记忆
 Date: 2016-09-04
 Time: 23:36
 bolg: https://www.iteblog.com
 本文地址:https://www.iteblog.com/archives/1730.html
 过往记忆博客,专注于hadoop、hive、spark、shark、flume的技术博客,大量的干货
 过往记忆博客微信公共帐号:iteblog_hadoop
/////////////////////////////////////////////////////////////////////

curl POST https://www.iteblog.com:9200/iteblog_book_index/book/_search
{
    "query": {
        "simple_query_string" : {
            "query": "(saerch~1 algorithm~1) + (grant ingersoll)  | (tom morton)",
            "fields": ["_all", "summary^2"]
        }
    },
    "_source": [ "title", "summary", "authors" ],
    "highlight": {
        "fields" : {
            "summary" : {}
        }
    }
}

Term/Terms Query

前面的例子中我们已经介绍了全文搜索(full-text search),但有时候我们对结构化搜索中能够精确匹配并返回搜索结果更感兴趣。这种情况下我们可以使用termterms查询。在下面例子中,我们想搜索所有曼宁出版社(Manning Publications)出版的图书:

curl POST https://www.iteblog.com:9200/iteblog_book_index/book/_search -d '
{
    "query": {
        "term" : {
            "publisher": "manning"
        }
    },
    "_source" : ["title","publish_date","publisher"]
}'

[返回结果]

{
    "took": 25, 
    "timed_out": false, 
    "_shards": {
        "total": 3, 
        "successful": 3, 
        "failed": 0
    }, 
    "hits": [
        {
            "_index": "bookdb_index", 
            "_type": "book", 
            "_id": "2", 
            "_score": 1.2231436, 
            "_source": {
                "publisher": "manning", 
                "title": "Taming Text: How to Find, Organize, and Manipulate It", 
                "publish_date": "2013-01-24"
            }
        }, 
        {
            "_index": "bookdb_index", 
            "_type": "book", 
            "_id": "3", 
            "_score": 1.2231436, 
            "_source": {
                "publisher": "manning", 
                "title": "Elasticsearch in Action", 
                "publish_date": "2015-12-03"
            }
        }, 
        {
            "_index": "bookdb_index", 
            "_type": "book", 
            "_id": "4", 
            "_score": 1.2231436, 
            "_source": {
                "publisher": "manning", 
                "title": "Solr in Action", 
                "publish_date": "2014-04-05"
            }
        }
    ]
}

我们还可以使用terms关键字来指定多个terms,如下:

{
    "query": {
        "terms" : {
            "publisher": ["oreilly", "packt"]
        }
    }
}

Term Query - Sorted

  词查询结果和其他查询结果一样可以很容易地对其进行排序,而且我们可以对输出结果按照多层进行排序:

curl POST https://www.iteblog.com:9200/iteblog_book_index/book/_search
{
    "query": {
        "term" : {
            "publisher": "manning"
        }
    },
    "_source" : ["title","publish_date","publisher"],
    "sort": [
        { "publish_date": {"order":"desc"}},
        { "title": { "order": "desc" }}
    ]
}

[返回结果]

{
    "took": 25, 
    "timed_out": false, 
    "_shards": {
        "total": 3, 
        "successful": 3, 
        "failed": 0
    }, 
    "hits": [
        {
            "_index": "bookdb_index", 
            "_type": "book", 
            "_id": "3", 
            "_score": null, 
            "_source": {
                "publisher": "manning", 
                "title": "Elasticsearch in Action", 
                "publish_date": "2015-12-03"
            }, 
            "sort": [
                1449100800000, 
                "in"
            ]
        }, 
        {
            "_index": "bookdb_index", 
            "_type": "book", 
            "_id": "4", 
            "_score": null, 
            "_source": {
                "publisher": "manning", 
                "title": "Solr in Action", 
                "publish_date": "2014-04-05"
            }, 
            "sort": [
                1396656000000, 
                "solr"
            ]
        }, 
        {
            "_index": "bookdb_index", 
            "_type": "book", 
            "_id": "2", 
            "_score": null, 
            "_source": {
                "publisher": "manning", 
                "title": "Taming Text: How to Find, Organize, and Manipulate It", 
                "publish_date": "2013-01-24"
            }, 
            "sort": [
                1358985600000, 
                "to"
            ]
        }
    ]
}

Range Query(范围查询)

  另一种结构化查询就是范围查询。在下面例子中,我们搜索所有发行年份为2015的图书:

curl POST https://www.iteblog.com:9200/iteblog_book_index/book/_search
{
    "query": {
        "range" : {
            "publish_date": {
                "gte": "2015-01-01",
                "lte": "2015-12-31"
            }
        }
    },
    "_source" : ["title","publish_date","publisher"]
}


[返回结果]

{
    "took": 25, 
    "timed_out": false, 
    "_shards": {
        "total": 2, 
        "successful": 2, 
        "failed": 0
    }, 
    "hits": [
        {
            "_index": "bookdb_index", 
            "_type": "book", 
            "_id": "1", 
            "_score": 1, 
            "_source": {
                "publisher": "oreilly", 
                "title": "Elasticsearch: The Definitive Guide", 
                "publish_date": "2015-02-07"
            }
        }, 
        {
            "_index": "bookdb_index", 
            "_type": "book", 
            "_id": "3", 
            "_score": 1, 
            "_source": {
                "publisher": "manning", 
                "title": "Elasticsearch in Action", 
                "publish_date": "2015-12-03"
            }
        }
    ]
}

范围查询可以应用于日期,数字以及字符类型的字段。

本博客文章除特别声明,全部都是原创!
原创文章版权归过往记忆大数据(过往记忆)所有,未经许可不得转载。
本文链接: 【23种非常有用的ElasticSearch查询例子(4)】(https://www.iteblog.com/archives/1730.html)
喜欢 (8)
分享 (0)
发表我的评论
取消评论

表情
本博客评论系统带有自动识别垃圾评论功能,请写一些有意义的评论,谢谢!