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" : "浙江" } })


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM