我正在尝试使用弹性搜索来执行子字符串搜索。
response = es.search(index='salary_fulltime', body={
'query':{
'bool':{
'must':[{
'match_phrase':{
'title':'sr. java developer'
}
},{
'match_phrase':{
'location':'holtsville'
}
}]
}
}
})在我的数据库里有这样的标题,
Senior Java Developer, Java Developer, Java Engineer但是没有像sr. java developer这样的例子,
有没有办法可以进行子串匹配。尽管我的弹性搜索索引中没有Sr.,但有没有一种方法可以将sr. java developer与我们数据库中的内容(如Senior Java Developer, Java Developer, Java Engineer )进行匹配。
目前我的搜索没有匹配到任何东西。
[{'_id': '484',
'_index': 'data',
'_score': 13.8527,
'_source': {'title': 'Java Developer / Engineer'},
'_type': '_doc'},
{'_id': '385',
'_index': 'data',
'_score': 12.527,
'_source': {'title': 'Senior Java Developer / Engineer'},
'_type': '_doc'},
{'_id': '433',
'_index': 'data',
'_score': 11.828527,
'_source': {'title': 'Java Architect'},
'_type': '_doc'}]发布于 2021-05-23 21:40:02
假设title字段为text数据类型。因此,如果没有为text数据类型字段定义分析器,那么elasticsearch将使用standard analyzer。这会将"Senior Java Developer"标记为
{
"tokens": [
{
"token": "senior",
"start_offset": 0,
"end_offset": 6,
"type": "<ALPHANUM>",
"position": 0
},
{
"token": "java",
"start_offset": 7,
"end_offset": 11,
"type": "<ALPHANUM>",
"position": 1
},
{
"token": "developer",
"start_offset": 12,
"end_offset": 21,
"type": "<ALPHANUM>",
"position": 2
}
]
}在搜索查询中,当您搜索sr. java developer时,它再次被标记化为sr、java、developer。此查询将匹配具有上述任一标记的任何文档。
您可以简单地使用match查询而不是匹配短语查询
{
"query": {
"match": {
"title": "sr. java developer"
}
}
}搜索结果将是
"hits": [
{
"_index": "67660379",
"_type": "_doc",
"_id": "2",
"_score": 0.6409958,
"_source": {
"title": "Java Developer"
}
},
{
"_index": "67660379",
"_type": "_doc",
"_id": "1",
"_score": 0.5403744,
"_source": {
"title": "Senior Java Developer"
}
},
{
"_index": "67660379",
"_type": "_doc",
"_id": "3",
"_score": 0.14181954,
"_source": {
"title": "Java Engineer"
}
}
]更新1:
可以将minimum_should_match参数与匹配查询一起使用
{
"query": {
"match": {
"title": {
"query": "sr. java developer",
"minimum_should_match": "75%"
}
}
}
}搜索结果将是
"hits": [
{
"_index": "67660379",
"_type": "_doc",
"_id": "2",
"_score": 0.6409958,
"_source": {
"title": "Java Developer"
}
},
{
"_index": "67660379",
"_type": "_doc",
"_id": "1",
"_score": 0.5403744,
"_source": {
"title": "Senior Java Developer"
}
}
]https://stackoverflow.com/questions/67660379
复制相似问题