首页 > 解决方案 > Elastic 没有找到 1 个单词的结果

问题描述

我不太了解使用 elasticsearch 得到的结果。我的研究结果似乎不一致。

通过这个搜索

{"query": {
    "multi_match" : {
      "query" : "tarif new",
      "fields" : [ "titles^3", "texts" ] 
    }
  }}

我有 2 个结果:

"titles": "Tarif New 208 (07/2019), Tarif New 208, Tarif New 208 , FREDERIC, f.mignon@lab360.be, , ",

"titles": "Tarif Traveller (07/2019), Traveller, Tarif New Traveller, , , , ",      

但是通过这个搜索:

{
  "query": {
    "multi_match" : {
      "query" : "tarif",
      "fields" : [ "titles^3", "texts" ] 
    }
  }
}

我只有 1 个结果:

"titles": "Tarif Traveller (07/2019), Traveller, Tarif New Traveller, , , , ",      

我的索引是由 Elasticsearch .NET 创建的

var indexDescriptor = new CreateIndexDescriptor(indexName)
    .Mappings(ms => ms
    .Map<EsContent>(m => m.AutoMap()
     .Properties(p => p
        .Text(t => t
            .Name(n => n.Titles)
            .Fields(ff => ff

                .Keyword(k => k
                    .Name("keyword")
                    .IgnoreAbove(256)
                )


            )
        )
        .Text(t => t
            .Name(n => n.Texts)
            .Fields(ff => ff

                .Keyword(k => k
                    .Name("keyword")
                    .IgnoreAbove(256)
                )
            )
        )
        .Text(t => t
            .Name(n => n.Tags)
            .Fields(ff => ff

                .Keyword(k => k
                    .Name("keyword")
                    .IgnoreAbove(256)
                )
            )
        )
    )

));

我真的不明白这些结果。当我使用“关税”一词进行搜索时,为什么没有出现“Price New 208 (07/2019)”作为结果?

你可以在这里找到 http://localhost:9200/content_1_be_fr?pretty的结果:

{
"content_1_be_fr": {
    "aliases": {},
    "mappings": {
        "escontent": {
            "properties": {
                "attachment": {
                    "properties": {
                        "author": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "containsMetadata": {
                            "type": "boolean"
                        },
                        "content": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "content_length": {
                            "type": "long"
                        },
                        "content_type": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "date": {
                            "type": "date"
                        },
                        "detect_language": {
                            "type": "boolean"
                        },
                        "indexed_chars": {
                            "type": "long"
                        },
                        "keywords": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "language": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "name": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "title": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        }
                    }
                },
                "content": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "content_Id": {
                    "type": "integer"
                },
                "country_Id": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "createdAt": {
                    "type": "date"
                },
                "documents": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "id": {
                    "type": "integer"
                },
                "language_Id": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "path": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "suggest": {
                    "type": "completion",
                    "analyzer": "simple",
                    "preserve_separators": true,
                    "preserve_position_increments": true,
                    "max_input_length": 50
                },
                "tags": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "texts": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "titles": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                }
            }
        }
    },
    "settings": {
        "index": {
            "creation_date": "1562573917609",
            "number_of_shards": "5",
            "number_of_replicas": "1",
            "uuid": "dVTg6anYTKuqOhaSW68pvw",
            "version": {
                "created": "6050099"
            },
            "provided_name": "content_1_be_fr"
        }
    }
}

}

"tarif" 词搜索的结果 => http://localhost:9200/content_1_be_fr/_search?q=tarif

