2014-08-27 34 views
0

我们的系统以下列格式存储帐户:acct:[email protected] 但是对于许多搜索,我们只需要用户名,因此对于用户创建的备忘录,我已决定使用用户领域的multi_field这样的:在多字段中使用多个匹配查询不起作用

{ 
    'text': { 
    'type': 'string' 
    } 
    'user': { 
    'type': 'multi_field', 
    'path': 'just_name', 
    'fields': { 
     'user': { 
     'type': 'string', 
     'index': 'analyzed', 
     'analyzer': 'lower_keyword' 
     }, 
     'username': { 
     'type': 'string', 
     'index': 'analyzed', 
     'analyzer': 'username' 
     } 
    } 
    } 
} 

和其他设置:

__settings__ = { 
    'analysis': { 
     'tokenizer': { 
      'username': { 
       'type': 'pattern', 
       'group': 1, 
       'pattern': '^acct:(.+)@.*$' 
      } 
     }, 
     'analyzer': { 
      'lower_keyword': { 
       'type': 'custom', 
       'tokenizer': 'keyword', 
       'filter': 'lowercase' 
      }, 
      'username': { 
       'tokenizer': 'username', 
       'filter': 'lowercase' 
      } 
     } 
    } 
} 

现在,如果我做一个查询的用户名它的工作原理。即如果我有以下用户:acct:[email protected]

和我做这样的查询:

{ 
    "query": { 
    "bool": { 
     "must": [ 
     { 
      "terms": { 
      "username": [ 
       "testuser" 
      ] 
      } 
     } 
     ], 
     "minimum_number_should_match": 1 
    } 
    }, 
    "size": 50 
} 

它的工作原理(我知道这是可以做到很容易,但是这是一个系统生成的查询)。

但是,我需要进行搜索,在文本和用户名字段中查找字符串。 我已决定为此使用multi-match查询。

{ 
    "query": { 
    "bool": { 
     "must": [ 
     { 
      "multi_match": { 
      "operator": "and", 
      "query": "testuser", 
      "type": "cross_fields", 
      "fields": [ 
       "text", 
       "username" 
      ] 
      } 
     } 
     ], 
     "minimum_number_should_match": 1 
    } 
    }, 
    "size": 50 
} 

现在的问题是,这个查询不适用于用户名字段。它适用于文本字段,以及其他字段(如果我包含它们),但不会为用户名字段返回任何结果。

你能帮我什么我做错了吗?

回答

0

我忘记了用户名分析器也会标记我的搜索匹配/多重匹配查询。这样字符串'testuser'被分析并且它产生了零令牌。

因此,解决方案是将用户名的字段映射更改为:

'username': { 
    'type': 'string', 
    'index': 'analyzed', 
    'index_analyzer': 'username', 
    'search_analyzer': 'lower_keyword' 
} 

现在无论查询工作。