0

我目前正在通过 Hibernate Search 4.1

我有一个表 (file_data,fileName) 我想创建一个查询,例如 file_data:XYZ 和 fileName:abc.pdf

如何执行此查询?下面我尝试对此进行查询..

Query luceQry3 = queryBuilder
                    .bool()
                    .must(
                            queryBuilder
                            .bool()
                                .must(
                                        queryBuilder.
                                        keyword().
                                        onField("file_data")
                                        .ignoreFieldBridge()
                                        .matching("HDFC")
                                        .createQuery()).createQuery())
                    .must(queryBuilder.
                            keyword().
                            onField("fileName")
                            .ignoreFieldBridge()
                            .matching("0_119.pdf")
                            .createQuery()).createQuery();

但是这个查询和

Query luceneQry = queryBuilder.keyword().
                        onField("file_data").ignoreFieldBridge().
                        matching("HDFC").
                        createQuery();

返回相同的结果。我想使用 AND 运算符,以便可以最小化查询结果。

--------我的实体映射如下-----

@Entity
@Table(name="file_upload")
@Indexed
@Analyzer(impl = org.apache.lucene.analysis.standard.StandardAnalyzer.class)
public class FileUploadModel implements Serializable {


    private static final long serialVersionUID = -5376124674712529869L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Field(index=Index.YES,store=Store.NO)
    @DocumentId
    private Integer file_upload_id;

    @Column(name="file_name")
    @Field(index=Index.YES,store=Store.YES)
    private String fileName;

    @IndexedEmbedded
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="user_id")
    private User user;

    @Column(name="file_type")
    private String file_type;


    @Column(name="file_data")
    @Lob
    @Field(index=Index.YES,store=Store.NO,name="file_data")
    @FieldBridge(impl=com.test.documentsearch.core.BlobToString.class )
    private byte[] file_data;

    public Integer getFile_upload_id() {
        return file_upload_id;
    }

    public void setFile_upload_id(Integer file_upload_id) {
        this.file_upload_id = file_upload_id;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }


    public String getFile_type() {
        return file_type;
    }


    public void setFile_type(String file_type) {
        this.file_type = file_type;
    }


    public byte[] getFile_data() {
        return file_data;
    }


    public void setFile_data(byte[] file_data) {
        this.file_data = file_data;
    }


    public static long getSerialversionuid() {
        return serialVersionUID;
    }

}

我用过FieldBridge...

public class BlobToString extends StringBridge {

    @Override
    public String objectToString(Object object) {
        try
        {
            byte[] byteData = ((String)object).getBytes();
            MagicMatch match = Magic.getMagicMatch((byte[])byteData);
            String mimeType = match.getMimeType();
            String extn = match.getExtension();

            if(mimeType.equalsIgnoreCase("image/jpeg"))
            {

            }
            else if(mimeType.equalsIgnoreCase("application/pdf"))
            {
                COSDocument cd = null;
                ByteArrayInputStream bais =null;
                try
                {
                    bais = new ByteArrayInputStream((byte[])object);
                    PDFParser pdf = new PDFParser(bais);
                    pdf.parse();  
                    cd = pdf.getDocument();  
                    PDFTextStripper stripper = new PDFTextStripper();  
                    String text = stripper.getText(new PDDocument(cd));
                    cd.close();
                    bais.close();
                    return text;
                }
                catch(Exception ex)
                {
                    cd.close();
                    bais.close();
                    ex.printStackTrace();
                }

            }
            else if(mimeType.equalsIgnoreCase("application/rtf") || mimeType.equalsIgnoreCase("text/rtf"))
            {
                ByteArrayInputStream bais = new ByteArrayInputStream((byte[])object);
                RTFEditorKit kit = new RTFEditorKit();
                Document doc = kit.createDefaultDocument();
                kit.read(bais, doc, 0);
                bais.close();
                return doc.getText(0,doc.getLength());
            }
            else if(mimeType.equalsIgnoreCase("application/msword"))
            {
                StringBuilder _result = new StringBuilder();
                try {

                    ByteArrayInputStream bais = new ByteArrayInputStream((byte[]) object);
                    WordExtractor extractor = new WordExtractor(bais);  
                    String wordText = extractor.getText(); 
                    bais.close();
                    return wordText;

                } catch (IOException ex) {
                    ex.printStackTrace();
                }

            }
            else if(mimeType.equalsIgnoreCase("application/vnd.ms-excel"))
            {
                StringBuilder _result = new StringBuilder();
                try {

                    ByteArrayInputStream bais = new ByteArrayInputStream((byte[]) object);
                    WordExtractor extractor = new WordExtractor(bais);  
                    String wordText = extractor.getText(); 
                    bais.close();
                    return wordText;

                } catch (IOException ex) {
                    ex.printStackTrace();
                }

            }
            System.out.println(mimeType);
            return null;
        }catch(Exception ex)
        {
            ex.printStackTrace();
            return ex.getMessage();
        }


    }
}
4

1 回答 1

5

进行两次查询

    Query query1=queryBuilder.keyword().
                onField("file_data").ignoreFieldBridge().
                matching("HDFC").
                createQuery();

    Query query2=queryBuilder.keyword().
                onField("fileName").ignoreFieldBridge().
                matching("abc.pdf").
                createQuery();

将它们添加到列表中:

   listOfQuery.add(query1);
   listOfQuery.add(query2);

然后将最终查询构建为

  Query finalLuceneQuery=new BooleanQuery();
  for (Query query : listOfQuery) {
                    finalLuceneQuery.add(query, BooleanClause.Occur.MUST);
                }
                fullTextQuery = fullTextSession.createFullTextQuery(finalLuceneQuery, <Youclass>.class);

这对你有用。

于 2013-04-15T06:55:22.160 回答