elasticsearch中query_string的特殊字符

时间:2016-08-30 13:59:03

标签: elasticsearch elasticsearch-2.0

假设我有一个索引&我在这个声明中添加了一份文件:

POST /temp/item
{
    "text": "dave@domain.com dave@do-main.com one,two three:four"
}

我想要一些查询语句来返回此文档,例如:

  1. *@domain*
  2. *@do-*
  3. one,two
  4. three:four - >这实际上会产生错误
  5. 每个都由类似于此的声明选择:

    GET /temp/item/_search
    {
     "query": {
        "bool": {
          "must": [
            {
              "query_string": {
                "query": "*@domain*",
                "allow_leading_wildcard": "true",
                "default_operator": "AND"
              }
            }
          ]
        }
      }
    }
    

    他们都没有回来。

    我理解原因是分析器设置为standard,它将文本按任何字边界分开。所以我认为我必须将分析仪更改为whitespace,如下所示:

    PUT /temp
    {
      "mappings": {
        "item" : {
          "properties" : {
            "text" : {
              "type" :    "string",
              "analyzer": "whitespace"
            }
          }
        }
      }
    }
    

    这样做并没有解决问题。该声明均未归还该文件。

    问题

    1. 如何配置索引或更改查询语句,以便捕获上述所有示例。
    2. 为什么在我将分析器更改为“空白”之后,Elasticsearch没有返回文档?

1 个答案:

答案 0 :(得分:0)

几乎您需要明确指定要匹配的query_string的“字段”。 可以使用default_fieldfields选项指定案例

示例:

{
 "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "query": "*@domain*",
            "fields": [
               "text"
            ], 
            "allow_leading_wildcard": "true",
            "default_operator": "AND"
          }
        }
      ]
    }
  }
}

如果未指定任何内容,query_string将使用_all字段。

2)three:four需要用双引号括起来,否则会被解释为field:three匹配query:four 例如:

{
 "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "query": "\"three:four\"",
            "fields": [
               "text"
            ], 
            "allow_leading_wildcard": "true",
            "default_operator": "AND"
          }
        }
      ]
    }
  }
}