Use mongoDB for full-text search

Use Mongo to do full text search using its text type index implementation

You need to create the table first

db.createCollection("search_data")
Copy the code

Design three fields in this table

{ "_id": ObjectId("60e6a2c3ee1a3f20f6f21971"), "eventid": NumberLong("610527999155245056"), "event_content": "Daily patrol found residents parked vans indiscriminately on the road of the community." ", "search_text": "Daily patrol found residents parked vans indiscriminately on the residential road." } The space in the search_text field data after the event_content field is segmented by the Chinese word spliter, which is important for searchCopy the code

Create indexes

db.syzh_event.createIndex({search_text: "text"})
Copy the code

Insert the data, and then search the data using the index

Db. Syzh_event. Find ({$text: {$search: 'red grass'}}, {score: {$meta: 'textScore'}}), sort ({score: {$meta: "TextScore "}}) example of" red grass "will be divided into two words, and then search, one of the words will get 0.5 points, two words will get 1.0 points, if the number of words, according to the segmentation effect, and so onCopy the code

Java Chinese word divider

<dependency> <groupId>org.ansj</groupId> <artifactId>ansj_seg</artifactId> <version>5.1.6</version> </dependency> String  searchText = IndexAnalysis.parse(search).toStringWithOutNature(" "); IndexAnalysis is a method for indexing words. For other methods, you can search on your own, or go to Github to search its open source repository and check the source codeCopy the code

Java implements the search syntax

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency>Copy the code

Java code:

Entity class:

public class SearchEventPojo { private String id; private Long eventid; private String event_content; private String search_text; private Double score; Getters and setters}Copy the code

Interface:

@Autowired private MongoTemplate mongoTemplate; @PostMapping("seacherData") public Map seacherData(@RequestBody JSONObject req){ String search = req.getString("search"); Parse (search).toStringWithOutNature(" "); String searchText = indexAnalysis.parse (search).toStringWithOutNature(" "); TextQuery textQuery = new TextQuery(new TextCriteria().matching(searchText)); textQuery.includeScore(); Textquery.sortbyscore (); List<SearchEventPojo> syzh_eventList = mongoTemplate.find(textQuery, SearchEventPojo.class, "syzh_event"); if (ListUtils.isNotBlank(syzh_eventList)) { Map<String,Object> map = new LinkedHashMap<>(); map.put("count",syzh_eventList.size()); map.put("data",syzh_eventList); return map; } @PostMapping("insertTestData") public void insertTestData(@RequestBody JSONObject req){ SearchEventPojo searchEventPojo = JSONUtils.toBean(req, SearchEventPojo.class); Parse (SearchEventPojo.getevent_content ()).toStringWithOutNature(" "); / / the space here is very important, and depends on the space a mongo search in Chinese word segmentation, (some exaggerated) searchEventPojo. SetSearch_text (text); searchEventPojo.setEventid(SFlakeUtils.getUUID()); // mongotemplate. insert(searchEventPojo, "syzh_event"); // Handle exceptions}Copy the code