简体   繁体   English

elasticsearch搜索过滤器等于问题

[英]elasticsearch search filter equals issue

I have an index of person database with the below mapping. 我有以下映射的人员数据库索引。

{
    "person" : {
                "sex"       : { "type" : "string" },
                "dob"      : { "type" : "string" },         
                "fname"      : { "type" : "string" },
                "lname"      : { "type" : "string" },
                "phone"      : { "type" : "string" }
      }
}

My need is to find all matching entries with multiple conditional clauses. 我需要找到带有多个条件子句的所有匹配条目。

Dob + phone + sex (OR) fname + lname + dob Dob +电话+性别(OR)fname + lname + dob

How do I create a query or filter (using bool) for the above condition. 如何为上述条件创建查询或过滤器(使用布尔值)。 Also I need to query or filter case-insensitive. 另外,我需要查询或过滤不区分大小写的内容。

Any ideas? 有任何想法吗?

Thanks 谢谢

Nesting two sets of must queries inside a should query will meet your requirements, see bool for more information: 嵌套两套必须查询的内查询将满足您的要求,请参阅布尔的详细资料:

curl -XGET 'http://localhost:9200/people/person/_search?pretty' -d '{
   "query": {
      "bool": {
         "should": [
             {"bool": {
                "must": [
                  {"match": { "sex" : "male" }},
                  {"match": { "dob" : "2000-11-14" }},
                  {"match": { "phone" : "1234 67889" }}
                ]
              }
             },
             {"bool": {
                "must": [
                  {"match": { "fname" : "bob" }},
                  {"match": { "dob" : "2000-11-14" }},
                  {"match": { "lname" : "smith" }}
                ]
              }
             }
            ]
        }
     }
 }'

Also I need to query or filter case-insensitive. 另外,我需要查询或过滤不区分大小写的内容。

the standard analyzer will index the data in lowercase - the match query will apply the same analyzer to your search term. 标准分析器会以小写字母索引数据- 匹配查询会将相同的分析器应用于您的搜索字词。

Also - it would be a good idea to store you DOB as a date. 另外-将DOB作为日期存储将是一个好主意。

You should change your mapping into a nested object, than you can use the nested filter or query. 您应该将映射更改为嵌套对象,然后才能使用嵌套过滤器或查询。 Since you want to do it case insensitive you would need the match query within the nested query. 由于您要区分大小写,因此需要在嵌套查询中使用匹配查询。

More information about the mapping: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-nested-type.html#mapping-nested-type 有关映射的更多信息: http : //www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-nested-type.html#mapping-nested-type

{
    "type1" : {
        "properties" : {
            "person" : {
                "type" : "nested",
                "properties": {
                    "sex" : {"type": "string" },
                    "dob"  : {"type": "string" }
                }
            }
        }
    }
}

Then create the nested query: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html#query-dsl-nested-query 然后创建嵌套查询: http : //www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html#query-dsl-nested-query

{
    "nested" : {
        "path" : "person",
        "query" : {
            "bool" : {
                "must" : [
                    {
                        "match" : {"person.dob" : "xx"}
                    },
                    {
                        "range" : {"person.sex" : "male"}
                    }
                ]
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM