Elasticsearch synonym analyzer not working

回眸只為那壹抹淺笑 提交于 2019-12-05 19:56:06

问题


EDIT: To add on to this, the synonyms seem to be working with basic querystring queries.

"query_string" : {
    "default_field" : "location.region.name.raw",
    "query" : "nh"
}

This returns all of the results for New Hampshire, but a "match" query for "nh" returns no results.


I'm trying to add synonyms to my location fields in my Elastic index, so that if I do a location search for "Mass," "Ma," or "Massachusetts" I'll get the same results each time. I added the synonyms filter to my settings and changed the mapping for locations. Here are my settings:

analysis":{
    "analyzer":{
        "synonyms":{
            "filter":[
                "lowercase",
                "synonym_filter"
            ],
        "tokenizer": "standard"
    }
},
"filter":{
    "synonym_filter":{
        "type": "synonym",
        "synonyms":[
            "United States,US,USA,USA=>usa",
            "Alabama,Al,Ala,Ala",
            "Alaska,Ak,Alas,Alas",
            "Arizona,Az,Ariz",
            "Arkansas,Ar,Ark",
            "California,Ca,Calif,Cal",
            "Colorado,Co,Colo,Col",
            "Connecticut,Ct,Conn",
            "Deleware,De,Del",
            "District of Columbia,Dc,Wash Dc,Washington Dc=>Dc",
            "Florida,Fl,Fla,Flor",
            "Georgia,Ga",
            "Hawaii,Hi",
            "Idaho,Id,Ida",
            "Illinois,Il,Ill,Ills",
            "Indiana,In,Ind",
            "Iowa,Ia,Ioa",
            "Kansas,Kans,Kan,Ks",
            "Kentucky,Ky,Ken,Kent",
            "Louisiana,La",
            "Maine,Me",
            "Maryland,Md",
            "Massachusetts,Ma,Mass",
            "Michigan,Mi,Mich",
            "Minnesota,Mn,Minn",
            "Mississippi,Ms,Miss",
            "Missouri,Mo",
            "Montana,Mt,Mont",
            "Nebraska,Ne,Neb,Nebr",
            "Nevada,Nv,Nev",
            "New Hampshire,Nh=>Nh",
            "New Jersey,Nj=>Nj",
            "New Mexico,Nm,N Mex,New M=>Nm",
            "New York,Ny=>Ny",
            "North Carolina,Nc,N Car=>Nc",
            "North Dakota,Nd,N Dak, NoDak=>Nd",
            "Ohio,Oh,O",
            "Oklahoma,Ok,Okla",
            "Oregon,Or,Oreg,Ore",
            "Pennsylvania,Pa,Penn,Penna",
            "Rhode Island,Ri,Ri & PP,R Isl=>Ri",
            "South Carolina,Sc,S Car=>Sc",
            "South Dakota,Sd,S Dak,SoDak=>Sd",
            "Tennessee,Te,Tenn",
            "Texas,Tx,Tex",
            "Utah,Ut",
            "Vermont,Vt",
            "Virginia,Va,Virg",
            "Washington,Wa,Wash,Wn",
            "West Virginia,Wv,W Va, W Virg=>Wv",
            "Wisconsin,Wi,Wis,Wisc",
            "Wyomin,Wi,Wyo"
        ]
    }
}

And the mapping for the location.region field:

"region":{
    "properties":{
        "id":{"type": "long"},
        "name":{
            "type": "string",
            "analyzer": "synonyms",
            "fields":{"raw":{"type": "string", "index": "not_analyzed" }}
        }
    }
}

But the synonyms analyzer doesn't seem to be doing anything. This query for example:

"match" : {
    "location.region.name" : {
        "query" : "Massachusetts",
        "type" : "phrase",
        "analyzer" : "synonyms"
    }
}

This returns hundreds of results, but if I replace "Massachusetts" with "Ma" or "Mass" I get 0 results. Why isn't it working?


回答1:


The order of the filters is

filter":[
    "lowercase",
    "synonym_filter"
]

So, if elasticsearch is "lowercasing" first the tokens, when it executes the second step, synonym_filter, it won't match any of the entries you have defined.

To solve the problem, I would define the synonyms in lower case




回答2:


You can also define your synonyms filter as case insensitive:


    "filter":{
        "synonym_filter":{
            "type": "synonym",
            "ignore_case" : "true",
            "synonyms":[
                ...
            ]
        }
    }



来源:https://stackoverflow.com/questions/29354312/elasticsearch-synonym-analyzer-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!