MCPcopy
hub / github.com/elastic/go-elasticsearch / setupIndex

Function setupIndex

_examples/bulk/base64.go:182–207  ·  view source on GitHub ↗
(ctx context.Context, es *elasticsearch.TypedClient, index string)

Source from the content-addressed store, hash-verified

180}
181
182func setupIndex(ctx context.Context, es *elasticsearch.TypedClient, index string) error {
183 if ok, _ := es.Indices.Exists(index).IsSuccess(ctx); ok {
184 if _, err := es.Indices.Delete(index).Do(ctx); err != nil {
185 return fmt.Errorf("delete index: %w", err)
186 }
187 }
188
189 mappings := esdsl.NewTypeMapping().
190 AddProperty("docid", esdsl.NewKeywordProperty()).
191 AddProperty("title", esdsl.NewTextProperty()).
192 AddProperty("text", esdsl.NewTextProperty().AddField("keyword", esdsl.NewKeywordProperty().IgnoreAbove(256))).
193 AddProperty("emb", esdsl.NewDenseVectorProperty().
194 Dims(1536).
195 Index(true).
196 Similarity(densevectorsimilarity.Cosine).
197 IndexOptions(esdsl.NewDenseVectorIndexOptions(densevectorindexoptionstype.Flat)))
198
199 if _, err := es.Indices.
200 Create(index).
201 Mappings(mappings).
202 Do(ctx); err != nil {
203 return fmt.Errorf("create index: %w", err)
204 }
205
206 return nil
207}
208
209func ingestDocs(ctx context.Context, client *elasticsearch.TypedClient, index string, docs []B64Doc) (time.Duration, error) {
210 start := time.Now()

Callers 1

mainFunction · 0.70

Calls 15

NewTypeMappingFunction · 0.92
NewKeywordPropertyFunction · 0.92
NewTextPropertyFunction · 0.92
NewDenseVectorPropertyFunction · 0.92
DoMethod · 0.65
IsSuccessMethod · 0.45
ExistsMethod · 0.45
DeleteMethod · 0.45
AddPropertyMethod · 0.45
AddFieldMethod · 0.45
IgnoreAboveMethod · 0.45

Tested by

no test coverage detected