问题
I have an elasticsearch index that contains a phone number object with a contact document. The JSON is saved like this:
"contactId": "fd71b8e0-c3dd-4861-a45d-6917fdb48038",
"phone": { "country": "123",
"area": "202",
"number": "4567"
},
"type": "business"
The mapping that is used in this document for the phone object is in the form:
,
"phone": {
"properties": {
"area": {
"type": "string"
},
"country": {
"type": "string"
},
"number": {
"type": "string"
}
}
},
We're querying the index using a C# application that's implementing Nest. In the application we construct a Nest SearchDescriptor object that is used as part of the client call. How can I add to the SearchDescriptor to search for a phone number when the user is searching for a string like 1232024567?
回答1:
One way in which you could solve this is by also indexing but not storing the complete number i.e. index it to make it available to search on, but don't store it because it can be reconstituted from the area, country and number that you do store (and also probably index).
if country, area and number can always be determined from a complete phone number at index time e.g. always in a known format adhering to a pattern, then you might approach this differently by indexing and storing the complete phone number, and if country, area and number are only needed for search, then specifying the complete phone number as a multi-field, using a custom analyzer at index time on a field for each of country, area and number to extract the correct terms for indexing. at search time, you can then use these fields within the multi-field to find matches on country, area and number values.
来源:https://stackoverflow.com/questions/45925599/searching-phone-number-object-in-elasticsearch-with-c-sharp