0

我无法弄清楚为什么elasticsearch不用not_analysed索引进行搜索。我在我的模型中有以下设置,Elasticsearch不处理“not_analyzed”索引

settings index: { number_of_shards: 1 } do 
     mappings dynamic: 'false' do 
     indexes :id 
     indexes :name, index: 'not_analyzed' 
     indexes :email, index: 'not_analyzed' 
     indexes :contact_number 
     end 
    end 

    def as_indexed_json(options = {}) 
     as_json(only: [ :id, :name, :username, :user_type, :is_verified, :email, :contact_number ]) 
    end 

而我在elasticsearch的映射是正确的,如下所示。

{ 
    "users-development" : { 
    "mappings" : { 
     "user" : { 
     "dynamic" : "false", 
     "properties" : { 
      "contact_number" : { 
      "type" : "string" 
      }, 
      "email" : { 
      "type" : "string", 
      "index" : "not_analyzed" 
      }, 
      "id" : { 
      "type" : "string" 
      }, 
      "name" : { 
      "type" : "string", 
      "index" : "not_analyzed" 
      } 
     } 
     } 
    } 
    } 
} 

但问题是,当我就没有分析领域的搜索(姓名和电子邮件,因为我希望他们可以不分析),它只能在完整的单词进行搜索。就像在下面的例子中,它应该有约翰,约翰和虎的所有3条记录。但它只返回2条记录。

我正在寻找如下

settings = { 
    query: { 
     filtered: { 
     filter: { 
      bool: { 
      must: [ 
       { terms: { name: [ "john", "tiger" ] } }, 
      ] 
      } 
     } 
     } 
    }, 
    size: 10 
    } 

    User.__elasticsearch__.search(settings).records 

这是我如何在回调after_save创建我的用户对象上的索引,

User.__elasticsearch__.client.indices.create(
       index: User.index_name, 
       id: self.id, 
       body: self.as_indexed_json, 
      ) 

一些应符合

[{ 
     "_index" : "users-development", 
     "_type" : "user", 
     "_id" : "670", 
     "_score" : 1.0, 
     "_source":{"id":670,"email":"[email protected]","name":"john baba","contact_number":null} 
    }, 
    { 
      "_index" : "users-development", 
      "_type" : "user", 
      "_id" : "671", 
      "_score" : 1.0, 
      "_source":{"id":671,"email":"hu[email protected]","name":"Johny Rocket","contact_number":null} 
     } 

    , { 
      "_index" : "users-development", 
      "_type" : "user", 
      "_id" : "736", 
      "_score" : 1.0, 
      "_source":{"id":736,"email":"[email protected]","name":"tiger sherof", "contact_number":null} 
     } ] 
文档

请提出任何建议。

+0

以上查询中的user_type是什么? – Richa

+0

你是如何编制索引的? “约翰”还是“约翰”?你能告诉我们你认为应该匹配的文件吗? – ChintanShah25

+0

@ ChintanShah25添加了应该匹配的文档 –

回答

0

我想你会得到期望与keyword toknizer结果与lowercase filter相结合,而不是使用not_analyzed

原因john*不匹配Johny是由于区分大小写。 此设置将工作

{ 
    "settings": { 
    "analysis": { 
     "analyzer": { 
     "keyword_analyzer": { 
      "type": "custom", 
      "filter": [ 
      "lowercase" 
      ], 
      "tokenizer": "keyword" 
     } 
     } 
    } 
    }, 
    "mappings": { 
    "my_type": { 
     "properties": { 
     "name": { 
      "type": "string", 
      "analyzer": "keyword_analyzer" 
     } 
     } 
    } 
    } 
} 

现在约翰*将匹配佐尼。如果您有各种要求,您应该使用multi-fieldsterms query约翰将不会给你约翰巴巴作为里面倒排索引没有令牌为约翰。您可以在一个字段上使用标准分析仪,在其他字段上使用关键字分析仪