美文网首页
es使用与原理1 -- 简单啊的API及filter与query

es使用与原理1 -- 简单啊的API及filter与query

作者: Teemo_fca4 | 来源:发表于2020-03-20 15:52 被阅读0次
    复杂类型数据及Object类型数据

    1、multivalue field
    建立索引时与string是一样的,数据类型不能混tag1 tag2 要么全是数字 要么全是字符串

    { "tags": [ "tag1", "tag2" ]}
    

    2、empty field

    null,[],[null]
    

    3、object field

    PUT /company/employee/1
    {
      "address": {
        "country": "china",
        "province": "guangdong",
        "city": "guangzhou"
      },
      "name": "jack",
      "age": 27,
      "join_date": "2017-01-01"
    }
    

    上面的address就是 object类型,address Object 数据底层的存储

    {
        "name":            [jack],
        "age":          [27],
        "join_date":      [2017-01-01],
        "address.country":         [china],
        "address.province":   [guangdong],
        "address.city":  [guangzhou]
    }
    

    又比如
    {
    "authors": [
    { "age": 26, "name": "Jack White"},
    { "age": 55, "name": "Tom Jones"},
    { "age": 39, "name": "Kitty Smith"}
    ]
    }
    存储如下 ,底层是这样存储的

    {
        "authors.age":    [26, 55, 39],
        "authors.name":   [jack, white, tom, jones, kitty, smith]
    }
    
    search api的基本语法

    1、search api的基本语法

    GET /search
    {}

    GET /index1,index2/type1,type2/search
    {}

    GET /_search
    {
    "from": 0,
    "size": 10
    }
    HTTP协议,一般不允许get请求带上request body,但是因为get更加适合描述查询数据的操作,因此还是这么用了。

    Query DSL

    1、Query DSL的基本语法

    {
        QUERY_NAME: {
            ARGUMENT: VALUE,
            ARGUMENT: VALUE,...
        }
    }
    

    2、如何组合多个搜索条件
    搜索需求:title必须包含elasticsearch,content可以包含elasticsearch也可以不包含,author_id必须不为111
    源数据如下

    {
      "took": 1,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
      },
      "hits": {
        "total": 3,
        "max_score": 1,
        "hits": [
          {
            "_index": "website",
            "_type": "article",
            "_id": "2",
            "_score": 1,
            "_source": {
              "title": "my hadoop article",
              "content": "hadoop is very bad",
              "author_id": 111
            }
          },
          {
            "_index": "website",
            "_type": "article",
            "_id": "1",
            "_score": 1,
            "_source": {
              "title": "my elasticsearch article",
              "content": "es is very bad",
              "author_id": 110
            }
          },
          {
            "_index": "website",
            "_type": "article",
            "_id": "3",
            "_score": 1,
            "_source": {
              "title": "my elasticsearch article",
              "content": "es is very goods",
              "author_id": 111
            }
          }
        ]
      }
    }
    

    搜索语句,当需要组合搜索的时候 就加一个bool

    GET /website/article/_search
    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "title": "elasticsearch"
              }
            }
          ],
          "should": [
            {
              "match": {
                "content": "elasticsearch"
              }
            }
          ],
          "must_not": [
            {
              "match": {
                "author_id": 111
              }
            }
          ]
        }
      }
    }
    

    must 是必须,should里面的条件是满足和非满足都无所谓,minimum_should_match最小数量必须为1

    GET /test_index/_search
    {
        "query": {
                "bool": {
                    "must": { "match":   { "name": "tom" }},
                    "should": [
                        { "match":       { "hired": true }},
                        { "bool": {
                            "must":      { "match": { "personality": "good" }},
                            "must_not":  { "match": { "rude": true }}
                        }}
                    ],
                    "minimum_should_match": 1
                }
        }
    }
    
    filter与query对比

    1 加入数据

    PUT /company/employee/2
    {
      "address": {
        "country": "china",
        "province": "jiangsu",
        "city": "nanjing"
      },
      "name": "tom",
      "age": 30,
      "join_date": "2016-01-01"
    }
    
    PUT /company/employee/3
    {
      "address": {
        "country": "china",
        "province": "shanxi",
        "city": "xian"
      },
      "name": "marry",
      "age": 35,
      "join_date": "2015-01-01"
    }
    

    搜索请求:年龄必须大于等于30,同时join_date必须是2016-01-01

    GET /company/employee/_search
    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "join_date": "2016-01-01"
              }
            }
          ],
          "filter": {
            "range": {
              "age": {
                "gte": 30
              }
            }
          }
        }
      }
    }
    

    2、filter与query对比大解密

    filter,仅仅只是按照搜索条件过滤出需要的数据而已,不计算任何相关度分数,对相关度没有任何影响
    query,会去计算每个document相对于搜索条件的相关度,并按照相关度进行排序

    一般来说,如果你是在进行搜索,需要将最匹配搜索条件的数据先返回,那么用query;如果你只是要根据一些条件筛选出一部分数据,不关注其排序,那么用filter
    除非是你的这些搜索条件,你希望越符合这些搜索条件的document越排在前面返回,那么这些搜索条件要放在query中;如果你不希望一些搜索条件来影响你的document排序,那么就放在filter中即可

    3、filter与query性能

    filter,不需要计算相关度分数,不需要按照相关度分数进行排序,同时还有内置的自动cache最常使用filter的数据
    query,相反,要计算相关度分数,按照分数进行排序,而且无法cache结果

    query搜索语法

    1、match all 查询所有数据

    GET /_search
    {
        "query": {
            "match_all": {}
        }
    }
    
    

    2、match 必须匹配该条件

    GET /_search
    {
        "query": { "match": { "title": "my elasticsearch article" }}
    }
    

    3、multi match 多重匹配

    GET /test_index/test_type/_search
    {
      "query": {
        "multi_match": {
          "query": "test",
          "fields": ["test_field", "test_field1"]
        }
      }
    }
    

    4、range query 范围查找

    GET /company/employee/_search 
    {
      "query": {
        "range": {
          "age": {
            "gte": 30
          }
        }
      }
    }
    

    5、term query 注意:在term 里面搜索不会去分词查询,在match 里面搜索会去分词

    GET /test_index/test_type/_search 
    {
      "query": {
        "term": {
          "test_field": "test hello"
        }
      }
    }
    

    6、terms query

    GET /_search
    {
        "query": { "terms": { "tag": [ "search", "full_text", "nosql" ] }}
    }
    
    

    相关文章

      网友评论

          本文标题:es使用与原理1 -- 简单啊的API及filter与query

          本文链接:https://www.haomeiwen.com/subject/kvryfhtx.html