【问题标题】:Elasticsearch not working with 'not_analyzed' indexElasticsearch 不使用“not_analyzed”索引
【发布时间】:2016-05-08 06:02:42
【问题描述】:

我无法弄清楚为什么 elasticsearch 不使用 not_analysed 索引进行搜索。我的模型中有以下设置,

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"
          }
        }
      }
    }
  }
}

但问题是,当我对未分析的字段(姓名和电子邮件,因为我希望它们不被分析)进行搜索时,它只搜索完整的单词。就像下面的示例一样,它应该返回 John、Johny 和 Tiger,所有 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":"john@monkeyofdoom.com","name":"john baba","contact_number":null}
    },
    {
          "_index" : "users-development",
          "_type" : "user",
          "_id" : "671",
          "_score" : 1.0,
          "_source":{"id":671,"email":"human@monkeyofdoom.com","name":"Johny Rocket","contact_number":null}
        }

    , {
          "_index" : "users-development",
          "_type" : "user",
          "_id" : "736",
          "_score" : 1.0,
          "_source":{"id":736,"email":"tiger@monkeyofdoom.com","name":"tiger sherof", "contact_number":null}
        } ]

请有任何建议。

【问题讨论】:

  • 上述查询中的 user_type 是什么?
  • 您是如何编制索引的? “约翰”还是“约翰”?你能告诉我们你认为应该匹配的文件吗?
  • @ChintanShah25 添加了应该匹配的文档
  • @ChintanShah25 感谢您的建议,实际上它只是搜索部分单词,例如名称是 John Baba 和 Johny Rocket,查询字符串是 John -- 它应该返回 2 条记录,但它只返回一条记录,即约翰。任何建议谢谢
  • 查看您的文档,您不会得到任何结果。你的要求是什么?什么应该匹配,什么不应该匹配?还有 johny 会如何匹配 john?

标签: ruby-on-rails-4 elasticsearch elasticsearch-rails elasticsearch-model


【解决方案1】:

我认为将keyword toknizerlowercase 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"
        }
      }
    }
  }
}

现在 john* 将匹配 johny。如果您有各种要求,您应该使用multi-fieldsterms query for john 不会给你 john baba,因为在倒排索引中没有标记为 john。您可以在一个字段上使用标准分析器,在另一个字段上使用关键字分析器。

【讨论】:

    【解决方案2】:

    根据文档term query

    术语查询查找包含倒排索引中指定的确切术语的文档。

    您正在搜索john,但您的文档中没有一个包含john,即为什么您没有得到任何结果。您可以在您的字段analysed 然后申请query string 或搜索确切的术语。

    更多详情请参考https://www.elastic.co/guide/en/elasticsearch/reference/2.x/query-dsl-term-query.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-29
      • 2015-12-24
      • 1970-01-01
      • 2017-01-10
      • 1970-01-01
      • 2016-02-18
      • 2015-01-31
      • 1970-01-01
      相关资源
      最近更新 更多