弹性搜索搜索案例不敏感的连字符白帽

时间:2015-07-10 07:16:32

标签: elasticsearch whitespace case-insensitive

我想搜索由连字符,白帽和不区分大小写组成的单词或单词。

这是索引结构:

body' => array(
  'settings' => array(
    "analysis" => array(
      "analyzer" => array(
        "default" => array(
          "type" => "custom",
          'tokenizer' => "whitespace",
          "filter" => array("lowercase")
        ),
        "autoCompleteSearch" => array(
          "tokenizer" => "standard",
          "filter" => array("lowercase", "trim", "standard")
       )
    )
  )
),
'mappings' => array(
  "myindex" => array(
    'properties' => array(
      'Id' => array(
        'type'  => 'integer',
        'index' => 'not_analyzed'
      ),
      'Title' => array(
        'type'  => 'string',
        'fields'=> array(
          raw' => array(
            'type'  => 'string',
            "search_analyzer" => "autoCompleteSearch",
            'index' => 'not_analyzed'
          )
        )
      ),
      'Content' => array(
        'type'  => 'string',
        'fields'=> array(
          raw' => array(
            'type'  => 'string',
            "search_analyzer" => "autoCompleteSearch",
            'index' => 'not_analyzed'
          )
        )
      )
    )
  )
)

以下是查询:

"query" : {
  "query_string": {
    "query": "*t-shir red*",
    "lowercase_expanded_terms": false,
    "fields": [
       "Title.raw",
       "Content.raw"
     ]
   }
 }

我想找到T恤RED,就像我在搜索领域写的那样。 " t恤红色"。

非常感谢你!

1 个答案:

答案 0 :(得分:0)

使用自定义原始小写子字段的类似内容:

{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_keyword_lowercase_analyzer": {
          "type": "custom",
          "tokenizer": "keyword",
          "filter": [
            "lowercase"
          ]
        }
      }
    }
  }, 
  "mappings": {
    "myindex": {
      "_source": {
        "enabled": true
      },
      "properties": {
        "Title": {
          "type": "string",
          "fields": {
            "raw": {
              "type": "string",
              "index": "not_analyzed"
            },
            "raw_lowercase": {
              "type": "string",
              "analyzer": "my_keyword_lowercase_analyzer"
            }
          }
        },
        "Content": {
          "type": "string",
          "fields": {
            "raw": {
              "type": "string",
              "index": "not_analyzed"
            },
            "raw_lowercase": {
              "type": "string",
              "analyzer": "my_keyword_lowercase_analyzer"
            }
          }
        },
        "Image": {
          "type": "string",
          "analyzer": "standard"
        }
      }
    }
  }
}

查询:

{
  "query": {
    "query_string": {
      "query": "*t-shirt\\ red*",
      "lowercase_expanded_terms": false, 
      "fields": [
        "Title.raw_lowercase",
        "Content.raw_lowercase"
      ]
    }
  }
}