我有兩個物體,Search并且Amount我正在以這種方式使用 Hibernate Search 進行映射:
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@ToString
@Entity
@Indexed
public class Search extends SearchableItem {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEARCH_SEQ")
@DocumentId
private Long id;
@Embedded
@Valid
@IndexedEmbedded
private Amount maxAmount;
@Embedded
@Valid
@IndexedEmbedded
private Amount minAmount;
}
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@ToString
@Embeddable
public class Amount {
@NotNull
@ScaledNumberField(sortable = Sortable.YES)
private BigDecimal value;
@NotNull
@Enumerated(EnumType.STRING)
@GenericField
private CurrencyCode currency;
}
這些注釋以這種方式在 Elasticsearch 上創建我的索引:
{
"search-000001": {
"aliases": {
"search-read": {
"is_write_index": false
},
"search-write": {
"is_write_index": true
}
},
"mappings": {
"dynamic": "strict",
"properties": {
"_entity_type": {
"type": "keyword",
"index": false
},
"maxAmount": {
"dynamic": "strict",
"properties": {
"currency": {
"type": "keyword",
"doc_values": false
},
"value": {
"type": "scaled_float",
"scaling_factor": 100
}
}
},
"minAmount": {
"dynamic": "strict",
"properties": {
"currency": {
"type": "keyword",
"doc_values": false
},
"value": {
"type": "scaled_float",
"scaling_factor": 100
}
}
}
}
},
"settings": {
"index": {
"routing": {
"allocation": {
"include": {
"_tier_preference": "data_content"
}
}
},
"number_of_shards": "1",
"provided_name": "search-000001",
"creation_date": "1644485712235",
"analysis": {
"normalizer": {
"sort": {
"filter": [
"asciifolding",
"lowercase"
],
"type": "custom"
}
},
"analyzer": {
"name": {
"filter": [
"asciifolding",
"lowercase"
],
"type": "custom",
"tokenizer": "standard"
},
"generic_text": {
"filter": [
"asciifolding",
"lowercase",
"porter_stem"
],
"type": "custom",
"tokenizer": "standard"
}
}
},
"number_of_replicas": "1",
"uuid": "GBfV36PLT2-JEVPlbE2DVw",
"version": {
"created": "7160399"
}
}
}
}
}
對于 Elasticsearch 上的搜索,我使用了一個自定義評分函式,它嘗試訪問搜索檔案中的 minAmount 和 maxAmount 物件,但我收到此錯誤:
Unhandled Exception illegal_argument_exception
No field found for [maxAmount] in mapping
Stack:
[
"org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:100)",
"org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:28)",
"score = calculateScoreAmount(doc.maxAmount, doc.minAmount, params);\n ",
" ^---- HERE"
]
我過去在其他物體上遇到過同樣的問題,@GenericField(sortable = Sortable.YES)我在分數腳本中指定我需要訪問的屬性解決了它(請注意,如果沒有明確的 Sortable.YES,我在腳本評分函式執行期間收到了來自 Elasticsearch 的錯誤)。這里的問題是這Amount是一個自定義物件,@GenericField(sortable = Sortable.YES)不能在其上指定。
有人可以幫忙嗎?
uj5u.com熱心網友回復:
maxAmount是一個物件欄位。通過 檢索其值是沒有意義的doc.maxAmount,因為此語法用于訪問 doc 值,并且物件欄位不在 doc 值中表示。
我不確定您的腳本是什么,或者您將它傳遞給 Elasticsearch 的位置,但問題可能就在那里。
如果您想訪問maxAmount.value/的檔案值minAmount.value,那么您可能應該替換calculateScoreAmount(doc.maxAmount, doc.minAmount, params);為calculateScoreAmount(doc['maxAmount.value'].value, doc['minAmount.value'].value, params);
如果要訪問表示maxAmount/的 JSON 物件minAmount,則不應使用 docvalues,而應使用_source: calculateScoreAmount(ctx._source.maxAmount, ctx._source.minAmount, params);。我不確定這種語法是否完全正確,因為我找不到任何檔案來訪問源代碼,但它應該接近這個。另外,請記住,如果您有很多檔案,這會很慢。
也可以看看:
- https://www.elastic.co/guide/en/elasticsearch/reference/7.17/modules-scripting-using.html
- https://www.elastic.co/guide/en/elasticsearch/reference/7.17/sort-search-results.html#script-based-sorting
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/428676.html
