java整合es指定字段不分词搜索

2018-09-14  本文已影响1217人  吴世浩

二、问题

在做一个需求的时候,需要按照电话号码查询用户关系,所以我这边先讲相关信息同步到es,但是电话号码是加密的,所以显示的字符串是杂乱的,既有字母,又有斜杠等号等字符,在进行分词查询的时候匹配不到相应的数据,所以需要对电话号码字段指定为不分词的查询即完全匹配

三、解决

import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldIndex;


@Document(indexName = "address_index",type = "t_address")
public class Address{
  
    @Id
    private Long id ;

    private String address;

    private String province;

    private String city;

    //@Field(type = FieldType.String , index = FieldIndex.not_analyzed)
    @Field(index = FieldIndex.not_analyzed)
    private String mobile;

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

在代码中指定某个字段不进行分词搜索时候,需要对其类型进行指定,否则查看索引如下图

not_analyzed.png

如果指定了字段类型,并且该字段不进行分词搜索,则可以看到其index为not_analyzed


analyzed.png

四、es后台管理使用遇到的问题

{
    "query": {
        "bool": {
            "filter": {
                "terms": {
                    "userNo": ["5832794"]
                }
            }
        }
    }
}
image.png

记住这里的查询,方法提交方式是POST、POST、POST

上一篇 下一篇

猜你喜欢

热点阅读