mongodb全文搜索

mongodb 的 enterprise 集合存储企业信息:

{
    "_id" : ObjectId("5d62b2a4380d051cfc00565b"),
    "companyName" : "浙江《交通旅游导报》报业有限公司",
    "companyCode" : "913300007719260474",
    "provice" : "",
    "city" : "",
    "adress" : "",
    "zipcode" : "",
    "companyEmail" : "",
    "contactPhone" : ",",
    "source" : 1,
    "date" : ISODate("2019-08-25T16:09:05.177Z"),
    "_class" : "com.third.demo.entity.NewEnterprise"
}

集合的javaBean:

import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.index.TextIndexed;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;

import java.io.Serializable;


@Data
@Document("enterprise")
public class GxyEnterpriseDto  implements Serializable {

    @Field("_id")
    private String practiceCompanyId;

    /**
     * 企业名称
     */
    @TextIndexed
    private String companyName;
    /**
     * 企业代码
     */
    @TextIndexed
    private String companyCode;
    /**
     * 省份
     */
    private String province;
    /**
     * 城市
     */
    private String city;
    /**
     * 区域
     */
    private String area;
    /**
     * 企业地址
     */
    private String address;
    /**
     * 邮编
     */
    private String zipcode;
    /**
     * 人数
     */
   private String peopleNum;

    /**
     * 经验范围
     */
   private String companScope;
    /**
     * 企业邮箱
     */
   private String companyEmail;
    /**
     * 是否可用
     */
   private Integer state;

    /**
     * 企业是否认证(1是 0否)
     */
    private Integer isCompanyAuthen;
    /**
     * 电话
     */
    private String contactPhone;
    //1 芥末返回 2校企  3 第三方没验证过 4学生提交
    private Integer source;
}

使用  @TextIndexed 注解给企业名称加上文本索引 ,拥有text index的collection才支持全文检索 。

查询的java 代码:

    public List<GxyEnterpriseDto> searchCompany(String companyName){
        Query query = new Query();
       // Term term = new Term();
        Pageable pageable = PageRequest.of(0,50);
        TextCriteria criteria = TextCriteria.forDefaultLanguage()
                .matching(companyName);

        Criteria criteria1 =Criteria.where("source").in(1,2);

        query.addCriteria(criteria1);
        query.addCriteria(criteria);
//        Aggregation eatAggregation = Aggregation.newAggregation(
//                //查询条件
//                Aggregation.match(criteria),
//                //查询项
//                Aggregation.project("practiceCompanyId","companyName","companyCode","province","city","area","address","zipcode","peopleNum","companyType","companyTypeValue","trade","tradeValue","companScope","companyEmail","contactName","contactPhone"),
//                //分组条件和聚合项
//                Aggregation.group("companyCode","companyName"),
//                Aggregation.limit(300)
//        );
        List<GxyEnterpriseDto> datas = mongoTemplate.find(query.with(pageable),GxyEnterpriseDto.class);
        return datas;
    }

db.getCollection('enterprise').find({ "source" : { "$in" : [1, 2] }, "$text" : { "$search" : "浙江" } })

原文地址:https://www.cnblogs.com/z360519549/p/11597701.html