Objective
The previous article explains how to customize the right-click menu, which is the front-end content, the content of this article is one: query. Chat history query, in the previous blog about finding friends has mentioned Elasticsearch, today it is going to play again. For Elasticsearch not cold of classmate, this article can not see.
From Baidu:
Elasticsearch is a Lucene-based search server. It provides a distributed multi-user-capable full-text search engine, based on a restful web interface. Elasticsearch is a popular enterprise-class search engine developed in Java and published as an open source under the Apache license terms. Designed for cloud computing, it can achieve real-time search, stable, reliable, fast, easy to install and use. We build a website or application, and to add search functionality, what strikes us is that it is difficult to search for work. We want our search solution to be fast, we want to have a 0 configuration and a completely free search mode, we want to be able to simply use JSON indexed data via HTTP, we want our search server to always be available, we want to be able to start one and expand to hundreds of, we want to search in real time, We want simple multi-tenancy and we want to build a cloud-based solution. Elasticsearch is designed to solve all these problems and more. Demo Demos
Chat casually:
Image Type Filtering
File type filtering
Keyword Query
Time period query (slightly)
Practical Explanation
Layim has provided us with an interface to open the history page. However, the query history page requires its own layout. Since HTML+CSS was not my strong point, I took the structure of the chat room directly past. There's an AJAX request on the Chat history page, and there's data binding, which is not much of an introduction. The main thing is to talk about how the background data query and detail considerations. Mainly talked about the elasticsearch, for Elasticsearch not interested in small partners can skip this article.
First of all, if you want to query the history, save the data first. So in the previous Layimhub method of receiving messages, we have stored a copy of the data in the ES in the same way. (example code below)
// Save Message Task.run (() = { messagefactory.createinstance (chatmessagesavetype.searchengine). Send (message); });
Chatinfo Chatinfo; varMine =Message.mine;
Chat History Model Chatinfo=NewChatinfo {addtime=Message.addtime, Avatar=Mine.avatar, Content=Mine.content, nickname=Mine.username, QQ=Mine.id, TimeSpan=Message.addtime.ToTimestamp (), Roomid=Message.roomid, Isfile= Mine.content.IndexOf ("file (") >-1, Isimg= Mine.content.IndexOf ("img[") >-1 };
In ES, index is the meaning of the index, which is equivalent to the table in the database
BOOLresult=es. Index (Chatinfo); return NewSendmessageresult (result);
So, when our users chat, the data is saved in the ES, just as SQL Server does a search, want to use ES search, it also need to save a copy. We open the client to look at the data, if you also installed the ES and head plug-ins, then the browser input 127.0.0.1:9200/_plugin/head can see the data.
Now that the data is available, let's look at the query criteria. 1, keyword Query 2, type Query 3, time period query 4, chat room ID query (most basic, A and B chat can not query A and C history records)
The query relationship between them is an and condition, and if SQL is represented, select * from ChatHistory where roomid=1 and content like '% sin cents% ' and ...
Then we need to translate the SQL into ES syntax. The end result is this.
{ "Query": { "Filtered": { "Filter": { "and": [//and Relationship {"Query": { "Match": { "Roomid": "friend_14895_14894"//Filter by chat room ID } } }, { "Term": { "Isimg":false//is IMG } }, { "Range": {//range query"Addtime": {//query field is time type"GT": "2016-08-16t00:00:00"//GT is greater than a certain time LT is less than a certain time } } } ] } } }, "From": 0,//Paging"Size": 50, "Sort": {//Sort"Addtime": { "Order": "ASC" } }, "Highlight": {//Highlight"Fields": { "Content": {}//content highlighted} }}
The core query method is as follows: (. Net client with the plainelastic.net, he has to construct query statements are encapsulated, similar to ORM, but the syntax I used too lame, so only to spell "SQL" it)
PublicJsonresultmodel Searchhistorymsg (stringGroupId, DateTime? StartTime =NULLDatetime? Endtime =NULL,stringKeyword =NULL,BOOLIsfile =false,BOOLIsimg =false,intPageIndex =1,intPageSize = -) { stringSt = StartTime = =NULL?"": StartTime. Value.tostring ("YYYY-MM-DD"); stringET = Endtime = =NULL?"": Endtime. Value.tostring ("YYYY-MM-DD"); int from= (PageIndex-1) *pageSize; //a chat group query stringQuerygroup ="{\ "query\": {\ "match\": {\ "roomid\": \ "Friend_14895_14894\" }}}"; //keyword Query stringQuerykeyword ="{\ "query\": {\ "match_phrase\": {\ "content\": {\ "query\": \ ""+ keyword +"\ ", \" Slop\ ": 0} }}"; //whether picture query stringQueryimg ="{\ "term\": {\ "isimg\": true}}"; //whether to include file queries stringQueryfile ="{\ "term\": {\ "isfile\": true}}"; //greater than less than a certain time period query stringQuerytimerange ="{\ "range\": {\ "addtime\": {\ "gt\": \ ""+st+"\ ", \" lt\ ": \""+et+"\" }} }"; //greater than a certain time stringQuerytimerangegt ="{\ "range\": {\ "addtime\": {\ "gt\": \ ""+st+"\"}} }"; //less than a certain time stringQuerytimerangelt ="{\ "range\": {\ "addtime\": {\ "lt\": \ ""+ et +"\" }} }"; stringQueryand =Querygroup; if(StartTime! =NULL&&endtime!=NULL) {Queryand+=","+Querytimerange; } if(StartTime! =NULL) {Queryand+=","+Querytimerangegt; } if(Endtime! =NULL) {Queryand+=","+Querytimerangelt; } if(!string. IsNullOrEmpty (keyword)) {Queryand+=","+Querykeyword; } if(isfile) {Queryand+=","+Queryfile; } if(isimg) {Queryand+=","+queryimg; } //Final query Statement stringquery ="{\ "query\": {\ "filtered\": {\ "filter\": {\ "and\": ["+ Queryand +"]}}},\ "from\":"+ from+", \ "size\":"+ PageSize +", \ "sort\": {\ "addtime\": {\ "order\": \ "asc\"}},\ "highlight\": { \ "fields\": {\ "content\": {}}}}"; varresult =Eschat. Querybayconditions (query); returnJsonresulthelper.createjson (Result,true); }
Then, as long as the conditions are right, the result is naturally the result we want.
However, the search will also be a bug, for example, if you enter the keyword img or file, the following is the case, because the database is stored in the string of HTML, and then to the interface to do the corresponding processing, the situation is a bit of egg pain.
Some students will notice that the IMG next to the EM tag, in fact, this is the reason for highlighting the keyword. Like, above the demonstration, the two words are highlighted, view the source code, they each word will have the EM tag contains, that is because ES in the query process, you can use the Highlight function, he will match the keyword to you with special tags, of course, we can also customize the label, such as B,i, tag can be. Then give a style, such as my here Em {color:red} so the highlight function is out.
Summarize
This article is basically around Elasticsearch, because it belongs to some additional functions, so it is not too thin. search function with SQL or MySQL or some other database can be implemented, mainly docking Layim to note, the way the binding and the image file HTML conversion.
Next trailer :"Intermediate" ASP. SignalR with LayIM2.0 Easy to implement the small details of the chat room (eight) of the web chat room, have you noticed?
Want to learn the small partner, can follow my blog Oh, my Qq:645857874,email:[email protected]
GitHub:https://github.com/fanpan26/LayIM_NetClient/
ASP. NET SignalR with LayIM2.0 easy to implement the web chat room (vii) History of query (time, keywords, pictures, files), keywords highlighted.