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

标签 spring-boot elasticsearch

我是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/

相关文章:

java - 在由 docker 拉取的 Spring Boot 应用程序中实现 LetsEncrypt

java - Spring-cloud-starter-openfeign : SSL handshake exception with feign-httpclient

hibernate - hibernate 搜索分页奇怪的行为

c# - 线程锁内的多线程

java - 构建树中的 spring-security-core 版本冲突问题

java - 如何减少 spring boot Controller 中的重复代码

arrays - Elasticsearch:设置字段中数组元素的数量

json - ElasticSearch将条件语句 'and'与 'or'组合

java - 尝试将列表传递给 hibernate 中的新类时出现错误,org.postgresql.util.PSQLException : ERROR: syntax error at or near "." ,

elasticsearch - Elasticsearch关于搜索嵌套数组对象不起作用的问题