如何使用elasticsearch

时间:2017-05-04 13:58:39

标签: php search elasticsearch lucene

我有一个名为“title_search”的字段,其值为“apple iphone 6s”。我正在使用shingle类型来索引数据字段。下面是映射和设置。

设置

{
    "settings": {
        "analysis": {
            "analyzer": {
                "bigram_combiner": {
                    "tokenizer": "standard",
                    "filter": [
                        "lowercase",
                        "custom_shingle",
                        "my_char_filter"
                    ]
                }
            },
            "filter": {
                "custom_shingle": {
                    "type": "shingle",
                    "min_shingle_size": 2,
                    "max_shingle_size": 3,
                    "output_unigrams": true
                },
                "my_char_filter": {
                    "type": "pattern_replace",
                    "pattern": " ",
                    "replacement": ""
                }
            }
        }
    }
}

映射

{
    "products": {
        "properties": {
            "title_search" : {
                  "type" : "string",
                  "analyzer":"bigram_combiner"
            }
        }
    }
}

当我分析该字段时,它给出了以下结果。

{
  "tokens": [
    {
      "token": "apple",
      "start_offset": 0,
      "end_offset": 5,
      "type": "<ALPHANUM>",
      "position": 0
    },
    {
      "token": "appleiphone",
      "start_offset": 0,
      "end_offset": 12,
      "type": "shingle",
      "position": 0,
      "positionLength": 2
    },
    {
      "token": "appleiphone6s",
      "start_offset": 0,
      "end_offset": 15,
      "type": "shingle",
      "position": 0,
      "positionLength": 3
    },
    {
      "token": "iphone",
      "start_offset": 6,
      "end_offset": 12,
      "type": "<ALPHANUM>",
      "position": 1
    },
    {
      "token": "iphone6s",
      "start_offset": 6,
      "end_offset": 15,
      "type": "shingle",
      "position": 1,
      "positionLength": 2
    },
    {
      "token": "6s",
      "start_offset": 13,
      "end_offset": 15,
      "type": "<ALPHANUM>",
      "position": 2
    }
  ]
}

它提供了一些结果。但如果用户搜索 apple6s ,则不会给出任何结果。设置是否有任何更改,因此apple6s也将使用shingle类型进行索引。

0 个答案:

没有答案