ElasticSearch ---- 查詢

 

模擬數據建立正則表達式

首先利用head差檢查建立book索引緩存

而後修改mappings配置app

http方法:  postelasticsearch

連接地址:  http://192.168.253.129:9200/ book/novel/_mappingspost

{3d

  "novel": {orm

    "properties": {blog

      "word_count": {排序

        "type": "integer"索引

      },

      "author": {

        "type": "keyword"

      },

      "title": {

        "type": "text"

      },

      "publish_date": {

        "type": "date",

        "format": "yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || epoch_millis"

      }

    }

  }

}

數據插入

第一條數據

http方法: put

鏈接地址: http://192.168.253.129:9200/book/novel/1

請求參數

{

  "author": "張三",

  "title": "移魂大法",

  "word_count": 1000,

  "publish_date": "2000-10-01"

}

{

  "author": "李三",

  "title": "JAVA入門",

  "word_count": 2000,

  "publish_date": "2010-10-01"

}

{

  "author": "張四",

  "title": "Python入門",

  "word_count": 2000,

  "publish_date": "2005-10-01"

}

{

  "author": "李四",

  "title": "Elasticsearch大法好",

  "word_count": 1000,

  "publish_date": "2017-08-01"

}

{

  "author": "王五",

  "title": "菜譜",

  "word_count": 5000,

  "publish_date": "2002-10-01"

}

{

  "author": "趙六",

  "title": "劍譜",

  "word_count": 10000,

  "publish_date": "1997-01-01"

}

{

  "author": "張三丰",

  "title": "太極拳",

  "word_count": 1000,

  "publish_date": "1997-01-01"

}

{

  "author": "瓦力",

  "title": "Elasticsearch入門",

  "word_count": 3000,

  "publish_date": "2017-08-20"

}

{

  "author": "很胖的瓦力",

  "title": "Elasticsearch精通",

  "word_count": 3000,

  "publish_date": "2017-08-15"

}

{

  "author": "牛魔王",

  "title": "芭蕉扇",

  "word_count": 1000,

  "publish_date": "2000-10-01"

}

{

  "author": "孫悟空",

  "title": "七十二變",

  "word_count": 1000,

  "publish_date": "2000-10-01"

}

2.2.4.1 簡單查詢

http方法: GET

http地址: http://192.168.253.129:9200/book/novel/1

book: 索引名稱

novel: type名稱

1: 文檔id

2.2.4.2 條件查詢

http方法: POST

http地址: http://192.168.253.129:9200/book/novel/_search

查詢全部數據

{

    "query": {

        "match_all": {}

    }

}

Query: 爲查詢關鍵字

添加起始條數和記錄數

{

  "query": {

    "match_all": {}

  },

  "from": 1,

  "size": 1

}

 

關鍵詞查詢

{

  "query": {

    "match": {

      "title": "Elasticsearch"

    }

  },

  "sort": [

    {

      "publish_date": {

        "order": "desc"

      }

    }

  ]

}

Match中爲匹配的字段和值

Sort中爲排序字段

2.2.4.3 聚合查詢

關鍵詞:aggs

http方法: POST

http地址: http://192.168.253.129:9200/book/novel/_search

{

  "aggs": {

    "group_by_word_count": {

      "terms": {

        "field": "word_count"

      }

    },

    "group_by_publish_date": {

      "terms": {

        "field": "publish_date"

      }

    }

  }

}

 

對指定的字段進行計算

{

  "aggs": {

    "grades_word_count": {

      "stats": {

        "field": "word_count"

      }

    }

  }

}

2.2.5> 高級查詢

2.2.5.1子條件查詢-- Query context

特定字段查詢所指特定值

Query context

在查詢過程當中, 除了判斷文檔是否知足查詢條件以外, ES還會計算一個_score來標識匹配的程度, 旨在判斷目標文檔和查詢條件匹配的有多好.

Query context經常使用的查詢有全文本查詢字段級別的查詢

1. 全文本查詢: 針對文本類型數據
1) 模糊匹配

http方法: post

http地址: http://192.168.253.129:9200/book/novel/_search

關鍵詞: query(查詢關鍵詞), match(模糊匹配關鍵詞)

請求參數:

{

  "query": {

    "match": {

      "author": "瓦力"

    }

  }

}

或者

{

  "query": {

    "match": {

      "title": "elasticsearch入門"

    }

  }

}

模糊匹配會把查詢的字段進行拆分, 如title中的」elasticsearch入門」, 會查詢」elasticsearch」和」入門」兩個詞語所匹配的內容

2) 習語匹配

http方法: post

http地址: http://192.168.253.129:9200/book/novel/_search

