Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
package com.azure.search.documents.indexes;

import com.azure.core.util.logging.ClientLogger;
import com.azure.search.documents.models.GeoPoint;
import com.azure.search.documents.indexes.models.LexicalAnalyzerName;
import com.azure.search.documents.indexes.models.SearchField;
import com.azure.search.documents.indexes.models.SearchFieldDataType;
import com.azure.search.documents.models.GeoPoint;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
Expand Down Expand Up @@ -113,7 +113,7 @@ private static SearchField buildNoneParameterizedType(java.lang.reflect.Field cl


private static boolean isArrayOrList(Type type) {
return type.getClass().isArray() || isList(type);
return isList(type) || ((Class) type).isArray();
}

private static boolean isList(Type type) {
Expand All @@ -140,13 +140,13 @@ private static SearchField buildCollectionField(java.lang.reflect.Field classFie
}

private static Type getComponentOrElementType(Type arrayOrListType, ClientLogger logger) {
if (arrayOrListType.getClass().isArray()) {
return arrayOrListType.getClass().getComponentType();
}
if (isList(arrayOrListType)) {
ParameterizedType pt = (ParameterizedType) arrayOrListType;
return pt.getActualTypeArguments()[0];
}
if (((Class) arrayOrListType).isArray()) {
return ((Class) arrayOrListType).getComponentType();
}
throw logger.logExceptionAsError(new RuntimeException(String.format(
"Collection type %s is not supported.", arrayOrListType.getTypeName())));
}
Expand All @@ -156,13 +156,7 @@ private static SearchField convertToBasicSearchField(java.lang.reflect.Field cla
SearchField searchField = new SearchField();
searchField.setName(classField.getName());
SearchFieldDataType dataType = covertToSearchFieldDataType(classField.getGenericType(), false, logger);
searchField.setType(dataType)
.setKey(false)
.setSearchable(false)
.setFacetable(false)
.setHidden(false)
.setFilterable(false)
.setSortable(false);
searchField.setType(dataType);
return searchField;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@

/**
* Optional arguments defines the array of synonymMaps used for the field.
* This option can be used only with searchable fields. Currently only one
* synonym map per field is supported. Assigning a synonym map to a field
* ensures that query terms targeting that field are expanded at query-time
* using the rules in the synonym map. This attribute can be changed on
* existing fields.
*
* @return An array of synonym map values. Or default to empty string array.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,6 @@ public ComplexField setFields(List<SearchField> fields) {
public SearchField build() {
return new SearchField().setName(super.getName())
.setType(super.getDataType())
.setFields(fields)
.setKey(false)
.setFilterable(false)
.setSortable(false)
.setHidden(false)
.setSearchable(false)
.setFacetable(false);
.setFields(fields);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ public void canMergeStaticallyTypedDocuments() throws ParseException {
.bedOptions("1 Queen Bed")
.sleepsCount(2)
.smokingAllowed(true)
.tags(Collections.singletonList("vcr/dvd")),
.tags(new String[] { "vcr/dvd" }),
new HotelRoom()
.description("Budget Room, 1 King Bed (Mountain View)")
.descriptionFr("Chambre Économique, 1 très grand lit (Mountain View)")
Expand All @@ -441,7 +441,7 @@ public void canMergeStaticallyTypedDocuments() throws ParseException {
.bedOptions("1 King Bed")
.sleepsCount(2)
.smokingAllowed(true)
.tags(Arrays.asList("vcr/dvd", "jacuzzi tub"))
.tags(new String[] {"vcr/dvd", "jacuzzi tub"})
));

// Update category, tags, parking included, rating, and rooms. Erase description, last renovation date, location and address.
Expand All @@ -463,7 +463,7 @@ public void canMergeStaticallyTypedDocuments() throws ParseException {
.baseRate(10.5)
.bedOptions("1 Queen Bed")
.sleepsCount(2)
.tags(Arrays.asList("vcr/dvd", "balcony"))
.tags(new String[] { "vcr/dvd", "balcony" })
));

// Fields whose values get updated are updated, and whose values get erased remain the same.
Expand Down Expand Up @@ -492,7 +492,7 @@ public void canMergeStaticallyTypedDocuments() throws ParseException {
.baseRate(10.5)
.bedOptions("1 Queen Bed")
.sleepsCount(2)
.tags(Arrays.asList("vcr/dvd", "balcony"))
.tags(new String[] { "vcr/dvd", "balcony" })
));

List<Hotel> originalDocs = new ArrayList<>();
Expand Down Expand Up @@ -561,7 +561,7 @@ public void canSetExplicitNullsInStaticallyTypedDocument() throws ParseException
.bedOptions("1 Queen Bed")
.sleepsCount(2)
.smokingAllowed(true)
.tags(Collections.singletonList("vcr/dvd")),
.tags(new String[] { "vcr/dvd" }),
new HotelRoom()
.description("Budget Room, 1 King Bed (Mountain View)")
.descriptionFr("Chambre Économique, 1 très grand lit (Mountain View)")
Expand All @@ -570,7 +570,7 @@ public void canSetExplicitNullsInStaticallyTypedDocument() throws ParseException
.bedOptions("1 King Bed")
.sleepsCount(2)
.smokingAllowed(true)
.tags(Arrays.asList("vcr/dvd", "jacuzzi tub"))
.tags(new String[] { "vcr/dvd", "jacuzzi tub" })
));

LoudHotel updatedDoc = new LoudHotel()
Expand All @@ -589,7 +589,7 @@ public void canSetExplicitNullsInStaticallyTypedDocument() throws ParseException
.type("Budget Room")
.baseRate(10.5)
.smokingAllowed(false)
.tags(Arrays.asList("vcr/dvd", "balcony"))
.tags(new String[] { "vcr/dvd", "balcony" })
));

LoudHotel expectedDoc = new LoudHotel()
Expand Down Expand Up @@ -621,7 +621,7 @@ public void canSetExplicitNullsInStaticallyTypedDocument() throws ParseException
.bedOptions(null)
.sleepsCount(null)
.smokingAllowed(false)
.tags(Arrays.asList("vcr/dvd", "balcony"))
.tags(new String[] { "vcr/dvd", "balcony" })
));

List<LoudHotel> originalDocs = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ Hotel prepareEmptyHotel() {
return new Hotel().hotelId("1")
.tags(new ArrayList<>())
.rooms(Collections.singletonList(
new HotelRoom().tags(new ArrayList<>())
new HotelRoom().tags(new String[0])
));
}

Expand Down Expand Up @@ -437,7 +437,7 @@ Hotel prepareSelectedFieldsHotel() throws ParseException {
.bedOptions("1 King Bed")
.sleepsCount(2)
.smokingAllowed(true)
.tags(Collections.singletonList("coffee maker")),
.tags(new String[] { "coffee maker" }),
new HotelRoom()
.description("Budget Room, 1 Queen Bed (Amenities)")
.descriptionFr("Chambre Économique, 1 grand lit (Services)")
Expand All @@ -446,7 +446,7 @@ Hotel prepareSelectedFieldsHotel() throws ParseException {
.bedOptions("1 Queen Bed")
.sleepsCount(2)
.smokingAllowed(false)
.tags(Collections.singletonList("coffee maker"))));
.tags(new String[] { "coffee maker" })));
}

ModelWithPrimitiveCollections preparePrimitivesModel() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.search.documents.indexes;

import com.azure.search.documents.SearchTestBase;
import com.azure.search.documents.indexes.models.SearchIndex;
import com.azure.search.documents.indexes.models.SynonymMap;
import com.azure.search.documents.test.environment.models.Hotel;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

import static com.azure.search.documents.TestHelpers.assertObjectEquals;

public class FieldBuilderServiceTests extends SearchTestBase {
private SearchIndexClient client;
private final List<String> indexesToDelete = new ArrayList<>();
private final List<String> synonymMapsToDelete = new ArrayList<>();
private SearchIndex index;
String synonymMapName = "fieldbuilder";

@Override
protected void beforeTest() {
super.beforeTest();
client = getSearchIndexClientBuilder().buildClient();
index = new SearchIndex().setName(testResourceNamer.randomName("fieldbuilder", 32));
}

@Override
protected void afterTest() {
super.afterTest();

for (String index : indexesToDelete) {
client.deleteIndex(index);
}

client.deleteSynonymMap(synonymMapName);
}

@Test
public void createIndexWithFieldBuilder() {
SynonymMap synonymMap = new SynonymMap().setName(synonymMapName).setSynonyms("hotel,motel");
client.createSynonymMap(synonymMap);
index.setFields(FieldBuilder.build(Hotel.class));
client.createIndex(index);
indexesToDelete.add(index.getName());
assertObjectEquals(index, client.getIndex(index.getName()), true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@

import com.azure.search.documents.TestHelpers;
import com.azure.search.documents.indexes.models.ComplexField;
import com.azure.search.documents.indexes.models.LexicalAnalyzerName;
import com.azure.search.documents.indexes.models.SearchField;
import com.azure.search.documents.indexes.models.SearchFieldDataType;
import com.azure.search.documents.indexes.models.SearchableField;
import com.azure.search.documents.indexes.models.SimpleField;
import com.azure.search.documents.test.environment.models.Hotel;
import com.azure.search.documents.test.environment.models.HotelAnalyzerException;
import com.azure.search.documents.test.environment.models.HotelCircularDependencies;
import com.azure.search.documents.test.environment.models.HotelSearchException;
import com.azure.search.documents.test.environment.models.HotelSearchableExceptionOnList;
import com.azure.search.documents.test.environment.models.HotelTwoDimensional;
import com.azure.search.documents.test.environment.models.HotelWithArray;
import com.azure.search.documents.test.environment.models.HotelWithEmptyInSynonymMaps;
import org.junit.jupiter.api.Test;

Expand All @@ -28,13 +27,7 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class FieldBuilderTest {
@Test
public void hotelComparison() {
List<SearchField> actualFields = sortByFieldName(FieldBuilder.build(Hotel.class));
List<SearchField> expectedFields = sortByFieldName(buildHotelFields());
assertListFieldEquals(expectedFields, actualFields);
}
public class FieldBuilderTests {

@Test
public void hotelSearchableThrowException() {
Expand Down Expand Up @@ -80,6 +73,13 @@ public void hotelAnalyzerException() {
"either analyzer or both searchAnalyzer and indexAnalyzer");
}

@Test
public void hotelWithArrayType() {
List<SearchField> actualFields = sortByFieldName(FieldBuilder.build(HotelWithArray.class));
List<SearchField> expectedFields = sortByFieldName(buildHotelWithArrayModel());
assertListFieldEquals(expectedFields, actualFields);
}

private void assertListFieldEquals(List<SearchField> expected, List<SearchField> actual) {
assertEquals(expected.size(), actual.size());
for (int i = 0; i < expected.size(); i++) {
Expand All @@ -105,25 +105,11 @@ private List<SearchField> buildHotelInAddress() {
return Collections.singletonList(hotel);
}

private List<SearchField> buildHotelFields() {
SearchField hotelId = new SimpleField("hotelId", SearchFieldDataType.STRING, false).setSortable(true)
.setKey(true).build();
SearchField hotelName = new SearchableField("hotelName", false).setAnalyzer(LexicalAnalyzerName.fromString("en.lucene"))
private List<SearchField> buildHotelWithArrayModel() {
SearchField hotelId = new SimpleField("hotelId", SearchFieldDataType.STRING, false).setKey(true)
.setSortable(true).build();
SearchField description = new SimpleField("description", SearchFieldDataType.STRING, false).build();
SearchField category = new SimpleField("category", SearchFieldDataType.STRING, false).build();
SearchField tags = new SearchableField("tags", true).build();
SearchField parkingIncluded = new SimpleField("parkingIncluded", SearchFieldDataType.BOOLEAN, false).build();
SearchField smokingAllowed = new SimpleField("smokingAllowed", SearchFieldDataType.BOOLEAN, false).build();
SearchField lastRenovationDate = new SimpleField("lastRenovationDate", SearchFieldDataType.DATE_TIME_OFFSET, false).build();
SearchField rating = new SimpleField("rating", SearchFieldDataType.INT32, false).build();
SearchField location = new SimpleField("location", SearchFieldDataType.GEOGRAPHY_POINT, false).build();
SearchField address = new ComplexField("address", false)
.setFields(buildHotelAddressField()).build();
SearchField rooms = new ComplexField("rooms", true).setFields(buildHotelRoomField()).build();

return Arrays.asList(hotelId, hotelName, description, category, tags, parkingIncluded, smokingAllowed,
lastRenovationDate, rating, location, address, rooms);
return Arrays.asList(hotelId, tags);
}

private List<SearchField> buildHotelAddressField() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ public class Hotel {
@JsonProperty(value = "HotelName")
private String hotelName;

@SimpleFieldProperty
@JsonProperty(value = "Description")
private String description;

@FieldIgnore
@JsonProperty(value = "Description_fr")
private String descriptionFr;

@SimpleFieldProperty
@JsonProperty(value = "Category")
private String category;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.fasterxml.jackson.annotation.JsonProperty;

public class HotelAddress {
@SimpleFieldProperty(isKey = true, isFacetable = true)
@SimpleFieldProperty(isFacetable = true)
@JsonProperty(value = "StreetAddress")
private String streetAddress;

Expand All @@ -19,7 +19,7 @@ public class HotelAddress {
@JsonProperty(value = "StateProvince")
private String stateProvince;

@SearchableFieldProperty(synonymMaps = {"America -> USA", "USA -> US"})
@SearchableFieldProperty(synonymMaps = {"fieldbuilder"})
@JsonProperty(value = "Country")
private String country;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.List;

public class HotelRoom {
@JsonProperty(value = "Description")
private String description;
Expand All @@ -29,7 +27,7 @@ public class HotelRoom {
private Boolean smokingAllowed;

@JsonProperty(value = "Tags")
private List<String> tags;
private String[] tags;


public String description() {
Expand Down Expand Up @@ -95,11 +93,11 @@ public HotelRoom smokingAllowed(Boolean smokingAllowed) {
return this;
}

public List<String> tags() {
public String[] tags() {
return this.tags;
}

public HotelRoom tags(List<String> tags) {
public HotelRoom tags(String[] tags) {
this.tags = tags;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.search.documents.test.environment.models;

import com.azure.search.documents.indexes.SearchableFieldProperty;
import com.azure.search.documents.indexes.SimpleFieldProperty;

public class HotelWithArray {
@SimpleFieldProperty(isKey = true, isSortable = true)
private String hotelId;

@SearchableFieldProperty
private String[] tags;
}
Loading