@IndexedEmbedded和@ContainedIn关系不起作用

时间:2018-08-21 07:29:04

标签: elasticsearch lucene hibernate-search

使用@IndexedEmbedded和@ContainedIn关系时,应将一个索引“ A”中的数据更新为另一索引“ B”。但是仍然使用包含@ContainedIn注释的实体的数据来更新包含@IndexedEmbedded注释的实体。

不确定,我在做什么错。但是根据文档,这应该可行。更新A之后,相同的内容也会反映在B上。

弹性DB结构很好,我相信。但是索引是空的。

但是当我将对象手动保存到Elastic DB时,HibernateMasterSearchIndex会更新,

fullTextSession.save(localBasclt0900TO.getHibernateMasterSearch());

我将必须像上面一样始终保存/删除/更新数据。如果是,那么为什么休眠搜索文档会说,它将自动运行。

我认为这不是正确的方法。

无论如何,下面是代码

    {
  "hibernatemastersearchindex" : {
    "aliases" : { },
    "mappings" : {
      "com.csc.pt.svc.data.to.HibernateMasterSearch" : {
        "dynamic" : "strict",
        "properties" : {
          "basclt0100TO" : {
            "properties" : {
              "clientname" : {
                "type" : "text"
              },
              "cltseqnum" : {
                "type" : "long",
                "store" : true
              },
              "firstname" : {
                "type" : "text"
              },
              "longname" : {
                "type" : "text"
              },
              "midname" : {
                "type" : "text"
              }
            }
          },
          "basclt0900to" : {
            "properties" : {
              "cltseqnum" : {
                "type" : "long",
                "store" : true
              },
              "email1" : {
                "type" : "text",
                "store" : true,
                "analyzer" : "standardAnalyzer"
              }
            }
          },
          "id" : {
            "type" : "keyword",
            "store" : true
          }
        }
      }
    },
    "settings" : {
      "index" : {
        "number_of_shards" : "5",
        "provided_name" : "hibernatemastersearchindex",
        "creation_date" : "1534838340085",
        "analysis" : {
          "filter" : {
            "standardAnalyzer_PatternReplaceFilterFactory" : {
              "all" : "true",
              "pattern" : "([^a-zA-Z0-9\\.])",
              "type" : "pattern_replace",
              "replacement" : " "
            }
          },
          "analyzer" : {
            "standardAnalyzer" : {
              "filter" : [
                "word_delimiter",
                "lowercase",
                "standardAnalyzer_PatternReplaceFilterFactory"
              ],
              "tokenizer" : "standard"
            }
          }
        },
        "number_of_replicas" : "1",
        "uuid" : "Douqy81VQ7mLX-VWiFYAdQ",
        "version" : {
          "created" : "6030299"
        }
      }
    }
  }
}

两个实体是:

HibernateMasterSearch(将要更新)

@Entity
@Indexed(index = "HibernateMasterSearchIndex")
public class HibernateMasterSearch {

    @Id
    @GeneratedValue
    private Long id;


    @OneToOne(cascade = { CascadeType.PERSIST, CascadeType.REMOVE })
    @IndexedEmbedded(prefix = "clt0100_")
    private Basclt0100TO basclt0100TO;

    @OneToOne(cascade = { CascadeType.PERSIST, CascadeType.REMOVE })
    @IndexedEmbedded(prefix = "clt0900_")
    private Basclt0900TO basclt0900to ;

Basclt0900TO(将提供数据)

 @OneToOne(mappedBy = "basclt0900TO")
    @ContainedIn
    private HibernateMasterSearch hibernateMasterSearch;    


    public HibernateMasterSearch getHibernateMasterSearch() {
        return hibernateMasterSearch;
    }
    public void setHibernateMasterSearch(HibernateMasterSearch hibernateMasterSearch) {
     this.hibernateMasterSearch = hibernateMasterSearch;
    }

Basclt0100TO(将提供数据)

@OneToOne
@ContainedIn
private HibernateMasterSearch hibernateMasterSearch;    


public HibernateMasterSearch getHibernateMasterSearch() {
    return hibernateMasterSearch;
}
public void setHibernateMasterSearch(HibernateMasterSearch hibernateMasterSearch) {
    if(hibernateMasterSearch != null) {
        hibernateMasterSearch.setBasclt0100TO(this);
    }
    this.hibernateMasterSearch = hibernateMasterSearch;
}

用于HibernateMasterSearch的Hibernate HBM

    <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                                   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
   <class name="com.csc.pt.svc.data.to.HibernateMasterSearch" table="HibernateMasterSearch">
         <id name="id" type="bphx.c2ab.hibernate.NullSafeLong" column="ID">
            <generator class="identity" />
         </id>
        </class>

  </hibernate-mapping>

创建初始索引

private void createInitialIndex() {
        Session session = HibernateSessionFactory.current().getSession("");
        FullTextSession fullTextSession = Search.getFullTextSession(session.getSession());
        try {

            fullTextSession.createIndexer().startAndWait();

        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

除了上面的代码,我没有在代码中的任何地方使用全文搜索会话。

hibernate.cfg.xml

<property name="hibernate.search.default.indexmanager">elasticsearch</property>
        <property name="hibernate.search.default.elasticsearch.host">http://127.0.0.1:9200</property>
        <property name="hibernate.search.default.elasticsearch.index_schema_management_strategy">drop-and-create</property>
        <property name="hibernate.search.default.elasticsearch.required_index_status">yellow</property>

保存A / Basclt0100的代码

public static DBAccessStatus insert(Basclt0100TO basclt0100TO) {
    DBAccessStatus status = new DBAccessStatus();
    try {
        Session session = HibernateSessionFactory.current().getSession("BASCLT0100");
        if (Util.get(session, basclt0100TO, basclt0100TO.getId()) != null) {
            status.setSqlCode(DBAccessStatus.DB_DUPLICATE);
        } else {
            if (session.contains(basclt0100TO)) {
                session.evict(basclt0100TO);
            }
            LocalDateTime currentDate = LocalDateTime.now();
            Soundex soundex = new Soundex();    
            basclt0100TO.setPhnclient(soundex.encode(basclt0100TO.getClientname()));
            basclt0100TO.setPhnfirst(soundex.encode(basclt0100TO.getFirstname()));

            session.save(basclt0100TO);
            session.flush();
        }
    } catch (HibernateException ex) {
        status.setException(ex);
    }
    return status;
}

以同样的方式将B / Basclt0900和C / Basclt0300持久保存到数据库中。

我是否应该同时将这些持久化到ES DB。不确定如何以这样的方式处理/持久化数据,即应使用复合索引中涉及的数据库表/索引所做的更改来更新复合索引。

0 个答案:

没有答案