關鍵詞: query(查詢關鍵詞), match_phrase(習語匹配關鍵詞)

請求參數:

{

  "query": {

    "match_phrase": {

      "title": "elasticsearch入門"

    }

  }

}

該查詢條件會將」title」字段中與」elasticsearch入門」相匹配的查出來

3) 多個字段的匹配查詢

http方法: post

http地址: http://192.168.253.129:9200/book/novel/_search

關鍵詞: query(查詢關鍵詞), muti_match(多字段匹配關鍵詞)

請求參數:

{

  "query": {

    "multi_match": {

      "query": "瓦力",

      "fields": [

        "author",

        "title"

      ]

    }

  }

}

該查詢條件會將」author」或者是」title」字段中包含」瓦力」的都查詢出來

 

 

4) 語法查詢(queryString)

支持通配符, 範圍查詢, 布爾查詢, 也能夠用在正則表達式中,

http方法: post

http地址: http://192.168.253.129:9200/book/novel/_search

關鍵詞: query(查詢關鍵詞), query_string(語法查詢關鍵詞)

請求參數

查詢」 elasticsearch」和」 大法」關鍵詞

{

  "query": {

    "query_string": {

      "query": "elasticsearch AND 大法"

    }

  }

}

關鍵詞OR的使用

{

  "query": {

    "query_string": {

      "query": "(elasticsearch AND 大法) OR Python"

    }

  }

}

指定字段和條件查詢

{

  "query": {

    "query_string": {

      "query": "瓦力 OR Elasticsearch",

      "fields": [

        "author",

        "title"

      ]

    }

  }

}

 

 

2. 字段級別的查詢: 針對結構化數據, 如數字, 日期等.

結構化數據的查詢

http方法: post

http地址: http://192.168.253.129:9200/book/novel/_search

關鍵詞: query(查詢關鍵詞), term(具體項關鍵詞)

查詢單詞數」word_count」爲1000的數據

{

  "query": {

    "term": {

      "word_count": 1000

    }

  }

}

查詢字數範圍在1000-2000之間的圖書

關鍵詞: range, gte大於等於, lte小於等於

{

  "query": {

    "range": {

      "word_count": {

        "gte": 1000,

        "lte": 2000

      }

    }

  }

}

查詢出版日期是今年初到如今的書籍(now關鍵字表明如今時間)

{

  "query": {

    "range": {

      "publish_date": {

        "gte": "2017-01-01",

        "lte": "now"

      }

    }

  }

}

2.2.5.2 子條件查詢--Filter context

http方法: post

http地址: http://192.168.253.129:9200/book/novel/_search

關鍵詞: query(查詢關鍵詞), bool, filter

{

  "query": {

    "bool": {

      "filter": {

        "term": {

          "word_count": 1000

        }

      }

    }

  }

}

Filter context相對於query context, 用於數據過濾, es會對其查詢結果緩存, 查詢速率會更快一些, filter要結合bool一塊兒使用.

 

2.2.5.3 複合條件查詢

以必定的邏輯組合子條件查詢

固定分數查詢

http方法: post

http地址: http://192.168.253.129:9200/_search (全文搜索)

關鍵詞: query(查詢關鍵詞), filter, boost關鍵詞, 固定分數

{

  "query": {

    "constant_score": {

      "filter": {

        "match": {

          "title": "elasticsearch"

        }

      },

      "boost": 2

    }

  }

}

固定了_score爲2. 而在模糊查詢中, _score爲變化的

 

布爾查詢

http方法: post

http地址: http://192.168.253.129:9200/_search (全文搜索)

關鍵詞: query(查詢關鍵詞), bool, should(應該知足), must(必須知足), must_not(必定不能知足的條件)

{

  "query": {

    "bool": {

      "should": [

        {

          "match": {

            "author": "瓦力"

          }

        },

        {

          "match": {

            "title": "elasticsearch"

          }

        }

      ]

    }

  }

}

 

{

  "query": {

    "bool": {

      "must": [

        {

          "match": {

            "author": "瓦力"

          }

        },

        {

          "match": {

            "title": "elasticsearch"

          }

        }

      ]

    }

  }

}

 

Bool和filter混合使用

{

  "query": {

    "bool": {

      "must": [

        {

          "match": {

            "author": "瓦力"

          }

        },

        {

          "match": {

            "title": "elasticsearch"

          }

        }

      ],

      "filter": [

        {

          "term": {

            "word_count": 3000

          }

        }

      ]

    }

  }

}

 

Must_not的使用

{

  "query": {

    "bool": {

      "must_not": {

        "term": {

          "author": "瓦力"

        }

      }

    }

  }

}

相關文章
相關標籤/搜索