zoukankan      html  css  js  c++  java
  • Elasticsearch 搜索

    章节


    我们已经尝试了一些基本的搜索参数,让我们深入研究Query DSL。

    文档字段

    首先看看返回的文档字段。默认情况下,搜索结果中包含了完整的JSON文档(_source字段),如果不希望返回源文档全部内容,可以设置要返回的字段。

    下面的例子,返回_source中的两个字段account_numberbalance:

    API

    GET /bank/_search
    {
      "query": { "match_all": {} },
      "_source": ["account_number", "balance"]
    }
    

    CURL

    curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d'
    {
      "query": { "match_all": {} },
      "_source": ["account_number", "balance"]
    }
    '
    

    匹配查询

    前面已经介绍过,使用match_all查询匹配所有文档。下面介绍一个新的查询类型:match查询,可以对某个字段进行搜索。

    下面的例子,返回编号为20的帐户:

    API

    GET /bank/_search
    {
      "query": { "match": { "account_number": 20 } }
    }
    

    CURL

    curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d'
    {
      "query": { "match": { "account_number": 20 } }
    }
    '
    

    下面的例子,返回地址中包含“mill”的所有帐户:

    API

    GET /bank/_search
    {
      "query": { "match": { "address": "mill" } }
    }
    

    CURL

    curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d'
    {
      "query": { "match": { "address": "mill" } }
    }
    '
    

    下面的例子,返回地址中包含“mill”或“lane”的所有帐户:

    API

    GET /bank/_search
    {
      "query": { "match": { "address": "mill lane" } }
    }
    

    CURL

    curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d'
    {
      "query": { "match": { "address": "mill lane" } }
    }
    '
    

    下面的例子,是match的一个变体match_phrasematch_phrase匹配整个短语,它返回地址中包含短语“mill lane”的所有帐户:

    API

    GET /bank/_search
    {
      "query": { "match_phrase": { "address": "mill lane" } }
    }
    

    CURL

    curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d'
    {
      "query": { "match_phrase": { "address": "mill lane" } }
    }
    '
    
    

    布尔查询

    布尔查询使用布尔逻辑,将小查询组合成大查询。

    下面的例子,bool must子句下包含两个匹配查询,返回地址中包含“mill”且也包含“lane”的帐户:

    API

    GET /bank/_search
    {
      "query": {
        "bool": {
          "must": [
            { "match": { "address": "mill" } },
            { "match": { "address": "lane" } }
          ]
        }
      }
    }
    

    CURL

    curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d'
    {
      "query": {
        "bool": {
          "must": [
            { "match": { "address": "mill" } },
            { "match": { "address": "lane" } }
          ]
        }
      }
    }
    '
    
    

    在上面的示例中,bool must子句包含的所有匹配条件为真,文档才能被视为匹配,类似逻辑与。

    下面例子中,bool should子句下包含两个匹配查询,返回地址中包含“mill”或“lane”的帐户:

    API

    GET /bank/_search
    {
      "query": {
        "bool": {
          "should": [
            { "match": { "address": "mill" } },
            { "match": { "address": "lane" } }
          ]
        }
      }
    }
    

    CURL

    curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d'
    {
      "query": {
        "bool": {
          "should": [
            { "match": { "address": "mill" } },
            { "match": { "address": "lane" } }
          ]
        }
      }
    }
    '
    
    

    在上面的示例中,bool should子句包含的匹配条件有一个为真,文档将被视为匹配,类似逻辑或。

    下面例子中,bool must_not子句包含两个匹配查询,返回地址中既不包含“mill”也不包含“lane”的帐户:

    API

    GET /bank/_search
    {
      "query": {
        "bool": {
          "must_not": [
            { "match": { "address": "mill" } },
            { "match": { "address": "lane" } }
          ]
        }
      }
    }
    

    CURL

    curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d'
    {
      "query": {
        "bool": {
          "must_not": [
            { "match": { "address": "mill" } },
            { "match": { "address": "lane" } }
          ]
        }
      }
    }
    '
    
    

    在上面的示例中,bool must_not子句包含的匹配条件全部为假,文档将被视为匹配,类似逻辑与非。

    可以在布尔查询中同时组合mustshouldmust_not子句,可以在这些布尔子句中组合布尔查询,以模拟任何复杂的多级布尔逻辑。

    下面例子中,返回所有40岁,但不居住在ID(aho)的人的账户:

    API

    GET /bank/_search
    {
      "query": {
        "bool": {
          "must": [
            { "match": { "age": "40" } }
          ],
          "must_not": [
            { "match": { "state": "ID" } }
          ]
        }
      }
    }
    

    CURL

    curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d'
    {
      "query": {
        "bool": {
          "must": [
            { "match": { "age": "40" } }
          ],
          "must_not": [
            { "match": { "state": "ID" } }
          ]
        }
      }
    }
    '
    
  • 相关阅读:
    JS 反射机制及 Reflect 详解
    React Hooks
    深入理解 React setState
    React 函数组件和类组件的区别
    tsconfig.json 编译器配置大全
    React TS 解决不声明变量类型时的报错问题
    JSX onClick 和 HTML onclick 的区别
    深入理解 ES6 Generator
    JS 算法与数据结构之队列
    深入理解 ES6 Proxy
  • 原文地址:https://www.cnblogs.com/jinbuqi/p/11504379.html
Copyright © 2011-2022 走看看