{
"took": 4,
"timed_out": false,
"_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
},
"hits": {
    "total": 106,
    "max_score": 6.665128,
    "hits": [
        {
            "_index": "content_1_be_fr",
            "_type": "escontent",
            "_id": "515",
            "_score": 6.665128,
            "_source": {
                "id": 515,
                "titles": "Tarif VP (07/2019), Tarif VP, Tarif VP, , , , ",
                "texts": "Tarif VP, , , , , , ",
                "createdAt": "2017-05-16T16:41:37.727",
                "tags": [
                    "Tarif",
                    "Fleet"
                ],
                "content_Id": 0
            }
        },
        {
            "_index": "content_1_be_fr",
            "_type": "escontent",
            "_id": "5024",
            "_score": 6.1476207,
            "_source": {
                "id": 5024,
                "titles": "Tarif Rifter (07/2019), Rifter, Tarif Rifter, , , , ",
                "texts": "Tarif Rifter, , , , , , ",
                "createdAt": "2018-05-30T12:14:37.677",
                "tags": [
                    "Tarif",
                    "Rifter"
                ],
                "content_Id": 0
            }
        },
        {
            "_index": "content_1_be_fr",
            "_type": "escontent",
            "_id": "621",
            "_score": 5.9972153,
            "_source": {
                "id": 621,
                "titles": "Tarif Traveller (07/2019), Traveller, Tarif New Traveller, , , , ",
                "texts": "Tarif New Traveller, , , , , , ",
                "createdAt": "2017-05-16T17:13:44.04",
                "tags": [
                    "Tarif",
                    "Fleet",
                    "Traveller"
                ],
                "content_Id": 0
            }
        },
        {
            "_index": "content_1_be_fr",
            "_type": "escontent",
            "_id": "544",
            "_score": 5.9037256,
            "_source": {
                "id": 544,
                "titles": "Tarif 208 GT (07/2019), tarif 208, Tarif 208 GT, , , , ",
                "texts": "Tarif 208 GT, , , , , , ",
                "createdAt": "2017-05-16T16:52:25.883",
                "tags": [
                    "Tarif",
                    "Fleet",
                    "208",
                    "GT"
                ],
                "content_Id": 0
            }
        },
        {
            "_index": "content_1_be_fr",
            "_type": "escontent",
            "_id": "545",
            "_score": 5.8449397,
            "_source": {
                "id": 545,
                "titles": "Tarif VU (07/2019), Tarif VU, Tarif VU, , , , ",
                "texts": "Tarif VU, , , , , , ",
                "createdAt": "2017-05-16T16:52:45.26",
                "tags": [
                    "Tarif",
                    "Fleet",
                    "VU"
                ],
                "content_Id": 0
            }
        },
        {
            "_index": "content_1_be_fr",
            "_type": "escontent",
            "_id": "510",
            "_score": 5.717454,
            "_source": {
                "id": 510,
                "titles": "Tarif Expert VU (07/2019), Expert VU, Tarif Expert VU, , , , ",
                "texts": "Tarif Expert VU, , , , , , ",
                "createdAt": "2017-05-16T16:40:11.43",
                "tags": [
                    "Tarif",
                    "Fleet",
                    "Expert"
                ],
                "content_Id": 0
            }
        },
        {
            "_index": "content_1_be_fr",
            "_type": "escontent",
            "_id": "512",
            "_score": 5.659007,
            "_source": {
                "id": 512,
                "titles": "Tarif Utility (05/2019), Utility, Tarif Utility, , , , ",
                "texts": "Tarif Utility, , , , , , ",
                "createdAt": "2017-05-16T16:40:49.82",
                "tags": [
                    "Tarif",
                    "Fleet",
                    "Utility"
                ],
                "content_Id": 0
            }
        },
        {
            "_index": "content_1_be_fr",
            "_type": "escontent",
            "_id": "520",
            "_score": 5.659007,
            "_source": {
                "id": 520,
                "titles": "Tarif 508 (07/2019), 508, Tarif 508, , , , ",
                "texts": "Tarif 508, , , , , , ",
                "createdAt": "2017-05-16T16:43:08.4",
                "tags": [
                    "Tarif",
                    "Fleet",
                    "508"
                ],
                "content_Id": 0
            }
        },
        {
            "_index": "content_1_be_fr",
            "_type": "escontent",
            "_id": "526",
            "_score": 5.659007,
            "_source": {
                "id": 526,
                "titles": "Tarif 108 (07/2019), 108, Tarif 108, , , , ",
                "texts": "Tarif 108, , , , , , ",
                "createdAt": "2017-05-16T16:45:04.447",
                "tags": [
                    "Tarif",
                    "Fleet",
                    "108"
                ],
                "content_Id": 0
            }
        },
        {
            "_index": "content_1_be_fr",
            "_type": "escontent",
            "_id": "516",
            "_score": 5.6427865,
            "_source": {
                "id": 516,
                "titles": "Tarif iOn (07/2019), iOn, Tarif iOn, , , , ",
                "texts": "Tarif iOn, , , , , , ",
                "createdAt": "2017-05-16T16:41:49.867",
                "tags": [
                    "Tarif",
                    "Fleet",
                    "iOn",
                    "Electric"
                ],
                "content_Id": 0
            }
        }
    ]
}

}

对于“新关税”=> http://localhost:9200/content_1_be_fr/_search?q=tarif new

