ElasticSearch

Par chaptor, il y a 8 ans


Bonjour,

J'essaie d'intégrer ElasticSearch à mon projet RoR mais j'ai un problème de mapping: Lorsque j'ajoute un enregistrement, il utilise le mapping automatique alors que j'ai configuré un mapping.

J'ai essayé plusieurs tuto sur une app vierge et j'obtiens toujours le même problème.

Voici le code à partir d'une app vierge:

Post.rb:

require 'elasticsearch/model' class Post < ApplicationRecord include Elasticsearch::Model include Elasticsearch::Model::Callbacks belongs_to :category def self.search(query) __elasticsearch__.search( { query: { multi_match: { query: query, fields: ['title^10', 'content'] } } } ) end settings index: { number_of_shards: 1 } do mappings dynamic: 'false' do indexes :title, analyzer: ''french' indexes :content, analyzer: ''french' indexes :category, type: 'object', properties: { name: {type: 'string', analyzer: 'french' } } end end end Post.__elasticsearch__.client.indices.delete index: Post.index_name rescue nil # Create the new index with the new mapping Post.__elasticsearch__.client.indices.create \ index: Post.index_name, body: { settings: Post.settings.to_hash, mappings: Post.mappings.to_hash } # Index all article records from the DB to Elasticsearch Post.import

Pourtant mon mapping est reconnu par elasticsearch car quand je vais sur http://localhost:9200/posts/_mapping?pretty=true , j'ai ceci qui correspond à mon mapping

{ "posts" : { "mappings" : { "post" : { "dynamic" : "false", "properties" : { "category" : { "properties" : { "name" : { "type" : "text", "analyzer" : "french" } } }, "content" : { "type" : "text", "analyzer" : "french" }, "title" : { "type" : "text", "analyzer" : "french" } } } } } }

Mais quand je fais:

GET /posts/_search { "query": { "match_all": {} } }

J'obtiens

{ "took": 1, "timed_out": false, "_shards": { "total": 1, "successful": 1, "failed": 0 }, "hits": { "total": 2, "max_score": 1, "hits": [ { "_index": "posts", "_type": "post", "_id": "1", "_score": 1, "_source": { "id": 1, "title": "jkhjkh", "context": "jkhjkhhg", "category_id": 1, "created_at": "2017-09-28T15:31:37.000Z", "updated_at": "2017-09-28T15:31:37.000Z" } }, { "_index": "posts", "_type": "post", "_id": "2", "_score": 1, "_source": { "id": 2, "title": "ju", "context": "yjyj", "category_id": 1, "created_at": "2017-09-28T15:34:50.000Z", "updated_at": "2017-09-28T15:34:50.000Z" } } ] } }

Le nom de la catégorie ne s'enregistre pas et je ne sais pas faire une recherche avec le nom de la catégorie. On dirais qu'il utilise le mapping automatique.

Aucune réponse