elasticsearch 第二講

elasticsearch 的一些詳細用法

term 的用法

term 是不進行 分詞處理的;
term 不進行大小寫轉換, 內容默認分詞處理的時候爲小寫
term 不進行算分的計算算法

初始化數據
POST products/_bulk
{"index":{"_id": 1}}
{"productId":"XHDK-A-1293-#fj3", "desc": "iPhone"}
{"index":{"_id":2}}
{"productId":"KDKE-8-9947-kL5", "desc": "iPad"}
{"index":{"_id":"3"}}
{"productId":"JODL-X-1937-#pV7", "desc": "MBP"}
查詢數據
POST products/_search
{
    "query":{
        "term":{
            //不能查詢到數據
            //"productId": "XHDK"
            //能夠查詢到數據
            //"productId": "xhdk"
            // 不能查詢到數據 緣由就是term不進行分詞處理
            //"XHDK-A-1293-#fj3"
            // 能夠經過 keyword 方式進行搜索
            //"productId.keyword": "XHDK-A-1293-#fj3"
        }
    }
}

//查看 XHDK-A-1293-#fj3 如何進行分詞也會知道上面查詢不到的緣由

POST _analyze
{
  "analyzer": "standard"
  , "text": ["XHDK-A-1293-#fj3"]
}
不進行算分的計算
POST products/_search
{
    "query":{
        "constant_score":{
            "filter":{
                "term": {
                  "productId.keyword": {
                    "value": "XHDK-A-1293-#fj3"
                  }
                }
            }
        }
    }
}
Query & Filtering 多字符字段查詢
POST products/_bulk
{"index": {"_id": "1"}}
{"price": "10", "avaliable": true, "date": "2018-01-01", "productID": "XHDK-A-1293-#fJ3"}
{"index": {"_id":"2"}}
{"price":"20","avaliable":true,"date": "2019-01-01","productID": "KDKE-B-9947-#kL5"}
{"index": {"_id": "3"}}
{"price": "30", "avaliable": false, "productID": "JODL-X-1937-#pV7"}
{"index": {"_id": "4"}}
{"price": "30", "avaliable": false, "productID": "QQPX-R-3956-#aD8"}
{"index": {"_id": "5"}}
{"price": "40", "avaliable": false, "genre":"Comedy"}
{"index":{"_id":6}}
{"price":"50","genre":["Comedy","Romance"]}

範圍查找 查找 price 大於20 小於等於30的數據;同上能夠添加不進行分數計算 用 constant_scoreapp

POST products/_search
{
	"query": {
		"range":{
			"price":{
				"gt": 20,
				"lte": 30
			}
		}
	}
}

日期範圍的查找 同 上price的方式;dom

日期計算的方式 y 年 M月 d日 h時 m分 s秒 w周 獲取 now -1y 表示獲取的是 去年今天的日期elasticsearch

不進行分數計算 提升效率 參考 constant_score 的用法;post

POST products/_search
{
	"query":{
		"range":{
			"date": {
				"gt": "now-1y"
			}
		}
	}
}

查看存在某一個字段的數據 existsui

不進行分數計算 提升效率 參考 constant_score 的用法;code

POST products/_search
{
	"query":{
		"exists":{
			"field": "date"
		}
	}
}

對於多字段 genre 表示的是包含,而不是獲取完整的值;blog

例以下面的語句,返回的就是兩條數據排序

若是想返回一條數據,能夠添加字段 genre_count 字段,表示對應的長度 ,添加冗餘字段ip

POST products/_search
{
	"query":{
		"term":{
			"genre.keyword": "Comedy"
		}
	}
}

添加genre_count 字段語句爲

{"index": {"_id": "5"}}
{"price": "40", "avaliable": false, "genre":"Comedy", "genre_count": 1}
{"index":{"_id":6}}
{"price":"50","genre":["Comedy","Romance"], "genre_count": 2}

查詢符合條件的數據

POST product/_search
{
	"query": {
		"bool":{
			"must":[
				{
          "term":{
            "genre.keyword": {
              "value": "Comedy"
            }
          }
        },
        {
          "term":{
            "genre_count": {
              "value": "1"
            }
          }
        }
			]
		}
	}
}
es 算法分爲 TF-IDF 算法,表示的是 TF(文字) IDF(搜索文字所在的文檔數據/總文檔數)
es 新算法爲 BM25

should 和must 同一級別混合使用的時候,should不會生效,可是能夠嵌套使用

should 表示 或者 must 表示必定匹配 filter表示的是 必定匹配(可是不會計算分數)must_not 表示的是 必定不匹配(不會計算分數)

查詢 價格爲 30 不能夠訪問 同時價格爲大於等於10的數據
POST product/_search
{
	"query": {
		"bool": {
			"must": [
				{"term": {
					"price": {
						"value": "30"
					}
				}}
			],
			"filter": [
        {
        	"term": {
          "avaliable": "false"
         }
        }
			],
			"must_not": [
        {"range": {
          "price": {
            "lte": 10
          }
        }}
      ]
		}
	}
}

更改計算分數的方式,

boost 權重值 boost > 1 對打分提升, 1 > boost > 0 打分的權重相對低,當 boost < 0 貢獻爲負分

當前爲不一樣的字段設置的權重值

POST blogs/_bulk
{"index": {"_id":"1"}}
{"title": "Apple iPad", "content": "Apple iPad,Apple iPad"}
{"index": {"_id":"2"}}
{"title": "Apple iPad,Apple iPad", "content": "Apple iPad"}

