代码之家  ›  专栏  ›  技术社区  ›  diplosaurus

弹性搜索索引与搜索时间分析器

  •  1
  • diplosaurus  · 技术社区  · 8 年前

    遇到了一个问题,使我觉得我不完全理解ElasticSearch5.5中的索引与搜索时间分析。

    假设我有一个基本指数 name 和A state . 为了简单起见,我设置了 al => alabama 作为唯一的同义词。

    PUT people
    {
      "mappings": {
        "person": {
          "properties": {
            "name": {
              "type": "text"
            },
            "state": {
              "type": "text",
              "analyzer": "us_state"
            }
          }
        }
      },
      "settings": {
        "analysis": {
          "filter": {
            "state_synonyms": {
              "type": "synonym",
              "synonyms": "al => alabama"
            }
          },
          "analyzer": {
            "us_state": {
              "filter": [
                "standard",
                "lowercase",
                "state_synonyms"
              ],
              "type": "custom",
              "tokenizer": "standard"
            }
          }
        }
      }
    }
    

    我的理解是当我索引一个文档时 状态 字段数据将作为扩展的同义词表单进行索引。这可以在运行时进行测试:

    GET people/_analyze
    {
      "text": "al",
      "field": "state"
    }
    

    又回来了

    {
      "tokens": [
        {
          "token": "alabama",
          "start_offset": 0,
          "end_offset": 2,
          "type": "SYNONYM",
          "position": 0
        }
      ]
    }
    

    看起来不错,让我们为文档编制索引:

    POST people/person
    {
      "name": "dave",
      "state": "al"
    }
    

    并执行搜索:

    GET people/person/_search
    {
      "query": {
        "bool": {
          "should": [
            {
              "term": {
                "state": "al"
              }
            }
          ]
        }
      }
    }
    

    不返回任何内容:

    {
      "took": 3,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
      },
      "hits": {
        "total": 0,
        "max_score": null,
        "hits": []
      }
    }
    

    我希望 al 在我寻找的过程中 us_state 分析并匹配我的文档。但是,如果将查询更改为:

    "term": { "state": "alabama" }

    1 回复  |  直到 8 年前
        1
  •  2
  •   Val    8 年前

    这是因为你用了 term 不分析输入的查询。你应该把它改成 match 而是询问,一切都会好起来的

    GET people/person/_search
    {
      "query": {
        "bool": {
          "should": [
            {
              "match": {
                "state": "al"
              }
            }
          ]
        }
      }
    }
    
    推荐文章