{
"took": 3,
"timed_out": false,
"_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
},
"hits": {
    "total": 296,
    "max_score": 10.11692,
    "hits": [
        {
            "_index": "content_1_be_fr",
            "_type": "escontent",
            "_id": "5200",
            "_score": 10.11692,
            "_source": {
                "id": 5200,
                "titles": "Tarif Partner VU (07/2019), Partner VU, Tarif Partner VU, , , , ",
                "texts": "Tarif NEW Partner VU, , , , , , ",
                "createdAt": "2018-08-28T14:51:25.41",
                "tags": [
                    "Tarif",
                    "Fleet",
                    "Partner"
                ],
                "content_Id": 0
            }
        },
        {
            "_index": "content_1_be_fr",
            "_type": "escontent",
            "_id": "522",
            "_score": 9.818497,
            "_source": {
                "id": 522,
                "titles": "Tarif 3008 (07/2019), 3008, Tarif New 3008, , , , ",
                "texts": "Tarif New 3008, , , , , , ",
                "createdAt": "2017-05-16T16:43:46.463",
                "tags": [
                    "Tarif",
                    "Fleet",
                    "3008"
                ],
                "content_Id": 0
            }
        },
        {
            "_index": "content_1_be_fr",
            "_type": "escontent",
            "_id": "5816",
            "_score": 9.40431,
            "_source": {
                "id": 5816,
                "titles": "Tarif New 208 (07/2019), Tarif New 208, Tarif New 208 , FREDERIC, f.mignon@lab360.be, , ",
                "texts": "Tarif, , , MIGNON, , , ",
                "createdAt": "2019-07-01T08:36:19.583",
                "tags": [
                    "Tarif",
                    "Fleet",
                    "208",
                    "NEW 208"
                ],
                "content_Id": 0
            }
        },
        {
            "_index": "content_1_be_fr",
            "_type": "escontent",
            "_id": "550",
            "_score": 9.044639,
            "_source": {
                "id": 550,
                "titles": "Tarif 2008 (07/2019), 2008, Tarif New 2008, , , , ",
                "texts": "Tarif New 2008, , , , , , ",
                "createdAt": "2017-05-16T16:54:07.163",
                "tags": [
                    "Tarif",
                    "Fleet",
                    "2008"
                ],
                "content_Id": 0
            }
        },
        {
            "_index": "content_1_be_fr",
            "_type": "escontent",
            "_id": "621",
            "_score": 8.852159,
            "_source": {
                "id": 621,
                "titles": "Tarif Traveller (07/2019), Traveller, Tarif New Traveller, , , , ",
                "texts": "Tarif New Traveller, , , , , , ",
                "createdAt": "2017-05-16T17:13:44.04",
                "tags": [
                    "Tarif",
                    "Fleet",
                    "Traveller"
                ],
                "content_Id": 0
            }
        },
        {
            "_index": "content_1_be_fr",
            "_type": "escontent",
            "_id": "5572",
            "_score": 8.2428465,
            "_source": {
                "id": 5572,
                "titles": "Tarif New Partner 15/03/2019, Tarif New Partner 15/03/2019, , DAVID, david.delpercio@mpsa.com, , ",
                "texts": ", , , DEL PERCIO, 003223884093, , ",
                "createdAt": "2019-03-15T17:27:55.623",
                "tags": [],
                "content_Id": 0
            }
        },
        {
            "_index": "content_1_be_fr",
            "_type": "escontent",
            "_id": "625",
            "_score": 8.189293,
            "_source": {
                "id": 625,
                "titles": "Tarif 5008 (07/2019), 5008, Tarif New 5008, , , , ",
                "texts": "Tarif New 5008, , , , , , ",
                "createdAt": "2017-05-16T17:14:57.087",
                "tags": [
                    "Tarif",
                    "5008"
                ],
                "content_Id": 0
            }
        },
        {
            "_index": "content_1_be_fr",
            "_type": "escontent",
            "_id": "5277",
            "_score": 7.9583516,
            "_source": {
                "id": 5277,
                "titles": "Tarif 508 SW (07/2019), 508 SW, Tarif 508 SW (07/2019), MARTIN, martin.want@peugeot.com, , ",
                "texts": "Tarif New 508 SW&nbsp;, , , WANT, 003223884092, , ",
                "createdAt": "2018-10-03T11:07:32.933",
                "tags": [
                    "508 SW"
                ],
                "content_Id": 0
            }
        },
        {
            "_index": "content_1_be_fr",
            "_type": "escontent",
            "_id": "620",
            "_score": 7.922105,
            "_source": {
                "id": 620,
                "titles": "Tarif Expert VP (07/2019), Expert VP, Tarif New Expert VP, , , , ",
                "texts": "Tarif New Expert VP, , , , , , ",
                "createdAt": "2017-05-16T17:13:20.587",
                "tags": [
                    "Tarif",
                    "Fleet",
                    "Expert"
                ],
                "content_Id": 0
            }
        },
        {
            "_index": "content_1_be_fr",
            "_type": "escontent",
            "_id": "5689",
            "_score": 7.673352,
            "_source": {
                "id": 5689,
                "titles": "New Partner : Tarif 01/05/2019, New Partner : Tarif 01/05/2019, , DAVID, david.delpercio@mpsa.com, , ",
                "texts": ", , , DEL PERCIO, 003223884093, , ",
                "createdAt": "2019-05-03T16:20:59.047",
                "tags": [],
                "content_Id": 0
            }
        }
    ]
}

}

