es使用與原理1 -- 簡單啊的API及filter與query性能對比

復(fù)雜類型數(shù)據(jù)及Object類型數(shù)據(jù)

1、multivalue field
建立索引時與string是一樣的,數(shù)據(jù)類型不能混tag1 tag2 要么全是數(shù)字 要么全是字符串

{ "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 數(shù)據(jù)底層的存儲

{
    "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協(xié)議,一般不允許get請求帶上request body,但是因為get更加適合描述查詢數(shù)據(jù)的操作,因此還是這么用了。

Query DSL

1、Query DSL的基本語法

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

2、如何組合多個搜索條件
搜索需求:title必須包含elasticsearch,content可以包含elasticsearch也可以不包含,author_id必須不為111
源數(shù)據(jù)如下

{
  "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最小數(shù)量必須為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 加入數(shù)據(jù)

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,僅僅只是按照搜索條件過濾出需要的數(shù)據(jù)而已,不計算任何相關(guān)度分數(shù),對相關(guān)度沒有任何影響
query,會去計算每個document相對于搜索條件的相關(guān)度,并按照相關(guān)度進行排序

一般來說,如果你是在進行搜索,需要將最匹配搜索條件的數(shù)據(jù)先返回,那么用query;如果你只是要根據(jù)一些條件篩選出一部分數(shù)據(jù),不關(guān)注其排序,那么用filter
除非是你的這些搜索條件,你希望越符合這些搜索條件的document越排在前面返回,那么這些搜索條件要放在query中;如果你不希望一些搜索條件來影響你的document排序,那么就放在filter中即可

3、filter與query性能

filter,不需要計算相關(guān)度分數(shù),不需要按照相關(guān)度分數(shù)進行排序,同時還有內(nèi)置的自動cache最常使用filter的數(shù)據(jù)
query,相反,要計算相關(guān)度分數(shù),按照分數(shù)進行排序,而且無法cache結(jié)果

query搜索語法

1、match all 查詢所有數(shù)據(jù)

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" ] }}
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容