elasticsearch中LessThen遇到的坑

開發中,使用到LessThen-小于某個參數的邏輯查詢,如下:

/// <summary>
/// 查詢狀態已發布(狀態小于2)的政策要聞分頁
/// </summary>
/// <param name="input"></param>
/// <returns></returns>    
public async Task<PagedDto<PageSearchOutput>> GetPageSearchResultAsync(PageSearchInput input){var elasticSearchManager = LazyServiceProvider.LazyGetRequiredService<IElasticSearchManager>();var searchRequest = new SearchRequest<PageIndex>(Nest.Indices.Parse(PageExtensionConsts.IndexName));searchRequest.Sort = new List<ISort> { new FieldSort { Field = Infer.Field<PageIndex>(d => d.CreationTime), Order = SortOrder.Descending } };var filterList = new List<QueryContainer>{new NumericRangeQuery(){Field = Infer.Field<PageIndex>(d => d.Status),LessThan = (byte)PageStatus.Revoked //小于2的查詢出來}};if (input.IsEmpty())searchRequest.Query = new BoolQuery{Filter = filterList};else{var shouldList = new List<QueryContainer>();if (!input.QueryText.IsNullOrWhiteSpace()){var titleMatchQuery = new MatchQuery{Field = Infer.Field<PageIndex>(d => d.Title),Query = input.QueryText};var contentMatchQuery = new MatchQuery{Field = Infer.Field<PageIndex>(d => d.Content),Query = input.QueryText};shouldList.Add(titleMatchQuery);shouldList.Add(contentMatchQuery);searchRequest.Sort = new List<ISort> { new FieldSort { Field = "_score", Order = SortOrder.Descending } };}if (!input.Category.IsNullOrWhiteSpace()){filterList.Add(new TermQuery { Field = Infer.Field<PageIndex>(d => d.Category), Value = input.Category });}if (input.Province.HasValue){filterList.Add(new TermQuery { Field = Infer.Field<PageIndex>(d => d.Province), Value = input.Province });}if (input.City.HasValue){filterList.Add(new TermQuery { Field = Infer.Field<PageIndex>(d => d.City), Value = input.City });}var query = new BoolQuery{Should = shouldList,Filter = filterList};if (shouldList.Any()){query.MinimumShouldMatch = 1;}searchRequest.Query = query;}var queryDslJson = elasticSearchManager.GetQueryDslJson(searchRequest);var esResponse = await elasticSearchManager.SearchAsync<PageIndex>(searchRequest,(input.PageIndex - 1) * input.PageSize,input.PageSize);var pageIndices = esResponse.Documents;var ids = pageIndices.Select(i => i.Id).ToList();var qtos = await _queryRepository.GetPagesHitsAsync(ids);var outputs = pageIndices.Select(i =>{var areaIds = new List<int>();var area = string.Empty;areaIds.Add(i.Province);if (i.City.HasValue)areaIds.Add(i.City.Value);if (areaIds.Any())area = _queryRepository.GetAreaAsync(areaIds).Result;return new PageSearchOutput{Id = i.Id,Title = i.Title,Content = i.Content,CategoryName = PageCategory.FromValue(i.Category).DisplayName,TitlePicture = i.TitlePicture,CreationTime = i.CreationTime.ToDateTimeStr(),Status = ((PageStatus)i.Status).GetDescription(),Area = area,Hits = qtos.FirstOrDefault(qto => qto.Id == i.Id)?.Hits};});return new PagedDto<PageSearchOutput>(esResponse.Total, outputs.ToList());}

但是通過

var queryDslJson = elasticSearchManager.GetQueryDslJson(searchRequest);

轉json得到的內容如下:LessThen后參數內容轉義成2.0了,

這是因為LessThen是double?類型的,但是es中保存的2,所以導致查詢出來的結果不是預期的,把狀態=2的數據也查詢出來了

{"query": {"bool": {"filter": [{"range": {"status": {"lt": 2.0}}}]}},"sort": [{"creationTime": {"order": "desc"}}]
}

所以但是修改成

/// <summary>
/// 查詢狀態已發布(狀態小于2)的政策要聞分頁
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public async Task<PagedDto<PageSearchOutput>> GetPageSearchResultAsync(PageSearchInput input)
{
??????? var elasticSearchManager = LazyServiceProvider.LazyGetRequiredService<IElasticSearchManager>();
??????? var searchRequest = new SearchRequest<PageIndex>(Nest.Indices.Parse(PageExtensionConsts.IndexName));
??????? searchRequest.Sort = new List<ISort> { new FieldSort { Field = Infer.Field<PageIndex>(d => d.CreationTime), Order = SortOrder.Descending } };

??????? var filterList = new List<QueryContainer>{
??????????? //new NumericRangeQuery(){
??????????? //??? Field = Infer.Field<PageIndex>(d => d.Status),
??????????? //??? LessThan = (byte)PageStatus.Revoked
??????????? //}
??????????? new TermQuery { Field = Infer.Field<PageIndex>(d => d.Status), Value = (byte)PageStatus.Published } //使用TermQuery 來實現查詢邏輯
??????? };

??????? double? tt = (byte)PageStatus.Revoked;

??????? if (input.IsEmpty())
??????????? searchRequest.Query = new BoolQuery
??????????? {
??????????????? Filter = filterList
??????????? };
??????? else
??????? {
??????????? var shouldList = new List<QueryContainer>();

??????????? if (!input.QueryText.IsNullOrWhiteSpace())
??????????? {
??????????????? var titleMatchQuery = new MatchQuery
??????????????? {
??????????????????? Field = Infer.Field<PageIndex>(d => d.Title),
??????????????????? Query = input.QueryText
??????????????? };
??????????????? var contentMatchQuery = new MatchQuery
??????????????? {
??????????????????? Field = Infer.Field<PageIndex>(d => d.Content),
??????????????????? Query = input.QueryText
??????????????? };
??????????????? shouldList.Add(titleMatchQuery);
??????????????? shouldList.Add(contentMatchQuery);
??????????????? searchRequest.Sort = new List<ISort> { new FieldSort { Field = "_score", Order = SortOrder.Descending } };
??????????? }

??????????? if (!input.Category.IsNullOrWhiteSpace())
??????????? {
??????????????? filterList.Add(new TermQuery { Field = Infer.Field<PageIndex>(d => d.Category), Value = input.Category });
??????????? }
??????????? if (input.Province.HasValue)
??????????? {
??????????????? filterList.Add(new TermQuery { Field = Infer.Field<PageIndex>(d => d.Province), Value = input.Province });
??????????? }
??????????? if (input.City.HasValue)
??????????? {
??????????????? filterList.Add(new TermQuery { Field = Infer.Field<PageIndex>(d => d.City), Value = input.City });
??????????? }
??????????? var query = new BoolQuery
??????????? {
??????????????? Should = shouldList,
??????????????? Filter = filterList
??????????? };
??????????? if (shouldList.Any())
??????????? {
??????????????? query.MinimumShouldMatch = 1;
??????????? }
??????????? searchRequest.Query = query;
??????? }
??????? var queryDslJson = elasticSearchManager.GetQueryDslJson(searchRequest);

??????? var esResponse = await elasticSearchManager.SearchAsync<PageIndex>(
??????????? searchRequest,
??????????? (input.PageIndex - 1) * input.PageSize,
??????????? input.PageSize);
??????? var pageIndices = esResponse.Documents;

??????? var ids = pageIndices.Select(i => i.Id).ToList();
??????? var qtos = await _queryRepository.GetPagesHitsAsync(ids);
??????? var outputs = pageIndices.Select(i =>
??????? {
??????????? var areaIds = new List<int>();
??????????? var area = string.Empty;
??????????? areaIds.Add(i.Province);
??????????? if (i.City.HasValue)
??????????????? areaIds.Add(i.City.Value);
??????????? if (areaIds.Any())
??????????????? area = _queryRepository.GetAreaAsync(areaIds).Result;
??????????? return new PageSearchOutput
??????????? {
??????????????? Id = i.Id,
??????????????? Title = i.Title,
??????????????? Content = i.Content,
??????????????? CategoryName = PageCategory.FromValue(i.Category).DisplayName,
??????????????? TitlePicture = i.TitlePicture,
??????????????? CreationTime = i.CreationTime.ToDateTimeStr(),
??????????????? Status = ((PageStatus)i.Status).GetDescription(),
??????????????? Area = area,
??????????????? Hits = qtos.FirstOrDefault(qto => qto.Id == i.Id)?.Hits
??????????? };
??????? });
??????? return new PagedDto<PageSearchOutput>(esResponse.Total, outputs.ToList());
??? }

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/207051.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/207051.shtml
英文地址,請注明出處:http://en.pswp.cn/news/207051.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

使用OkHttp上傳本地圖片及參數

下面以一個例子來講解在項目中如何使用OKHttp來對本地圖片做個最簡單的上傳功能&#xff0c;基本上無封裝&#xff0c;只需要簡單調用便可&#xff08;對于OKHttp的引入不再單獨做介紹&#xff09;。 1&#xff1a;構建上傳圖片附帶的參數&#xff08;params&#xff09; Map…

2.vue學習筆記(目錄結構+模板語法+屬性綁定)

1.目錄結構 1.vscode ——VSCode工具的配置文件夾 2.node_modules ——Vue項目的運行依賴文件夾 3.public ——資源文件夾&#xff08;瀏覽器圖標&#xff09; 4.src ——源碼文件夾 5..gitgnore ——git忽略文件 6.index.html ——如果html文件 7.package.json —…

基于SpringBoot+Vue學生成績管理系統前后端分離(源碼+數據庫)

一、項目簡介 本項目是一套基于SpringBootVue學生成績管理系統&#xff0c;主要針對計算機相關專業的正在做bishe的學生和需要項目實戰練習的Java學習者。 包含&#xff1a;項目源碼、數據庫腳本等&#xff0c;該項目可以直接作為bishe使用。 項目都經過嚴格調試&#xff0c;確…

ElasticSearch中的分析器是什么?

在Elasticsearch中&#xff0c;分析器&#xff08;Analyzer&#xff09;是一個用于文本分析的重要組件。它定義了如何將文本分解成單詞和子詞&#xff0c;這對于索引和搜索是非常重要的。 在Elasticsearch中&#xff0c;分析器定義了如何將文本轉換為可以被索引和搜索的形式。…

虛幻學習筆記10—C++函數與藍圖的通信

一、前言 除了上一章C變量與藍圖通信講的變量能與藍圖通信外&#xff0c;還有函數和枚舉也可以和藍圖通信。函數的關鍵字為”UFUNCTION“、枚舉的關鍵字為”UENUM“。 二、實現 2.1、BlueprintCallable藍圖中調用 該函數時帶執行的&#xff0c;帶入如下。編譯成功后在藍圖中輸…

macOS 獲取文件夾大小

macOS 獲取文件夾大小 獲取文件夾大小的擴展如下&#xff1a; extension URL {var fileSize: Int? { // in bytesdo {let val try self.resourceValues(forKeys: [.totalFileAllocatedSizeKey, .fileAllocatedSizeKey])return val.totalFileAllocatedSize ?? val.fileAll…

來自Sui的溫馨建議:保護您的Web3私鑰

當您安裝一個錢包并創建Sui賬戶時&#xff0c;錢包包含一個公鑰和一個私鑰。保護好私鑰的安全非常重要&#xff0c;從而可以保護您的Sui資產&#xff0c;包括錢包中的任何tokens。 公鑰加密技術是幾十年前開發的&#xff0c;是當今互聯網上大多數安全交易的基礎&#xff0c;包…

Navicat Premium 16 for Mac/Windows:高效的數據庫開發工具

Navicat Premium 16是一款功能強大的數據庫開發工具&#xff0c;為開發人員提供了全面的工具和功能&#xff0c;幫助他們更高效地進行數據庫開發和管理。不論是初學者還是專業開發人員&#xff0c;Navicat Premium 16都能滿足他們的需求&#xff0c;并提供直觀、易用的界面。 …

【深度學習】AlexNet網絡實現貓狗分類

【深度學習】AlexNet網絡實現貓狗分類 AlexNet簡介 AlexNet是一種卷積神經網絡&#xff08;Convolutional Neural Network&#xff0c;CNN&#xff09;模型&#xff0c;它在2012年的ImageNet圖像分類挑戰賽中取得了重大突破&#xff0c;引發了深度學習在計算機視覺領域的熱潮…

為“異常”努力是值得的

異常是OO語言處理錯誤的方式,在C中&#xff0c;鼓勵使用異常。侯捷再書中談起異常&#xff0c;“十年前撰寫“未將異常考慮在內的”函數是為一種美好實踐&#xff0c;而今我們致力于寫出“異常安全碼”。”可見異常安全的重要。 說起異常安全&#xff0c;首先就要是異常的出現…

Leetcode—213.打家劫舍II【中等】

2023每日刷題&#xff08;五十二&#xff09; Leetcode—213.打家劫舍II 算法思路 實現代碼 class Solution { public:// 左閉右開int rob1(vector<int>& nums, int start, int end) {int n nums.size();int f0 0, f1 0, new_f 0;for(int i start; i < end…

pytorch學習入門之 Variable(變量)

Variable(變量) autograd.Variable 是包的核心類. 它包裝了張量, 并且支持幾乎所有的操作. 一旦你完成了你的計算, 你就可以調用 .backward() 方法, 然后所有的梯度計算會自動進行. 你還可以通過 .data 屬性來訪問原始的張量, 而關于該 variable(變量)的梯度會被累計到 .…

初識 OpenCV

初識 OpenCV 簡介 OpenCV&#xff08;Open Source Computer Vision Library&#xff09;是一個涵蓋了數百種計算機視覺算法的開源算法庫。 OpenCV 具有模塊化結構&#xff0c;這意味著該軟件包包含多個共享或靜態庫。其中包含以下模塊&#xff1a; Core functionality (core…

機器學習硬件十年:性能變遷與趨勢

本文分析了機器學習硬件性能的最新趨勢&#xff0c;重點關注不同GPU和加速器的計算性能、內存、互連帶寬、性價比和能效等指標。這篇分析旨在提供關于ML硬件能力及其瓶頸的全面視圖。本文作者來自調研機構Epoch&#xff0c;致力于研究AI發展軌跡與治理的關鍵問題和趨勢。 &…

【送書活動四期】被GitHub 要求強制開啟 2FA 雙重身份驗證,我該怎么辦?

記得是因為fork了OpenZeppelin/openzeppelin-contracts的項目&#xff0c;之后就被GitHub 要求強制開啟 2FA 雙重身份驗證了&#xff0c;一拖再拖&#xff0c;再過幾天帳戶操作將受到限制了&#xff0c;只能去搞一下了 目錄 2FA是什么為什么要開啟 2FA 驗證GitHub 欲在整個平臺…

消息隊列 - RabbitMQ

消息隊列 - RabbitMQ 1. 初識 MQ1.1 同步調用1.2 異步調用1.3.技術選型 2. RabbitMQ2.1 安裝2.2 收發信息2.2.1 交換機(Exchange)2.2.2 隊列2.2.3 綁定關系2.2.4 發送消息 2.3 數據隔離 1. 初識 MQ 微服務一旦拆分&#xff0c;必然涉及到服務之間的相互調用&#xff0c;之前講…

MySQL六 | 存儲引擎

目錄 存儲引擎 存儲引擎特點 存儲引擎選擇 Innodb與MyISAM區別 存儲引擎 默認存儲引擎:InnoDB show engines;#展示當前數據庫支持的存儲引擎 存儲引擎特點 特點InnoDBMyISAMMemory存儲限制64TB有有事務安全支持--鎖機制行鎖表鎖表鎖Btree鎖支持支持 支持 Hash索引--支…

編譯 Android gradle-4.6-all.zip 報錯問題記錄

編譯 Android gradle-4.6-all.zip 報錯問題記錄 方法一&#xff1a;替換資源&#xff1a;方法二&#xff1a;修改源方法三&#xff1a;修改版本 編譯時候無法下載 gradle-4.6-all Downloading https://services.gradle.org/distributions/gradle-4.6-all.zip 方法一&#xf…

《一念關山》熱度破萬,愛奇藝古裝賽道出盡風頭

?劉詩詩重回古裝劇、新式武俠公路片、質感細膩的鏡頭美學......看點滿滿的《一念關山》頻頻登上熱搜&#xff0c;俘獲了大批觀眾的心。 開播首日熱度就刷新了愛奇藝2023年站內紀錄&#xff0c;《一念關山》作為2023年愛奇藝在古裝賽道的收官之作&#xff0c;口碑和熱度兼收。…

Linux內核-標準IO和系統IO的區別

概念 標準IO&#xff1a;指的是C語言實現的文件操作的函數 系統IO&#xff08;文件IO&#xff09;&#xff1a;指的是linux或windows或unix&#xff0c;實現文件操作的函數。 為什么要有兩種IO C語言要實現跨平臺&#xff0c;所以C語言在不同操作系統中實現文件操作方式是不一…