我不明白为什么文档 id: 5816 在我搜索“关税”一词时不会出现,而如果我用“新关税”搜索则看起来不错。如果我按单词“new”和单词“208”搜索,它也会出现在结果中。

我也尝试使用此映射,但结果相同:

...  "titles": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    },
                    "analyzer": "jaf_Custom"
                }
            }
        }
    },
    "settings": {
        "index": {
            "number_of_shards": "5",
            "provided_name": "content_1_be_fr",
            "creation_date": "1562575529961",
            "analysis": {
                "analyzer": {
                    "jaf_Custom": {
                        "filter": [
                            "lowercase",
                            "stop",
                            "standard",
                            "snowball"
                        ],
                        "type": "custom",
                        "tokenizer": "standard"
                    }
                }
            },
            "number_of_replicas": "1",
            "uuid": "o0Iz7hnXQTa_ZgM3AxqYpA",
            "version": {
                "created": "6050099"
            }
        }
    }

并完成 http://localhost:9200/content_1_be_fr/_termvectors/5816?fields=titles 给:

{
"_index": "content_1_be_fr",
"_type": "_doc",
"_id": "5816",
"_version": 1,
"found": true,
"took": 15,
"term_vectors": {
    "titles": {
        "field_statistics": {
            "sum_doc_freq": 22882,
            "doc_count": 1995,
            "sum_ttf": 25409
        },
        "terms": {
            "208": {
                "term_freq": 3,
                "tokens": [
                    {
                        "position": 2,
                        "start_offset": 10,
                        "end_offset": 13
                    },
                    {
                        "position": 7,
                        "start_offset": 35,
                        "end_offset": 38
                    },
                    {
                        "position": 10,
                        "start_offset": 50,
                        "end_offset": 53
                    }
                ]
            },
            "2019": {
                "term_freq": 1,
                "tokens": [
                    {
                        "position": 4,
                        "start_offset": 18,
                        "end_offset": 22
                    }
                ]
            },
            "07": {
                "term_freq": 1,
                "tokens": [
                    {
                        "position": 3,
                        "start_offset": 15,
                        "end_offset": 17
                    }
                ]
            },
            "be": {
                "term_freq": 1,
                "tokens": [
                    {
                        "position": 14,
                        "start_offset": 82,
                        "end_offset": 84
                    }
                ]
            },
            "f.mignon": {
                "term_freq": 1,
                "tokens": [
                    {
                        "position": 12,
                        "start_offset": 66,
                        "end_offset": 74
                    }
                ]
            },
            "frederic": {
                "term_freq": 1,
                "tokens": [
                    {
                        "position": 11,
                        "start_offset": 56,
                        "end_offset": 64
                    }
                ]
            },
            "lab360": {
                "term_freq": 1,
                "tokens": [
                    {
                        "position": 13,
                        "start_offset": 75,
                        "end_offset": 81
                    }
                ]
            },
            "new": {
                "term_freq": 3,
                "tokens": [
                    {
                        "position": 1,
                        "start_offset": 6,
                        "end_offset": 9
                    },
                    {
                        "position": 6,
                        "start_offset": 31,
                        "end_offset": 34
                    },
                    {
                        "position": 9,
                        "start_offset": 46,
                        "end_offset": 49
                    }
                ]
            },
            "tarif": {
                "term_freq": 3,
                "tokens": [
                    {
                        "position": 0,
                        "start_offset": 0,
                        "end_offset": 5
                    },
                    {
                        "position": 5,
                        "start_offset": 25,
                        "end_offset": 30
                    },
                    {
                        "position": 8,
                        "start_offset": 40,
                        "end_offset": 45
                    }
                ]
            }
        }
    }
}

}

我想,这意味着“关税”一词已被很好地索引。

标签: elasticsearch

解决方案


我通过添加自定义分析器来解决我的问题:

              "titles": {
                    "type": "text",
                    "analyzer": "jaf_Custom"
                }
            }
        }
    },
    "settings": {
        "index": {
            "number_of_shards": "5",
            "provided_name": "content_1_be_fr",
            "creation_date": "1562655737479",
            "analysis": {
                "filter": {
                    "NG": {
                        "type": "ngram",
                        "min_gram": "2",
                        "max_gram": "10"
                    },
                    "lg": {
                        "type": "stemmer",
                        "language": "french"
                    }
                },
                "analyzer": {
                    "jaf_Custom": {
                        "filter": [
                            "lowercase",
                            "stop",
                            "standard",
                            "snowball",
                            "lg",
                            "NG"
                        ],
                        "type": "custom",
                        "tokenizer": "keyword"
                    }
                }
            },
            "number_of_replicas": "1",
            "uuid": "YGBuNh7jQ2-9VMlI5gpOEg",
            "version": {
                "created": "6050099"
            }
        }
    }

推荐阅读