使用Elasticsearch进行“您是不是故意”搜索

时间:2018-10-03 12:19:01

标签: elasticsearch

我正在使用Elasticsearch实施“您是不是要”技术。经过研究后,我发现短语提示器仅用于此目的。但是我面临一些问题。 我的地图文件:

{
  "settings": {
    "index": {
      "number_of_shards": 1,
      "analysis": {
        "analyzer": {
          "trigram": {
            "type": "custom",
            "tokenizer": "standard",
            "filter": ["standard", "shingle"]
          },
          "reverse": {
            "type": "custom",
            "tokenizer": "standard",
            "filter": ["standard", "reverse"]
          }
        },
        "filter": {
          "shingle": {
            "type": "shingle",
            "min_shingle_size": 2,
            "max_shingle_size": 3
          }
        }
      }
    }
  },
  "mappings": {
    "test": {
      "properties": {
        "title": {
          "type": "text",
          "fields": {
            "trigram": {
              "type": "text",
              "analyzer": "trigram"
            },
            "reverse": {
              "type": "text",
              "analyzer": "reverse"
            }
          }
        }
      }
    }
  }
}

我的文档是:

{"title": "nobel prize"}

我的搜索查询是:

{
  "suggest": {
    "text": "noble prze",
    "simple_phrase": {
      "phrase": {
        "field": "title.trigram",
        "size": 1,
        "gram_size": 3,
        "direct_generator": [ {
          "field": "title.trigram",
          "suggest_mode": "always"
        } ],
        "highlight": {
          "pre_tag": "<em>",
          "post_tag": "</em>"
        }
      }
    }
  }
}

如果要搜索的文本中只有一个字符不匹配,则查询工作正常。否则,建议的短语只能更正一个字符。 输出为:

{
    "took": 6,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 0,
        "max_score": 0,
        "hits": []
    },
    "suggest": {
        "simple_phrase": [
            {
                "text": "noble prze",
                "offset": 0,
                "length": 10,
                "options": [
                    {
                        "text": "nobel prze",
                        "highlighted": "<em>nobel</em> prze",
                        "score": 0.16992487
                    }
                ]
            }
        ]
    }
}

0 个答案:

没有答案