Elasticsearch 7 upgrade (#113)

* initial upgrade to elasticsearch 7

* fix stat page error

* i am an idiot

* fix es not creating new indexes

* more complete removal of doc_type

Co-authored-by: Luna D <cod7777@yandex.ru>
This commit is contained in:
liamwhite 2020-05-04 08:19:42 -04:00 committed by GitHub
parent 4b86e783ef
commit 6bbe358dd1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 164 additions and 218 deletions

View file

@ -21,17 +21,21 @@ services:
postgres: postgres:
image: postgres:12.2 image: postgres:12.2
environment:
- POSTGRES_PASSWORD=postgres
volumes: volumes:
- postgres_data:/var/lib/postgresql/data - postgres_data:/var/lib/postgresql/data
logging: logging:
driver: "none" driver: "none"
elasticsearch: elasticsearch:
image: elasticsearch:6.8.5 image: elasticsearch:7.6.2
volumes: volumes:
- elastic_data:/var/lib/elasticsearch - elastic_data:/var/lib/elasticsearch
logging: logging:
driver: "none" driver: "none"
environment:
- discovery.type=single-node
ulimits: ulimits:
nofile: nofile:
soft: 65536 soft: 65536

View file

@ -6,11 +6,6 @@ defmodule Philomena.Comments.ElasticsearchIndex do
"comments" "comments"
end end
@impl true
def doc_type do
"comment"
end
@impl true @impl true
def mapping do def mapping do
%{ %{
@ -21,8 +16,6 @@ defmodule Philomena.Comments.ElasticsearchIndex do
} }
}, },
mappings: %{ mappings: %{
comment: %{
_all: %{enabled: false},
dynamic: false, dynamic: false,
properties: %{ properties: %{
id: %{type: "integer"}, id: %{type: "integer"},
@ -41,7 +34,6 @@ defmodule Philomena.Comments.ElasticsearchIndex do
} }
} }
} }
}
end end
@impl true @impl true

View file

@ -50,14 +50,9 @@ defmodule Philomena.Elasticsearch do
index = index_for(module) index = index_for(module)
index_name = index.index_name() index_name = index.index_name()
doc_type = index.doc_type() mapping = index.mapping().mappings.properties
mapping = Elastix.Mapping.put(elastic_url(), index_name, "_doc", %{properties: mapping})
index.mapping().mappings
|> Map.get(String.to_atom(doc_type))
|> Map.get(:properties)
Elastix.Mapping.put(elastic_url(), index_name, doc_type, %{properties: mapping})
end end
def index_document(doc, module) do def index_document(doc, module) do
@ -67,7 +62,7 @@ defmodule Philomena.Elasticsearch do
Elastix.Document.index( Elastix.Document.index(
elastic_url(), elastic_url(),
index.index_name(), index.index_name(),
[index.doc_type()], "_doc",
data.id, data.id,
data data
) )
@ -79,7 +74,7 @@ defmodule Philomena.Elasticsearch do
Elastix.Document.delete( Elastix.Document.delete(
elastic_url(), elastic_url(),
index.index_name(), index.index_name(),
index.doc_type(), "_doc",
id id
) )
end end
@ -93,7 +88,7 @@ defmodule Philomena.Elasticsearch do
doc = index.as_json(record) doc = index.as_json(record)
[ [
%{index: %{_index: index.index_name(), _type: index.doc_type(), _id: doc.id}}, %{index: %{_index: index.index_name(), _id: doc.id}},
doc doc
] ]
end) end)
@ -179,7 +174,7 @@ defmodule Philomena.Elasticsearch do
Elastix.Search.search( Elastix.Search.search(
elastic_url(), elastic_url(),
index.index_name(), index.index_name(),
[index.doc_type()], [],
query_body query_body
) )
@ -194,12 +189,13 @@ defmodule Philomena.Elasticsearch do
Map.merge(elastic_query, %{ Map.merge(elastic_query, %{
from: (page_number - 1) * page_size, from: (page_number - 1) * page_size,
size: page_size, size: page_size,
_source: false _source: false,
track_total_hits: true
}) })
results = search(module, elastic_query) results = search(module, elastic_query)
time = results["took"] time = results["took"]
count = results["hits"]["total"] count = results["hits"]["total"]["value"]
entries = Enum.map(results["hits"]["hits"], &String.to_integer(&1["_id"])) entries = Enum.map(results["hits"]["hits"], &String.to_integer(&1["_id"]))
Logger.debug("[Elasticsearch] Query took #{time}ms") Logger.debug("[Elasticsearch] Query took #{time}ms")

View file

