2014-07-06 41 views
1

我有一些Twitter数据,我想与之合作。我希望能够搜索一个名字。当试图生成'名字'和'_id'的时候,我遇到了一些麻烦。elasticsearch:_id字段的特殊行为?

第一,我创​​建了分析:

curl -XPUT 'localhost:9200/twitter_users' -d ' 
{ 
    "settings": { 
     "analysis": { 
      "analyzer": { 
       "str_search_analyzer": { 
        "tokenizer": "keyword", 
        "filter": [ 
         "lowercase" 
        ] 
       }, 
       "str_index_analyzer": { 
        "tokenizer": "keyword", 
        "filter": [ 
         "lowercase", 
         "ngram" 
        ] 
       } 
      }, 
      "filter": { 
       "ngram": { 
        "type": "ngram", 
        "min_gram": 3, 
        "max_gram": 20 
       } 
      } 
     } 
    } 
}' 

然后我定义我的映射:

curl -XPUT 'http://localhost:9200/twitter_users/users/_mapping' -d ' 
{ 
    "users": { 
     "type" : "object", 
     "properties": { 
      "_id": { 
       "type": "string", 
       "copy_to": "id" 
      }, 
      "id": { 
       "type": "string", 
       "search_analyzer": "str_search_analyzer", 
       "index_analyzer": "str_index_analyzer", 
       "index": "analyzed" 
      }, 
      "name": { 
       "type": "multi_field", 
       "fields": { 
        "name": { 
         "type": "string", 
         "index": "not_analyzed" 
        }, 
        "ngrams": { 
         "type": "string", 
         "search_analyzer": "str_search_analyzer", 
         "index_analyzer": "str_index_analyzer", 
         "index": "analyzed" 
        } 
       } 
      } 
     } 
    } 
}' 

,并插入一些测试数据:

curl -XPUT "localhost:9200/twitter_users/users/johndoe" -d '{ 
    "_id" : "johndoe", 
    "name" : "John Doe" 
}' 

curl -XPUT "localhost:9200/twitter_users/users/janedoe" -d '{ 
    "_id" : "janedoe", 
    "name" : "Jane Doe" 
}' 

查询的名字让我的预期成果:

curl -XPOST "http://localhost:9200/twitter_users/users/_search" -d '{ 
    "query": { 
     "match": { 
      "name.ngrams": "doe" 
     } 
    } 
}' 

,但查询的ID没有给我的结果:

curl -XPOST "http://localhost:9200/twitter_users/users/_search" -d '{ 
    "query": { 
     "match": { 
      "id": "doe" 
     } 
    } 
}' 

我也测试,以_id多场就像我的名字一样。但是那也行不通。

是_id行为不同于其他领域?或者我在这里做错了什么?

编辑:使用elasticsearch v1.1.2并从一个河流插件中将数据从mongodb中提取出来。

感谢您的帮助

米尔科

+0

我在这里有同样的问题..试图将分析器添加到_id字段。你有没有解决这个问题? – yash

+0

'_id'字段不能在elasticsearch中配置,请检查[this](https://www.elastic.co/blog/great-mapping-refactoring#meta-fields) – Rohanil

回答

0

貌似“copy_to”是问题,但为什么不插入“身份证”值到直接的“ID”字段?

curl -XPUT "localhost:9200/twitter_users/users/johndoe" -d '{ 
    "id" : "johndoe", 
    "name" : "John Doe" 
}' 

curl -XPUT "localhost:9200/twitter_users/users/janedoe" -d '{ 
    "id" : "janedoe", 
    "name" : "Jane Doe" 
}' 
+0

好吧,我应该说我是使用一个河流插件来获取数据(在这种情况下是从mongodb)。除非在这个步骤中有一个简单的方法来产生新的领域,否则我无法按照你的方式去做。 – MirkoMachine

相关问题