我的字典在下面
stopwords.txt有所有的停用詞
并synonym.txt有足球,足球
abc = [
{'id':1, 'name': 'christiano ronaldo', 'description': '[email protected]', 'type': 'football'},
{'id':2, 'name': 'lionel messi', 'description': '[email protected]','type': 'soccer'},
{'id':3, 'name': 'sachin', 'description': 'was', 'type': 'cricket'}
]
- 我有兩個 txt 檔案
stopwords.txt和synonym.txt - 如果我正在搜索停用詞,那么這些檔案不應回傳
- 我需要在
name和上應用設定description
resp = es.search(index="players",body={
"query": {
"query_string": {
"fields": ["name^2","description^2"],
"query": "was football*"
}
}})
我的出
{'took': 17,
'timed_out': False,
'_shards': {'total': 1, 'successful': 1, 'skipped': 0, 'failed': 0},
'hits': {'total': {'value': 2, 'relation': 'eq'},
'max_score': 2.345461,
'hits': [{'_index': 'players',
'_type': '_doc',
'_id': '3',
'_score': 2.345461,
'_source': {'id': 3,
'name': 'sachin',
'description': 'was',
'type': 'cricket'}},
{'_index': 'players',
'_type': '_doc',
'_id': '1',
'_score': 2.0,
'_source': {'id': 1,
'name': 'christiano ronaldo',
'description': '[email protected]',
'type': 'football'}}]}}
預期結果如下
{'took': 2,
'timed_out': False,
'_shards': {'total': 1, 'successful': 1, 'skipped': 0, 'failed': 0},
'hits': {'total': {'value': 2, 'relation': 'eq'},
'max_score': 2.0,
'hits': [{'_index': 'players',
'_type': '_doc',
'_id': '1',
'_score': 2.0,
'_source': {'id': 1,
'name': 'christiano ronaldo',
'description': '[email protected]',
'type': 'football'}},
{'_index': 'players',
'_type': '_doc',
'_id': '2',
'_score': 2.0,
'_source': {'id': 2,
'name': 'lionel messi',
'description': '[email protected]',
'type': 'soccer'}}]}}
uj5u.com熱心網友回復:
基本上你需要定義同義詞和停用詞,而不是在檔案中定義它,你也可以在設定本身中傳遞同義詞,這樣改變它會很容易,你只需要關閉索引,更新同義詞串列并再次打開索引。
此外,如果您使用english內容,Elasticsearch 也有一個默認的停止世界串列,was您可以在此處查看所有默認停止詞
現在使用以下設定和映射來創建您的索引
{
"settings": {
"index": {
"analysis": {
"filter": {
"synonym_en": {
"type": "synonym_graph",
"synonyms": [
"football, soccer"
]
},
"english_stop": {
"type": "stop",
"stopwords": "_english_"
}
},
"analyzer": {
"english_analyzer": {
"tokenizer": "standard",
"filter": [
"lowercase",
"english_stop",
"synonym_en"
]
}
}
}
}
},
"mappings": {
"properties": {
"name": {
"type": "text",
"analyzer": "english_analyzer"
},
"description": {
"type": "text",
"analyzer": "english_analyzer"
}
}
}
}
之后使用相同的查詢,現在您應該能夠獲得預期的結果。
uj5u.com熱心網友回復:
您可以使用以下索引映射,在 中包含多個檔案stopwords_path,然后在name和description欄位上使用自定義分析器。
{
"settings": {
"analysis": {
"analyzer": {
"stop-analyzer": {
"tokenizer": "whitespace",
"filter": [
"stop_words_1",
"stop_words_2"
]
}
},
"filter": {
"stop_words_1": {
"type": "stop",
"stopwords_path": "stopwords.txt"
},
"stop_words_2": {
"type": "stop",
"stopwords_path": "synonym.txt"
}
}
}
},
"mappings": {
"properties": {
"name": {
"type": "text",
"analyzer": "stop-analyzer"
},
"description": {
"type": "text",
"analyzer": "stop-analyzer"
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/498378.html
