⑴ elasticsearch 怎麼同時檢索單 field 多個值的條件
1、 多詞條查詢:
多詞條查詢 允許匹配那些在內容中含有某些詞條的文檔。詞條查詢允許匹配單個未經分析的詞條,多詞條查詢可以用來匹配多個這樣的詞條。假設想得到所有在tags欄位中含有novel或book的文檔。運行以下查詢來達到目的:
{
"query":{
"terms":{
"tags":["novel","book"],
"minimum_match":1
}
}
}
把minimum_match屬性設置為1;這意味著至少有1個詞條應該匹配。如果想要查詢匹配所有詞條的文檔,可以把minimum_match屬性設置為2
2、使用bool查詢來合並多個term插敘。
可以通過布爾查詢來封裝無限數量的查詢,並通過下面描述的節點之一使用一個邏輯值來連接它們。
should:被它封裝的布爾查詢可能被匹配,也可能不被匹配。
被匹配的should節點數由minimum_should_match參數控,此參數的值描述了文檔被視為匹配時,應該匹配的should子句的最少數量。舉例來說,它可以是個整數值,比如2,也可以是個百分比,比如75%。
3、must:被它封裝的布爾查詢必須被匹配,文檔才會返回。
4、must_not:被它封裝的布爾查詢必須不被匹配,文檔才會返回。
{
"query":{
"bool":{
"should":{
"term":{
"title":"aa"
}
},
"should":{
"term":{
"title":"bb"
}
},
"should":{
"term":{
"title":"cc"
}
},
minimum-should-match=1
}
}
}
⑵ elasticsearch query 和 filter 的區別
如下例子,查找性別是女,所在的州是PA,過濾條件是年齡是39歲,回balance大於等於10000的文答檔: { "query": { "bool": { "must": [ { "match": { "gender": "F" } }, { "match": { "state": "PA" } } ], "filter": [ { "term": { "age": "39" } }...
⑶ elasticsearch 怎麼實現模糊匹配
QueryDSL如下:
{
"query": {
"bool": {
"must": [
{
"term": {
"category_id": "7"
}
},
{
"term": {
"enable": 1
}
},
{
"term": {
"status": 2
}
},
{
"range": {
"stock": {
"gt": 0
}
}
},
{
"match": {
"search_field": {
⑷ Elasticsearch 怎麼讓模糊搜索和其他條件同時滿足
{
"query": {
"bool": {
"must": [
{
"term": {
"allocation": "0"
}
},
{
"range": {
"order_id": {
"from": "0",
"to": "9999999999"
}
}
},
{
"match": {
"content" : {
"query" : "乘客離開",
"analyzer" : "ik"
}
}
}
],
"must_not": [],
"should": []
}
},
"from": 0,
"size": 10,
"sort": [],
"facets": {}
}
⑸ ElasticSearch中Filter和Query的異同
如下例子,查找性別是女,所在的州是PA,過濾條件是年齡是39歲,balance大於等於10000的文檔:
{
"query":
{
"bool":
{
"must":
[
{
"match":
{
"gender":
"F"
}
},
{
"match":
{
"state":
"PA"
}
}
],
"filter":
[
{
"term":
{
"age":
"39"
}
},
{
"range":
{
"balance":
{
"gte":
"10000"
}
}
}
]
}
}
}
返回結果:
查詢雖然包含這兩種,但是查詢在不同的執行環境下,操作還是不一樣的。
Query與Filter
查詢在Query查詢上下文和Filter過濾器上下文中,執行的操作是不一樣的:
Query查詢上下文:
在查詢上下文中,查詢會回答這個問題——「這個文檔匹不匹配這個查詢,它的相關度高么?」
如何驗證匹配很好理解,如何計算相關度呢?之前說過,ES中索引的數據都會存儲一個_score分值,分值越高就代表越匹配。另外關於某個搜索的分值計算還是很復雜的,因此也需要一定的時間。
查詢上下文
是在
使用query進行查詢時的執行環境,比如使用search的時候。
Filter過濾器上下文:
在過濾器上下文中,查詢會回答這個問題——「這個文檔匹不匹配?」
答案很簡單,是或者不是。它不會去計算任何分值,也不會關心返回的排序問題,因此效率會高一點。
過濾上下文
是在使用filter參數時候的執行環境,比如在bool查詢中使用Must_not或者filter。
另外,經常使用過濾器,ES會自動的緩存過濾器的內容,這對於查詢來說,會提高很多性能。
總結
1
查詢上下文中,查詢操作不僅僅會進行查詢,還會計算分值,用於確定相關度;在過濾器上下文中,查詢操作僅判斷是否滿足查詢條件
2
過濾器上下文中,查詢的結果可以被緩存。
⑹ elasticsearch怎麼實現分組排序
thisObj.className = "active"; document.getElementById(tabObj+"_Content"+i).style.display = "block"; }else{ tabList[i].className = "normal"; document.getElementById(tabObj+"_Content"+i).style.display = "none"; }
⑺ elasticsearch 怎麼過濾分詞一個字
分詞一個字
⑻ 如何讓elasticsearch欄位不分池
該屬性的取值可以為yes和no,用於指定欄位的原始屬性是否存入索引。默認值是no.意味著不能版在結果中返回欄位的原權始值(即使沒有存儲原始值,也可以使用Soure欄位返回原始值)。如果已經建立索引可以搜索該欄位的內容。
⑼ ElasticSearch DSL 分組查詢後再次分組問題
SearchRequestBuilder對象設置index、type、分頁、高亮等等後,列印這個對象即可
⑽ 請教elasticsearch自定義結果集過濾如何支持
用groovy腳本自定義ElasticSearch查詢,來實現以上功能。 例,數據中包含欄位birdtyday,記錄遊客生日: "birthday": "1992-02-05 00:00:00", 新建文件getAgeByBirthday.groovy,編輯其內容為: def b = doc[birthday_field].value def birthday = new Date(b) def now = new Date() long age = (now -birthday)/365 age 並把此文件放在es的config/scripts目錄下(如果沒有此目錄就新建一個)。 然後在config/elasticsearch.yml文件中加一行: script.groovy.sandbox.enabled: true 最後重啟es即可。 接下來,我們就可以用以下DSL進行年齡統計了 GET /lovingtrip-report/hotelcustomer/_search?search_type=count { "aggs": { "counts_by_age": { "terms": { "script_file": "getAgeByBirthday", "params": { "birthday_field": "birthday" }, "size": 100 } } } } 或者: GET /lovingtrip-report/hotelcustomer/_search?search_type=count { "aggs": { "histogram_by_age": { "histogram": { "script_file": "getAgeByBirdthday", "params": { "birdthday_field": "birdthday" }, "interval": 5 } } } } 不過腳本查詢性能不佳,且不能利用es的緩存,所以在大數據量或高性能要求的場景下不適用。。 ------------------------------------- 補充一個自定義的年齡range過濾: range_AgeByBirthday.groovy: def b = doc[birdthday_field].value def birdthday = new Date(b) def now = new Date() long age = (now -birdthday)/365 gte<=age && age<=lte DSL: GET /lovingtrip-report/hotelcustomer/_search?search_type=count { "query": { "filtered": { "filter": { "script": { "script_file": "range_AgeByBirdthday", "params": { "birdthday_field": "birdthday", "gte": 50, "lte": 60 } } } } }, "aggs": { "histogram_by_age": { "histogram": { "script_file": "getAgeByBirdthday", "params": { "birdthday_field": "birdthday" }, "interval": 5 } } } }