//match 支持 query和bost的使用,bost表示的是當前的權重值
POST blogs/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "title": {
              "query": "apple,ipad",
              "boost": 1
            }
          }
        },
        {
          "match": {
            "content": {
              "query": "apple,ipad",
              "boost": 4
            }
          }
        }
      ]
    }
  }
}

同一個字段設置權重值

這裏設置的是 nagative_boosting 的權重值,若是 > 1 表示排前 若是爲 1 > a > 0 表示爲 排後,若是爲負數報錯

POST news/_bulk
{"index":{"_id": "1"}}
{"conent": "Apple mac"}
{"index":{"_id": "2"}}
{"conent": "Apple iPad"}
{"index":{"_id": "3"}}
{"conent": "Apple employee like Apple Pipe and Apple juice"}

POST news/_search
{
	"query":{
		"boosting": {
			"positive":{
				"bool": {
					"must": [
						{
							"match": {
								"content": "apple"
							}
						}
					]
				}
			},
			"negative": {
				"match": {
					"content": "pip"
				}
			},
			"negative_boosting": 0.1
		}
	}
}
dis_max 的用法;也是對排序上的一個用法

一、以下兩條數據,若是使用普通查詢 title 和 body 包含 brown fox語句的查詢,則1 數據在前 2數據在後,緣由是 排序的規則是全部符合 字段的累加(例如:eg1), 1數據title和body中 都包含brown 1數據 只有body中包含 brown fox,對於這種想讓2數據排序靠前,則須要使用dis_max

二、使用dis_max 表示的是字段中 按照排分最高的字段進行排序 (例如:eg2)

三、使用dis_max 表示的是字段中 按照最高分字段進行排序的,例如 Quick pets 則 兩個數據中都包含(例如:eg3),按照最高字段評分比對,則 當前按照的是 title字段進行比對的(能夠經過explain: true查看),則兩個數據的評分同樣,第一條匹配的是 quick 第二條匹配的是 pets,因此能夠經過添加字段 tie_breaker

四、使用tie_breaker 表示的是 獲取最佳匹配語句評分的_score,其餘語句的評分與 tie_breaker 相乘得數的分,二者評分求和並規範化;得出的結果 (例如:eg4)

PUT blogs/_doc/1
{
  "title": "Quick brown rabbits",
  "body": "Brown rabbits are commonly seen."
}
PUT blogs/_doc/2
{
  "title": "Keeping pets healthy",
  "body": "My quick brown fox eats rabbits on a regular basis"
}
eg1
POST blogs/_search
{
	"query": {
		"bool": {
			"must": [
				{"match": {"title":" brown fox"}},
				{"match": {"body":" brown fox"}}
			]
		}
	}
}
eg2
POST blogs/_search
{
	"query": {
		"dis_max": {
			"queries": [
				{"match": {"title":" quick fox"}},
				{"match": {"body":" quick fox"}}
			]
		}
	}
}
eg3
POST blogs/_search
{
  "query": {
    "dis_max": {
      "queries": [
        {"match": { "title": "Quick pets"}},
        {"match": { "body": "Quick pets" }}
        ]
    }
  }
}
eg4
POST blogs/_search
{
  "query": {
    "dis_max": {
      "queries": [
        {"match": {"title": "Quick pets"}},
        {"match": {"body": "Quick pets"}}
        ]
        , "tie_breaker": 0.7
    }
  }
}
最佳字段 多數字段 混合字段三種場景

優先設置 title的mapping,保證字段使用的是 english 分詞器,同時設置它的 title.std 分詞器爲 standard

PUT title
{
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        "analyzer": "english",
        "fields": {
          "std": {
            "type": "text",
            "analyzer": "standard"
          }
        }
      }
    }
  }
}

POST title/_bulk
{"index": {"_id": 1}}
{"title": "My dog barks"}
{"index": {"_id": 2}}
{"title": "I see a lot of barking dogs on the road"}

best_field 表示的是最佳字段匹配 默認

當前 順序爲 1 2,由於 使用的是 english分詞器,分詞的結果都是原生的詞,能夠經過 analayzer 查看分詞結果;他們匹配的內容都爲 dog 和 bark ,當前最短則分數最高;

POST title/_search
{
	"query": {
		"multi_match": {
			"query": "barking dogs",
			"fields": ["title"],
			"type": "best_fields"
		}
	}
}

most_field 表示的是 多個字段分數的累加,使用 operator ,它表示的是單個 字段中同時包含query信息

當前是從 title.std 和 title中查找符合 query的數據,由於std爲 standard 的分詞方式,則保留最原始的詞內容,因此這個順序查找的是 2 1;

POST title/_search
{
  "query": {
    "multi_match": {
      "query": "barking dogs",
      "fields": ["title.std", "title"],
      "type": "most_fields"
    }
  }
}

cross_field 查找的是多個字段都總和包含對應的query信息,而不是僅僅單個字段中包含信息,表示的是 多個字段分數累加,可是能夠添加operator,表示在全部字段中都包含query信息,至關月copy to,把全部字段的值放到一塊兒,而後從中查找是否符合,可是比copy to節省空間

PUT address/_doc/1
{
  "street": "5 Poland Street",
  "city": "London",
  "country": "United Kingdom",
  "postcode": "W1V 3DG"
}


POST address/_search
{
  "query": {
    "multi_match": {
      "query": "Poland Street W1V",
      "operator": "and", 
      "fields": ["street", "city", "country", "postcode"]
      , "type": "cross_fields"
    }
  }
}
相關文章
相關標籤/搜索