弹性搜索-example.com与example

时间:2019-01-07 09:16:52

标签: elasticsearch elasticsearch-5 elasticsearch-dsl-py

我们正在使用弹性搜索5.6.9在Django 1.11服务器上提供搜索功能。

如果我为数据example.com编制索引并搜索example.com,则得到搜索结果,但是如果我搜索example,则没有任何搜索结果。

理想情况下,我希望exampleexample.com都可以工作并返回相同的搜索结果。

我该如何获得这种行为?

我认为我必须将分析器和令牌生成器更改为相同的

使用simple分析器似乎是正确的选择。 例如: POST _analyze { "analyzer": "simple", "text": "example.com" } 返回examplecom作为单独的令牌 { "tokens": [ { "token": "example", "start_offset": 0, "end_offset": 7, "type": "word", "position": 0 }, { "token": "com", "start_offset": 8, "end_offset": 11, "type": "word", "position": 1 } ] }

我认为在索引数据时和在查询时必须在查询中设置相同的分析器/令牌器。

我尝试按照以下说明将analyzer设置为simplehttps://www.elastic.co/guide/en/elasticsearch/reference/5.6/analyzer.html 但是,现在我仍然必须搜索example.com而不是example,现在我在搜索结果的highlight中什么也看不到。

我对这是如何产生搜索结果而不是highlight感到困惑。

我在这里完全不在基地吗?

1 个答案:

答案 0 :(得分:0)

也许这个例子对您有帮助:

映射

x2=(5*y-0.5*x1-1.5*x3)

添加文档

PUT /so54071449
{
  "mappings": {
    "doc": {
      "properties": {
        "url": {
          "type": "text",
          "term_vector": "with_positions_offsets",
          "fields": {
            "simple": {
              "type": "text",
              "analyzer": "simple",
              "search_analyzer": "simple",
              "term_vector": "with_positions_offsets"
            }
          }
        }
      }
    }
  }
}

通过POST /so54071449/doc { "url": "example.com" }

搜索
example

GET /so54071449/_search { "query": { "multi_match": { "query": "example", "fields": ["url", "url.simple"] } }, "highlight": { "fields": { "url": { "matched_fields": [ "url", "url.simple" ] } } } } 的结果

example

通过{ "took": 1, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 1, "max_score": 0.25811607, "hits": [ { "_index": "so54071449", "_type": "doc", "_id": "AWgoEwDT2HOwokHu0yvd", "_score": 0.25811607, "_source": { "url": "example.com" }, "highlight": { "url": [ "<em>example</em>.com" ] } } ] } }

搜索
example.com

GET /so54071449/_search { "query": { "multi_match": { "query": "example.com", "fields": ["url", "url.simple"] } }, "highlight": { "fields": { "url": { "matched_fields": [ "url", "url.simple" ] } } } } 的结果

example.com

我已使用multi fields{ "took": 4, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 1, "max_score": 0.51623213, "hits": [ { "_index": "so54071449", "_type": "doc", "_id": "AWgoEwDT2HOwokHu0yvd", "_score": 0.51623213, "_source": { "url": "example.com" }, "highlight": { "url": [ "<em>example.com</em>" ] } } ] } } 字段上应用了两个分析器(standard是默认设置,在url子字段上应用了simple )和matched_fields to combine highlighting的结果来自url.simpleurl到一个字段中。