關(guān)鍵字 | 例子 | Elasticsearch查詢字符串 |
---|---|---|
And | findByNameAndPrice | { “query” : {“bool” : {“must” : [{ “query_string” : { “query” : “?”, “fields” : [ “name” ] } },{“query_string” : { “query” : “?”, “fields” : [ “price” ] } }]}}} |
Or | findByNameOrPrice | {“query”:{“bool”:{“should”:[{“query_string”:{“query”:“?”,“fields”:[“name”]}},{“query_string”:{“query”:“?”,“fields”:[“price”]}}]}}} |
Is | findByName | {“query”:{“bool”:{“must”:[{“query_string”:{“query”:“?”,“fields”:[“name”]}}]}}} |
Not | findByNameNot | {“query”:{“bool”:{“must_not”:[{“query_string”:{“query”:“?”,“fields”:[“name”]}}]}}} |
Between | findByPriceBetween | {“query”:{“bool”:{“must”:[{“range”:{“price”:{“from”:“?”,“to”:“?”,“include_lower”:true,“include_upper”:true}}}]}}} |
LessThan | findByPriceLessThan | {“query”:{“bool”:{“must”:[{“range”:{“price”:{“from”:null,“to”:“?”,“include_lower”:true,“include_upper”:false}}}]}}} |
LessThanEqual | findByPriceLessThanEqual | {“query”:{“bool”:{“must”:[{“range”:{“price”:{“from”:null,“to”:“?”,“include_lower”:true,“include_upper”:true}}}]}}} |
GreaterThan | findByPriceGreaterThan | {“query”:{“bool”:{“must”:[{“range”:{“price”:{“from”:“?”,“to”:null,“include_lower”:false,“include_upper”:true}}}]}}} |
GreaterThanEqual | findByPriceGreaterThanEqual | {“query”:{“bool”:{“must”:[{“range”:{“price”:{“from”:“?”,“to”:null,“include_lower”:true,“include_upper”:true}}}]}}} |
Before | findByPriceBefore | {“query”:{“bool”:{“must”:[{“range”:{“price”:{“from”:null,“to”:“?”,“include_lower”:true,“include_upper”:true}}}]}}} |
After | findByPriceAfter | {“query”:{“bool”:{“must”:[{“range”:{“price”:{“from”:“?”,“to”:null,“include_lower”:true,“include_upper”:true}}}]}}} |
Like | findByNameLike | {“query”:{“bool”:{“must”:[{“query_string”:{“query”:“?*”,“fields”:[“name”]},“analyze_wildcard”:true}]}}} |
StartingWith | findByNameStartingWith | {“query”:{“bool”:{“must”:[{“query_string”:{“query”:“?*”,“fields”:[“name”]},“analyze_wildcard”:true}]}}} |
EndingWith | findByNameEndingWith | {“query”:{“bool”:{“must”:[{“query_string”:{“query”:“*?”,“fields”:[“name”]},“analyze_wildcard”:true}]}}} |
Contains/Containing | findByNameContaining | {“query”:{“bool”:{“must”:[{“query_string”:{“query”:“?”,“fields”:[“name”]},“analyze_wildcard”:true}]}}} |
In (when annotated as FieldType.Keyword) | findByNameIn(Collection names) | {“query”:{“bool”:{“must”:[{“bool”:{“must”:[{“terms”:{“name”:[“?”,“?”]}}]}}]}}} |
In | findByNameIn(Collection names) | { “query”: {“bool”: {“must”: [{“query_string”:{“query”: “”?" “?”", “fields”: [“name”]}}]}}} |
NotIn (when annotated as FieldType.Keyword) | findByNameNotIn(Collection names) | {“query”:{“bool”:{“must”:[{“bool”:{“must_not”:[{“terms”:{“name”:[“?”,“?”]}}]}}]}}} |
NotIn | findByNameNotIn(Collectionnames) | {“query”: {“bool”: {“must”: [{“query_string”: {“query”: “NOT(”?" “?”)", “fields”: [“name”]}}]}}} |
True | findByAvailableTrue | {“query”:{“bool”:{“must”:[{“query_string”:{“query”:“true”,“fields”:[“available”]}}]}}} |
False | findByAvailableFalse | {“query”:{“bool”:{“must”:[{“query_string”:{“query”:“false”,“fields”:[“available”]}}]}}} |
OrderBy | findByAvailableTrueOrderByNameDesc | {“query”:{“bool”:{“must”:[{“query_string”:{“query”:“true”,“fields”:[“available”]}}]}},“sort”:[{“name”:{“order”:“desc”}}]} |
Exists | findByNameExists | {“query”:{“bool”:{“must”:[{“exists”:{“field”:“name”}}]}}} |
IsNull | findByNameIsNull | {“query”:{“bool”:{“must_not”:[{“exists”:{“field”:“name”}}]}}} |
IsNotNull | findByNameIsNotNull | {“query”:{“bool”:{“must”:[{“exists”:{“field”:“name”}}]}}} |
IsEmpty | findByNameIsEmpty | {“query”:{“bool”:{“must”:[{“bool”:{“must”:[{“exists”:{“field”:“name”}}],“must_not”:[{“wildcard”:{“name”:{“wildcard”:“*”}}}]}}]}}} |
IsNotEmpty | findByNameIsNotEmpty | {“query”:{“bool”:{“must”:[{“wildcard”:{“name”:{“wildcard”:“*”}}}]}}} |
不支持使用GeoJson參數(shù)構(gòu)建Geo-shape查詢的方法名。如果您需要在存儲(chǔ)庫(kù)中擁有這樣的函數(shù),請(qǐng)?jiān)谧远x存儲(chǔ)庫(kù)實(shí)現(xiàn)中使用ElasticsearchOperations和CriteriaQuery。
三、方法返回類型
存儲(chǔ)庫(kù)方法可以定義為具有以下返回類型以返回多個(gè)元素:
- List
- Stream
- SearchHits
- List<SearchHit>
- Stream<SearchHit>
- SearchPage
四、使用@Query注解
例2:使用@Query注解在方法上聲明query
傳遞給該方法的參數(shù)可以插入到查詢字符串中的占位符中。第一個(gè)、第二個(gè)、第三個(gè)參數(shù)的占位符形式為?0、?1、?2等。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-851356.html
interface BookRepository extends ElasticsearchRepository<Book, String> {
@Query("{\"match\": {\"name\": {\"query\": \"?0\"}}}")
Page<Book> findByName(String name,Pageable pageable);
}
設(shè)置為注解參數(shù)的String必須是一個(gè)有效的Elasticsearch JSON查詢。它將作為查詢?cè)氐闹蛋l(fā)送給Elasticsearch;例如,如果使用參數(shù)John調(diào)用函數(shù),它將產(chǎn)生以下查詢體:文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-851356.html
{
"query": {
**自我介紹一下,小編13年上海交大畢業(yè),曾經(jīng)在小公司待過(guò),也去過(guò)華為、OPPO等大廠,18年進(jìn)入阿里一直到現(xiàn)在。**
**深知大多數(shù)Linux運(yùn)維工程師,想要提升技能,往往是自己摸索成長(zhǎng)或者是報(bào)班學(xué)習(xí),但對(duì)于培訓(xùn)機(jī)構(gòu)動(dòng)則幾千的學(xué)費(fèi),著實(shí)壓力不小。自己不成體系的自學(xué)效果低效又漫長(zhǎng),而且極易碰到天花板技術(shù)停滯不前!**
**因此收集整理了一份《2024年Linux運(yùn)維全套學(xué)習(xí)資料》,初衷也很簡(jiǎn)單,就是希望能夠幫助到想自學(xué)提升又不知道該從何學(xué)起的朋友,同時(shí)減輕大家的負(fù)擔(dān)。**
**既有適合小白學(xué)習(xí)的零基礎(chǔ)資料,也有適合3年以上經(jīng)驗(yàn)的小伙伴深入學(xué)習(xí)提升的進(jìn)階課程,基本涵蓋了95%以上Linux運(yùn)維知識(shí)點(diǎn),真正體系化!**
**由于文件比較大,這里只是將部分目錄大綱截圖出來(lái),每個(gè)節(jié)點(diǎn)里面都包含大廠面經(jīng)、學(xué)習(xí)筆記、源碼講義、實(shí)戰(zhàn)項(xiàng)目、講解視頻,并且后續(xù)會(huì)持續(xù)更新**
**如果你覺得這些內(nèi)容對(duì)你有幫助,可以添加VX:vip1024b (備注Linux運(yùn)維獲取)**
*如果你覺得這些內(nèi)容對(duì)你有幫助,可以添加VX:vip1024b (備注Linux運(yùn)維獲?。?*
[外鏈圖片轉(zhuǎn)存中...(img-XYWZMUBf-1712547368448)]
到了這里,關(guān)于Spring Data訪問(wèn)Elasticsearch----查詢方法,程序員必學(xué)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!