gpt4 book ai didi

spring-boot - 如何在Spring Boot Elastic Search与MongoDB集成中实现对非结构化数据的搜索

转载 作者:行者123 更新时间:2023-12-03 01:24:02 24 4
gpt4 key购买 nike

我是Elasticsearch的新手,想知道以下情况是否对我有用

I wanna achieve search functionality on unstructured data, What I mean by that is I dont know what kind of fields does a model have, as you can see the image below I have a data property inside a model in which any kind of data can be How can I achieve search functionality on this data model

I know how to connect mongodb and elasticsearch using mongo-connect but I dont know that requirement can be achieved or not?

最佳答案

该答案基于您的最新评论。

例如,假设您的data字段映射如下:

PUT my_index
{
"mappings": {
"properties": {
"data": {
"type": "nested"
}
}
}
}

如您所见,我们没有在架构中插入字段,而当我们为第一个文档建立索引时,elastic将为我们做到这一点。

插入一个新文档:
POST my_index/_doc/1
{
"data" : {
"adType" : "SELL",
"price" : "2000",
"numberOfRooms" : 20,
"isNegotiable" : "true",
"area" : 200

}
}

如果我们要搜索单词 SELL,但不知道为其分配了哪个字段,则可以使用以下查询:
GET my_index/_search
{
"query": {
"nested": {
"path": "data",
"query": {
"multi_match": {
"query": "2000",
"fields": [],
"type": "best_fields"
}
}
}
}
}

我们设置 fields=[]的含义:

If no fields are provided, the multi_match query defaults to the index.query.default_field index settings, which in turn defaults to *. * extracts all fields in the mapping that are eligible to term queries and filters the metadata fields. All extracted fields are then combined to build a query.



We used multi_match query

我们得到的结果是:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"data" : {
"adType" : "SELL",
"price" : "2000",
"numberOfRooms" : 20,
"isNegotiable" : "true",
"area" : 200
}
}
}
]
}
}

更新

插入文件
POST my_index/_doc/1
{
"data" : "SELL 2000 20 true 200"
}

然后查询:
GET my_index/_search
{
"query": {
"match":
{
"data":"SELL 2000"
}
}
}

在 Spring 使用QueryBuilder
QueryBuilder qb = QueryBuilders.matchQuery("data", "SELL 2000");

我希望这就是您想要的。

关于spring-boot - 如何在Spring Boot Elastic Search与MongoDB集成中实现对非结构化数据的搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58968680/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com