㈠ 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 怎麼過濾分詞一個字
分詞一個字
㈢ 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 怎麼執行 and 條件的 in 查詢
{"query":{"bool":{"must":[{"term":{"sample":1}},{"term":{"name":2}}]}}}
㈤ elasticsearch 怎麼同時檢索單 field 多個值的條件
多詞條查詢:
多詞條查詢 允許匹配那些在內容中含有某些詞條的文檔。詞條查詢允許匹配單個未經分析的詞條,多詞條查詢可以用來匹配多個這樣的詞條。假設想得到所有在tags欄位中含有novel或book的文檔。運行以下查詢來達到目的:
{
"query" : {
"terms" : {
"tags" : [ "novel", "book" ],
"minimum_match" : 1
}
}
}
上述查詢返回在tags欄位中包含一個或兩個搜索詞條的所有文檔。為什麼?這是因為我們把minimum_match屬性設置為1;這意味著至少有1個詞條應該匹配。如果想要查詢匹配所有詞條的文檔,可以把minimum_match屬性設置為2
2. 使用bool查詢來合並多個term插敘。
可以通過布爾查詢來封裝無限數量的查詢,並通過下面描述的節點之一使用一個邏輯值來連接它們。
should:被它封裝的布爾查詢可能被匹配,也可能不被匹配。
被匹配的should節點數目由minimum_should_match參數控,此參數的值描述了文檔被視為匹配時,應該匹配的should子句的最少數量。舉例來說,它可以是個整數值,比如2,也可以是個百分比,比如75%。
must:被它封裝的布爾查詢必須被匹配,文檔才會返回。
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 多條件搜索語句怎麼寫
以下代碼是動態構建查詢語句:
[java] view plain private SearchRequestBuilder dynamicSearch(String index, String type, String startTime, String endTime, String status, String title, String city, String resOfficer, int pageIndex, int pageCapacity) { SearchRequestBuilder searchRequestBuilder = client.prepareSearch(index) .setTypes(type) .setSearchType(SearchType.DFS_QUERY_THEN_FETCH) .setPostFilter(FilterBuilders.rangeFilter("start_time").from(startTime).to(endTime)) .setFrom(pageIndex).setSize(pageCapacity) .setExplain(true); BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery(); queryBuilder.mustNot(QueryBuilders.matchQuery("site", "www.huodongxing.com")); //活動名稱 if (!StringUtils.isEmpty(title)) { queryBuilder.must(QueryBuilders.queryString(title).field("title")); } //城市 if (!StringUtils.isEmpty(city)) { queryBuilder.must(termQuery("city", city)); } //業務狀態 if (!StringUtils.isEmpty(status)) { if (status.equals(BusinessStatus.REJECTED.getCode()) || status.equals(BusinessStatus.COOPERATED.getCode())) { queryBuilder.must(termQuery("bus_status", status)); } } //負責人 if (!StringUtils.isEmpty(resOfficer)) { queryBuilder.must(queryString(resOfficer).field("resOfficer")); } if (StringUtils.isEmpty(title) && StringUtils.isEmpty(city) && StringUtils.isEmpty(status) && StringUtils.isEmpty(resOfficer)) { searchRequestBuilder.setQuery(matchAllQuery()); } searchRequestBuilder.setQuery(queryBuilder); return searchRequestBuilder; }
㈧ Elasticsearch 怎麼根據條件刪除數據
curl -XDELETE 'localhost:9200/customer/external/_query?pretty' -d '
{
"query": { "match": { "name": "John" } }
}
把這個輸入就可回以了答