2016-10-13 19 views
2

我试图做一个简单的GET请求的几种不同的方式,在两个不同的属性过滤,例如查询必须完全,2个场不分析

"query": { 
    "filtered": { 
     "filter": { 
      "bool": { 
       "must": [ 
        { 
         "term": { 
          "email": "[email protected]" 
         } 
        }, 
        { 
         "term": { 
          "password": "bb3810356e9b60cf6..." 
         } 
        } 
       ] 
      } 
     }, 
     "query": { 
      "match_all": [] 
     } 
    } 
} 

的问题是,我得到没有回报。据我了解,这是因为ElasticSearch分析电子邮件字段,使查询失败。因此,如果我使用术语erik.landvall而不是完整的电子邮件地址,它将与文档匹配 - 这证实了这是怎么回事。

我可以在创建索引时将属性定义为type:stringindex:not_analyzed。但是如果我想在不同的环境中搜索电子邮件属性呢?因此,在我看来,应该指定我想过滤查询中属性的实际值。然而,我可能无法找到这样的查询的外观。

查询时是否可以强制Elasticsearch使用“not_analyze”?如果是这样,那么怎么样?

+0

我没有很多时间来回答你的问题,但这里看看:http://stackoverflow.com/questions/40007971/case-insensitive-elasticsearch-with -uppercase-or-lowercase/40009671#40009671我想你也会对此感兴趣:https://www.elastic.co/guide/en/elasticsearch/reference/current/multi-fields.html - 你可以为一个输入json字段指定具有不同映射的多个字段。如果没有人会更深入地回答你的问题,我明天就会做。 –

+0

@Adam这两个建议似乎都是为了在创建索引时在查询之前指定映射。我的问题是,如果可以在不更改索引的情况下指定它。 – superhero

+0

我从来没有使用它,但有可能在查询级别指定分析器,但并非所有查询都允许。如果你不想分析输入字符串,那么你只需要指定关键字分析器。你可以在这里阅读更多:http://stackoverflow.com/questions/32565662/elastic-search-match-query-with-analyzer-is-not-working或在这里看看(分析器字段在查询中):https: //www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html –

回答

1

您可以使用scripting来达到此目的。您将不得不直接访问您使用_source存储的JSON。请尝试以下查询

{ 
    "query": { 
    "bool": { 
     "filter": { 
     "script": { 
      "script": { 
       "inline" : "_source.email==param1 && _source.password==param2", 
       "params" : { 
        "param1" : "[email protected]", 
        "param2" : "bb3810356e9b60cf6" 
       } 
      } 
     } 
     } 
    } 
    } 
} 

您将需要enable dynamic scripting。将script.inline: on添加到您的yml文件并重新启动节点。

如果这种查询相当规律,那么重新索引数据会比其他人在评论中提出的要好得多。

1

不可能打开/关闭分析或不分析,通过using fields将其转换为分析所需的分析方法。

curl -XPUT 'localhost:9200/my_index?pretty' -d' 
{ 
    "mappings": { 
    "my_type": { 
     "properties": { 
     "city": { 
      "type": "string", 
      "fields": { 
      "raw": { 
       "type": "string", 
       "index": "not_analyzed" 
      } 
      } 
     } 
     } 
    } 
    } 
}' 
curl -XPUT 'localhost:9200/my_index/my_type/1?pretty' -d' 
{ 
    "city": "New York" 
}' 
curl -XPUT 'localhost:9200/my_index/my_type/2?pretty' -d' 
{ 
    "city": "York" 
}' 
curl -XGET 'localhost:9200/my_index/_search?pretty' -d' 
{ 
    "query": { 
    "match": { 
     "city": "york" 
    } 
    }, 
    "sort": { 
    "city.raw": "asc" 
    }, 
    "aggs": { 
    "Cities": { 
     "terms": { 
     "field": "city.raw" 
     } 
    } 
    } 

}”

+0

这不可能在yml-settings文件中作为默认来完成吗?所以'not_analyzed'总是存在? – superhero

+0

@ErikLandvall我看到的唯一方法是定义动态模板https://www.elastic.co/guide/en/elasticsearch/reference/current/dynamic-templates.html –