@ -3,12 +3,6 @@ defmodule Philomena.ElasticsearchIndex do
# This is usually a collection name like "images". # This is usually a collection name like "images".
@callback index_name() :: String.t() @callback index_name() :: String.t()
# Returns the document type for the index.
# This is usually the singular of the index name, like "image".
#
# TODO: Remove for ES 7.0
@callback doc_type() :: String.t()
# Returns the mapping and settings for the index. # Returns the mapping and settings for the index.
@callback mapping() :: map() @callback mapping() :: map()

View file

@ -6,11 +6,6 @@ defmodule Philomena.Galleries.ElasticsearchIndex do
"galleries" "galleries"
end end
@impl true
def doc_type do
"gallery"
end
@impl true @impl true
def mapping do def mapping do
%{ %{
@ -21,8 +16,6 @@ defmodule Philomena.Galleries.ElasticsearchIndex do
} }
}, },
mappings: %{ mappings: %{
gallery: %{
_all: %{enabled: false},
dynamic: false, dynamic: false,
properties: %{ properties: %{
# keyword # keyword
@ -41,7 +34,6 @@ defmodule Philomena.Galleries.ElasticsearchIndex do
} }
} }
} }
}
end end
@impl true @impl true

View file

@ -6,11 +6,6 @@ defmodule Philomena.Images.ElasticsearchIndex do
"images" "images"
end end
@impl true
def doc_type do
"image"
end
@impl true @impl true
def mapping do def mapping do
%{ %{
@ -21,8 +16,6 @@ defmodule Philomena.Images.ElasticsearchIndex do
} }
}, },
mappings: %{ mappings: %{
image: %{
_all: %{enabled: false},
dynamic: false, dynamic: false,
properties: %{ properties: %{
anonymous: %{type: "boolean"}, anonymous: %{type: "boolean"},
@ -87,7 +80,6 @@ defmodule Philomena.Images.ElasticsearchIndex do
} }
} }
} }
}
end end
@impl true @impl true

View file

@ -6,11 +6,6 @@ defmodule Philomena.Posts.ElasticsearchIndex do
"posts" "posts"
end end
@impl true
def doc_type do
"post"
end
@impl true @impl true
def mapping do def mapping do
%{ %{
@ -21,8 +16,6 @@ defmodule Philomena.Posts.ElasticsearchIndex do
} }
}, },
mappings: %{ mappings: %{
post: %{
_all: %{enabled: false},
dynamic: false, dynamic: false,
properties: %{ properties: %{
id: %{type: "integer"}, id: %{type: "integer"},
@ -46,7 +39,6 @@ defmodule Philomena.Posts.ElasticsearchIndex do
} }
} }
} }
}
end end
@impl true @impl true

View file

@ -6,11 +6,6 @@ defmodule Philomena.Reports.ElasticsearchIndex do
"reports" "reports"
end end
@impl true
def doc_type do
"report"
end
@impl true @impl true
def mapping do def mapping do
%{ %{
@ -21,8 +16,6 @@ defmodule Philomena.Reports.ElasticsearchIndex do
} }
}, },
mappings: %{ mappings: %{
report: %{
_all: %{enabled: false},
dynamic: false, dynamic: false,
properties: %{ properties: %{
id: %{type: "integer"}, id: %{type: "integer"},
@ -42,7 +35,6 @@ defmodule Philomena.Reports.ElasticsearchIndex do
} }
} }
} }
}
end end
@impl true @impl true

View file

@ -6,11 +6,6 @@ defmodule Philomena.Tags.ElasticsearchIndex do
"tags" "tags"
end end
@impl true
def doc_type do
"tag"
end
@impl true @impl true
def mapping do def mapping do
%{ %{
@ -29,8 +24,6 @@ defmodule Philomena.Tags.ElasticsearchIndex do
} }
}, },
mappings: %{ mappings: %{
tag: %{
_all: %{enabled: false},
dynamic: false, dynamic: false,
properties: %{ properties: %{
id: %{type: "integer"}, id: %{type: "integer"},
@ -58,7 +51,6 @@ defmodule Philomena.Tags.ElasticsearchIndex do
} }
} }
} }
}
end end
@impl true @impl true

View file

@ -23,7 +23,7 @@ elixir:
p p
' There are ' There are
span.stat> span.stat>
= number_with_delimiter(@comment_aggs["hits"]["total"]) = number_with_delimiter(@comment_aggs["hits"]["total"]["value"])
' comments on the site. Of these, ' comments on the site. Of these,
=> number_with_delimiter(cmt_bucket["deleted"]["doc_count"]) => number_with_delimiter(cmt_bucket["deleted"]["doc_count"])
' have been deleted. ' have been deleted.